0

我正在尝试通过 android 表单将数据发布到 mysql 数据库。我将数据发布到服务器上托管的 PHP 脚本。我在 MYSQL 中得到空值。网络服务被调用,但它得到空白数据下面是我的 android 代码:

package com.register;

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 android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;

public class Register extends Activity {




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        EditText email_id = (EditText)  findViewById(R.id.email_id) ;
        EditText name = (EditText) findViewById(R.id.name);
        EditText password = (EditText) findViewById(R.id.password);
        Button button = (Button) findViewById(R.id.button1) ;

        final String email = email_id.getText().toString();
        final String fullname = name.getText().toString();
        final String mpassword = password.getText().toString();



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

        StrictMode.setThreadPolicy(policy); 
button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        //postData();
          HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://xyz/register.php");

            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("email", email));
                nameValuePairs.add(new BasicNameValuePair("name", fullname));
                nameValuePairs.add(new BasicNameValuePair("password", mpassword));
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }

    }
});

    }

下面是我的 PHP 代码:

mysql_connect("server","user","password");
mysql_select_db("testms");
$email =   $_POST['email'];
$name =    $_POST['name'] ;
$password = $_POST['password'] ;

$query_add="INSERT INTO  users (`email` ,`name` ,`password` )
VALUES ('".$email."','".$name."', '".$password."')";
$query_exec=mysql_query($query_add) or die(mysql_error()); 
mysql_close();      

    }
4

4 回答 4

1

尝试 $_REQUEST 变量并在连接到数据库之前获取数据

$email =   $_REQUEST ['email'];  
$name =    $_REQUEST ['name'] ;  
$password = $_REQUEST ['password'] ;    

mysql_connect("server","user","password");  
mysql_select_db("testms"); 
$query_add="INSERT INTO  users (`email` ,`name` ,`password` )  VALUES ('".$email."','".$name."', '".$password."')";  $query_exec=mysql_query($query_add) or die(mysql_error());   
mysql_close();              
}

我通常会发回一些数据以进行调试。

echo "some test string";

在您的应用程序中执行以下操作:只是为了了解,检查来自服务器的响应:

int ResponseCode = response.getStatusLine();

HttpEntity resEntity = response.getEntity();
if( resEntity != null ){
    if( EntityUtils.toString(resEntity).equalsIgnoreCase("some test string") )
    {
        ...do something
    }
    resEntity.consumeContent();
}
于 2012-08-24T19:16:33.253 回答
1

下面的代码应该可以工作,但它没有经过测试——我只是从我正在处理的项目中复制过来的。我将在几分钟内将 PHP 部分中的 MySQL 交互更新为 mysqli(CORRECT 方法),然后我将编辑我的答案。现在,只知道使用 mysql_* 已被贬低,您应该真正清理进出数据库的所有条目。无论如何,试一试:

爪哇:

@Override
public void onClick(View arg0) {
    // generate your params:
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("email", email));
    nameValuePairs.add(new BasicNameValuePair("name", fullname));
    nameValuePairs.add(new BasicNameValuePair("password", mpassword));

    // send them on their way
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://xyz/register.php");
        httpPost.setEntity(new UrlEncodedFormEntity(nameValueParams));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

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

PHP(已折旧/未清理):

<?php 

    $connection = mysql_connect("hostname", "username", "password")or die(mysql_error());
    $selection = mysql_select_db("database", $connection)or die(mysql_error());

    // You should echo these variables back to your app
    // so you know they are sending.
    // echo "Received: " . $email . " - " . $name . " - " . $password;
    $email = $_POST['email'];
    $name = $_POST['name'];
    $password = $_POST['password'];

    $insert = "INSERT INTO users('email','name','password') VALUES('$email','$name','$password')";
    $run = mysql_query($insert)or die(mysql_error());

?>

一个更好的 PHP 示例:

<?php   
    $mysqli_connection = new mysqli("hostname", "username", "password", "database");
    if ($mysqli_connection->connect_errno) {
        echo ("Connection Failure");
        exit();
    }

    $email = mysql_real_escape_string($_POST['email']);
    $name = mysql_real_escape_string($_POST['name']);
    $password = mysql_real_escape_string($_POST['password']);

    $insert = "INSERT INTO users('email','name','password') VALUES('$email','$name','$password')";
    if ($run = $mysql_connection->query($insert)) {
        echo 'Success';
        $run->free();
        $mysql_connection->close();
    } else {
        echo 'Error Inserting Content';
        exit();
    }
?>
于 2012-08-24T19:13:29.023 回答
0

尝试在您的按钮单击事件上移动获取文本字段数据,该变量是最终的,并且在创建活动后它已经具有数据。

于 2013-08-04T17:22:29.113 回答
0

[...]INTO 用户 ( email, name, password)[...] 看来你的报价是错误的。使用 '' 或 "",而不是 ``

于 2012-08-24T18:02:04.480 回答