我是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
}
答案 0 :(得分: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');)";