2011-08-19 58 views
0

我試圖發送一些值到服務器中的PHP文件使用Http post ...它只存儲在我的MySQL數據庫的字段中的空值.. ..please幫我...發表一些值到PHP並存儲在數據庫中使用HTTP,只存儲空值

我的Android代碼:

private void sendValues() 
       { 
        List<NameValuePair> data= new ArrayList<NameValuePair>(2); 
        data.add(new BasicNameValuePair("number", "123456789")); 
        data.add(new BasicNameValuePair("msg", "haiRam")); 



        try { 

        HttpClient httpclient=new DefaultHttpClient(); 

         HttpPost httppost=new HttpPost("http://www.mywebsite.com/fasttrack/HttpTest.php"); 

         httppost.setEntity(new UrlEncodedFormEntity(data)); 

         HttpResponse rs=httpclient.execute(httppost); 

       } catch (UnsupportedEncodingException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 

       } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 

       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 

       } 

我的PHP代碼:

<?php 
// Gets data from URL parameters 
$mobile=$_GET['number']; 
$message = $_GET['msg']; 


echo $message; 
echo $mobile; 


$connection=mysql_connect ("mysql", "user", "pass"); 
if (!$connection) { 
    die('Not connected : ' . mysql_error()); 
} 

// Set the active MySQL database 
$db_selected = mysql_select_db("gps", $connection); 
if (!$db_selected) { 
    die ('Can\'t use db : ' . mysql_error()); 
} 

// Insert new row with user data 
$query = sprintf("INSERT INTO Http" . 
     " (mobile,mess) " . 
     " VALUES ('%s', '%s');", 
     mysql_real_escape_string($mobile), 
     mysql_real_escape_string($message)); 

$result = mysql_query($query); 

if (!$result) { 
    die('Invalid query: ' . mysql_error()); 
} 
?> 

回答

1

您正在發送一個POST請求,但您的PHP文件試圖從GET請求獲取值。因此,$mobile$message在您的PHP腳本中始終爲空。

更改你的PHP代碼,以便它從POST請求中得到的值:

// Gets data from URL parameters 
$mobile=$_POST['number']; 
$message = $_POST['msg']; 

順便說一句:echo'ing $mobile$message是不必要的,除非你把它放在那裏進行調試;)

-1

在PHP文件中,使用POST而不是GET ....它會正常工作.....

相關問題