安卓+ PHP + MYSQL +注册错误 [英] android + php + mysql + register errors

查看:219
本文介绍了安卓+ PHP + MYSQL +注册错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建Android上的一个简单的登记表格使用PHP MySQL来在数据库中创建新的应用和存储数据。

的应用工作得很好,但问题是,该系统将数据插入到数据库,但它显示在模拟器上的错误消息。如何解决这个错误?谁能帮我?

register.java

 包com.sencide;进口java.io.IOException异常;
进口的java.util.ArrayList;
进口的java.util.List;进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicNameValuePair;
进口org.apache.http.protocol.HTTP;
进口org.apache.http.util.EntityUtils;进口android.app.Activity;
进口android.os.Bundle;
进口android.os.StrictMode;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;公共类AndroidRegister扩展活动实现OnClickListener {    EditText上的uname,upass;
    按钮registerBtn;
    TextView的结果;    串的用户,通过;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.register);         。StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建();
            StrictMode.setThreadPolicy(政策);            registerBtn =(按钮)findViewById(R.id.registerbtn);
            registerBtn.setOnClickListener(本);            结果=(的TextView)findViewById(R.id.resulttxt);
    }
    @覆盖
    公共无效的onClick(视图v){
        // TODO自动生成方法存根
        如果(V == registerBtn)
        {
            线程t =新的Thread(){                公共无效的run(){
                    registerData();
                }
            };
            t.start();        }
    }    公共无效registerData()
    {
         的uname =(EditText上)findViewById(R.id.username);
         。用户= uname.getText()的toString();
         upass =(EditText上)findViewById(R.id.password);
         通过= upass.getText()的toString()。         尝试
         {
             //创建一个新的HttpClient和邮政头
                HttpClient的HttpClient的=新DefaultHttpClient();
                Log.e(回应 - >中之后的HttpClient);
            / *如果login.php中的用户名和密码,等于saranga返回true * /
                HttpPost httppost =新HttpPost(http://10.0.2.2/register.php);
                Log.e(回应 - >中之后httppost);                清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(2);
                nameValuePairs.add(新BasicNameValuePair(username的用户));
                nameValuePairs.add(新BasicNameValuePair(密码,PASS));                httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
                Log.e(性反应 - >中,使用列表名称对之后);                //执行HTTP POST请求
                Log.w(SENCIDE,执行HTTP POST请求);
                HTT presponse响应= httpclient.execute(httppost);
                Log.e(性反应 - >中后,再执行HTTP响应);
                字符串str = EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
                Log.d(STR,STR);                如果(str.toString()。equalsIgnoreCase(真)){                    runOnUiThread(新的Runnable(){
                        公共无效的run(){                            result.setText(注册成功);
                        }
                    });                }其他{
                    runOnUiThread(新的Runnable(){
                        公共无效的run(){
                            result.setText(咄没有注册);                        }
                    });
                }         }
          赶上(ClientProtocolException E)
          {
              e.printStackTrace();
           }
          赶上(IOException异常E)
          {
                e.printStackTrace();
           }
    }
}

登录猫

  04-07 22:04:02.932:E /响应 - >(469):HttpClient的后
