2013-05-13 141 views
0

我是Android新手,我正在開發一個PHP和MySQL應用程序。我試圖從我的設備發送一些數據到PHP腳本,並將數據添加到我的數據庫,但添加到數據庫的數據爲空。我不使用模擬器,只使用Android設備。這是我的PHP和Java代碼:Android設備發送空數據到PHP

$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

你測試你要發送的空值的數據? – kabuto178 2013-05-13 18:49:59

+0

我已經測試了手機中的數據,它們不是空的。 – 2013-05-13 19:02:43

+0

我是新來的PHP,我想你需要打開Db連接 – 2013-05-13 19:22:33

回答

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');)";