-4

我正在 Android 上创建一个简单的注册表单,以使用 PHP MySQL 创建新用途并将数据存储在数据库中。

该应用程序运行良好,但问题是系统将数据插入数据库,但它在模拟器上显示错误消息。我该如何解决这个错误???谁能帮帮我??

注册.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();
           }




    }




}

原木猫

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>

注册.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'];

?>
4

1 回答 1

4

错误在您的 PHP 文件中。如果你查看你的 logcat,你有:

警告:mysql_fetch_array() 期望参数 1 是资源,在 C:\wamp\www\register.php 中给出的布尔值

这表明您没有连接到数据库,或者您的查询格式错误,或者您无权访问您尝试读取的表。PHP 手册显示mysql_query 函数可以返回 false - 这负责创建$sql变量。

试试这个:

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

这将告诉你错误是什么——在 PHP 端调试它可能是值得的——通过 Android 和 logcat 更难!

@顺便说一句,除非您知道自己在做什么,否则我不建议您在实时代码中使用错误抑制运算符 ( )。它会使调试变得更加困难,所以要小心。

于 2013-04-07T22:37:55.140 回答