04-07 22:04:02.932:E /响应 - >(469):后httppost
04-07 22:04:03.033:E /性反应 - >(469):使用列表名称对后
04-07 22:04:03.033:W / SENCIDE(469):执行HTTP POST请求
04-07 22:04:03.552:E /性反应 - >(469):执行HTTP响应之后
04-07 22:04:03.626:D / STR(469):其中; BR />
04-07 22:04:03.626:D / STR(469):其中,字体大小='1'><表类='Xdebug的错误XE预警XE-尖叫'DIR ='升'的边界=' 1'CELLSPACING ='0'CELLPADDING ='1'>
04-07 22:04:03.626:D / STR(469):其中,TR>百分位ALIGN =左的bgcolor =#f57900合并单元格=5><跨度风格=背景颜色:#CC0000;颜色:#fce94f;字体大小:X-大;'>< / SPAN>(!) SCREAM:错误SUP pression忽略< /第i< / TR>
04-07 22:04:03.626:D / STR(469):其中,TR>百分位ALIGN =左的bgcolor =#f57900合并单元格=5><跨度风格=背景颜色:#CC0000;颜色:#fce94f;字体大小:X-大;'>< / SPAN>(!)警告:mysql_fetch_array()预计参数1是资源,布尔用C给出:\\ WAMP \\ WWW \\ register.php上线474; I> 27< / I>< /第i< / TR>
04-07 22:04:03.626:D / STR(469):其中,TR>百分位ALIGN =左的bgcolor =#e9b96e合并单元格=5>调用堆栈和LT; /第i< / TR>
04-07 22:04:03.626:D / STR(469):其中,TR>百分位ALIGN =中心的bgcolor =#eeeeec'>#< /第i个百分位ALIGN =左 BGCOLOR ='#eeeeec'>时间< /第i个百分位ALIGN =左的bgcolor =#eeeeec'>内存< /第i个百分位ALIGN =左的bgcolor =#eeeeec'>功能< /第i个百分位ALIGN =左的bgcolor =#eeeeec'>位置和LT; /第i< / TR>
04-07 22:04:03.626:D / STR(469):其中,TR>< TD的bgcolor =#eeeeec'ALIGN ='中心'> 1< / TD>< TD的bgcolor =#eeeeec ALIGN ='中心'> 0.0593< / TD>< TD的bgcolor =#eeeeec'ALIGN ='正确'> 142592< / TD>< TD的bgcolor =#eeeeec'> {主要}() < / TD>< TD标题=C:\\ WAMP \\ WWW \\ register.php'的bgcolor =#eeeeec'> .. \\ register.php< b>:< / b> 0℃; / TD> < / TR>
04-07 22:04:03.626:D / STR(469):其中,TR>< TD的bgcolor =#eeeeec'ALIGN ='中心'> 2< / TD>< TD的bgcolor =#eeeeec ALIGN ='中心'> 0.3134< / TD>< TD的bgcolor =#eeeeec'ALIGN ='正确'> 150008< / TD>< TD的bgcolor =#eeeeec'>< A HREF = http://www.php.net/mysql_fetch_array'的目标='_新'> mysql_fetch_array< / A>
04-07 22:04:03.626:D / STR(469):()< / TD>< TD标题=C:\\ WAMP \\ WWW \\ register.php'的bgcolor =#eeeeec'> .. \\ register.php< b>:< / b> 27 LT; / TD>< / TR>
04-07 22:04:03.626:D / STR(469):其中,/表>< / FONT>

register.php

 < PHP
$主机=localhost的; //主机名
$用户名=*****; // MySQL用户名
$密码=****; // mysql的密码
$ DB_NAME =TESTLOGIN; //数据库名称
$ tbl_name表=成员; //表名//连接到服务器,然后选择DATABSE。
mysql_connect($主机,$用户名,密码$)或死亡(无法连接);
mysql_select_db($ DB_NAME)或死亡(不能选择DB);$名为myusername = $ _ POST [用户名];
$输入mypassword = $ _ POST ['密码'];//为了保护MySQL的注入
$名为myusername =的stripslashes($名为myusername);
$输入mypassword =的stripslashes($输入mypassword);
$名为myusername = mysql_real_escape_string($名为myusername);
$输入mypassword = mysql_real_escape_string($输入mypassword);$消息=;$ SQL =的mysql_query(INSERT INTO会员(用户名,密码)VALUES('$名为myusername','$输入mypassword'))或死亡(mysql_error());$消息=你现在已经被注册;$的getId = mysql_fetch_array($的SQL);            $ _SESSION ['login_user'] = $的getId [用户名];?>


解决方案

该错误是在你的PHP文件。如果你看看你的logcat,你有:


  

警告:mysql_fetch_array()预计参数1是资源,布尔用C给出:\\ WAMP \\ WWW \\ register.php


这表明,我认为你没有连接到数据库,或者您的查询的格式不正确,或者您没有访问到你正在尝试读取表。 PHP手册显示,请求mysql_query功能可以返回false - 这负责创建 $ SQL 变量。

试试这个:

  $ =的getId @mysql_fetch_array($ SQL)或死亡(mysql_error());

这会告诉你是什么错误 - 它可能是值得的PHP调试结束本 - 这是更难通过Android和logcat的

顺便说一句,我不建议你在现场code使用错误SUP pression操作符( @ ),除非你知道自己在做什么。它可以使调试更加困难,所以照顾它。

I am creating a simple register form on Android to create new use and store data in a database using PHP MySQL.

The application works well but the problem is that the system inserts data to the database but it displays on the emulator the error message. How do I fix this error??? Can anyone can help me??

register.java

