0

我是 Android 新手,我正在使用 PHP 和 MySQL 开发应用程序。我正在尝试从我的设备向 PHP 脚本发送一些数据并将数据添加到我的数据库中,但添加到数据库中的数据为空。我没有使用模拟器,只使用 Android 设备。这是我的 PHP 和 Java 代码:

$dbHost = "localhost";
$dbName = "emergency_db";
$dbUser = "root";
$dbPassword = "";

$connect = mysql_connect($dbHost, $dbUser, $dbPassword) or die ("Connection error"); // connects to database
mysql_select_db($dbName) or die ("Database selection error"); // selects the database

$username=$_POST['username'];
$password=$_POST['password'];
$name=$_POST['name'];
$email=$_POST['email'];

mysql_query("INSERT INTO USERS(Username, Password, Name, Email)VALUES('$username', '$password', '$name' , '$email')");

mysql_close($connect);

try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://localhost/register.php");
    List<NameValuePair> params = new ArrayList<NameValuePair>(4);
    params.add(new BasicNameValuePair("username", username ));
    params.add(new BasicNameValuePair("name", name));
    params.add(new BasicNameValuePair("email", email));
    params.add(new BasicNameValuePair("password", password));
    httppost.setEntity(new UrlEncodedFormEntity(params));
    HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}  
4

1 回答 1

0

使用mysqli. mysql现在不推荐使用。

如果您在公共网络上测试它,则必须使用http://192.168.xxx.xxx/register.php而不是。http://localhost/register.php

用这个代替你mysql_query

mysqli_query($connect,"INSERT INTO test (name,age,email) VALUES ('$name', '$age', '$email');)";
于 2015-06-23T18:43:01.513 回答