我是 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
}