package com.sencide;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidRegister extends Activity implements OnClickListener {

    EditText uname, upass;
    Button registerBtn;
    TextView result;

    String user, pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            registerBtn = (Button)findViewById(R.id.registerbtn);
            registerBtn.setOnClickListener(this);

            result = (TextView) findViewById(R.id.resulttxt);
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v == registerBtn)
        {
            Thread t = new Thread() {

                public void run() {
                    registerData();
                }
            };
            t.start();

        } 
    }

    public void registerData()
    {
         uname = (EditText)findViewById(R.id.username);
         user = uname.getText().toString();


         upass = (EditText)findViewById(R.id.password);
         pass = upass.getText().toString();

         try
         {
             // Create a new HttpClient and Post Header
                HttpClient httpclient = new DefaultHttpClient();
                Log.e("Response-->", "after httpclient");
            /* login.php returns true if username and password is equal to saranga */
                HttpPost httppost = new HttpPost("http://10.0.2.2/register.php");
                Log.e("Response-->", "after httppost");

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("username", user));
                nameValuePairs.add(new BasicNameValuePair("password", pass));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                Log.e("Responce-->", "after using the list name pair");

                // Execute HTTP Post Request
                Log.w("SENCIDE", "Execute HTTP Post Request");
                HttpResponse response = httpclient.execute(httppost);
                Log.e("Responce-->", "after execute the http response");
                String str = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
                Log.d("STR", str);

                if (str.toString().equalsIgnoreCase("true")) {

                    runOnUiThread(new Runnable() {
                        public void run() {

                            result.setText("register Successful");
                        }
                    });

                } else {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            result.setText("Duh no register");

                        }
                    });
                }

         }
          catch (ClientProtocolException e)
          {     
              e.printStackTrace(); 
           } 
          catch (IOException e)
          {
                e.printStackTrace();
           }




    }




}

log cat

04-07 22:04:02.932: E/Response-->(469): after httpclient
04-07 22:04:02.932: E/Response-->(469): after httppost
04-07 22:04:03.033: E/Responce-->(469): after using the list name pair
04-07 22:04:03.033: W/SENCIDE(469): Execute HTTP Post Request
04-07 22:04:03.552: E/Responce-->(469): after execute the http response
04-07 22:04:03.626: D/STR(469): <br />
04-07 22:04:03.626: D/STR(469): <font size='1'><table class='xdebug-error xe-warning xe-scream' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
04-07 22:04:03.626: D/STR(469): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> SCREAM: Error suppression ignored for</th></tr>
04-07 22:04:03.626: D/STR(469): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\register.php on line <i>27</i></th></tr>
04-07 22:04:03.626: D/STR(469): <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
04-07 22:04:03.626: D/STR(469): <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
04-07 22:04:03.626: D/STR(469): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0593</td><td bgcolor='#eeeeec' align='right'>142592</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\register.php' bgcolor='#eeeeec'>..\register.php<b>:</b>0</td></tr>
04-07 22:04:03.626: D/STR(469): <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.3134</td><td bgcolor='#eeeeec' align='right'>150008</td><td bgcolor='#eeeeec'><a href='http://www.php.net/mysql_fetch_array' target='_new'>mysql_fetch_array</a>
04-07 22:04:03.626: D/STR(469): (  )</td><td title='C:\wamp\www\register.php' bgcolor='#eeeeec'>..\register.php<b>:</b>27</td></tr>
04-07 22:04:03.626: D/STR(469): </table></font>

register.php

<?php
$host="localhost"; // Host name
$username="*****"; // Mysql username
$password="****"; // Mysql password
$db_name="testlogin"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$myusername=$_POST['username'];
$mypassword=$_POST['password'];

// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$message = "";

$sql = mysql_query("INSERT INTO members (username, password)VALUES('$myusername', '$mypassword')")or die(mysql_error());

$message = "you have  now been registered";

$getid = mysql_fetch_array($sql);

            $_SESSION['login_user'] = $getid['username'];

?>

解决方案

The error is in your PHP file. If you look at your logcat, you have:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\register.php

That suggests to me that you are not connected to the database, or your query is malformed, or you don't have access to the tables you are trying to read. The PHP manual shows that the mysql_query function can return false - this is responsible for creating the $sql variable.

Try this instead:

$getid = @mysql_fetch_array($sql) or die(mysql_error());

That will show you what the error is - it might be worth debugging this at the PHP end - it's much harder via Android and logcat!

Incidentally, I don't recommend you use the error suppression operator (@) in live code unless you know what you are doing. It can make debugging much harder, so take care with it.

这篇关于安卓+ PHP + MYSQL +注册错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