2012-05-10 40 views
1
發送數據時,通過Android的一個PHP的服務器我碰到這個錯誤凸輪

:「通知:未定義指數:在C:\ XAMPP \ htdocs中\ hello.php第4行」。經過幾個小時的搞亂之後,我無法解決我的問題。的Android到PHP錯誤 - 不明指數

hello.php:在安卓

<?php 
mysql_connect("localhost","user","pass"); 
mysql_select_db("mmo"); 
$r=mysql_query("update players set X = '".$_REQUEST['IT']."' where 22=22"); 
if(!$r) 
echo "Error in query: ".mysql_error(); 
mysql_close(); 
header('Refresh: 0.01; URL=http://localhost/hello.php'); 
?> 

更新方法:

public void UpdateSeverWithPlayer() 
{List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("IT","3")); 
try{ 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://10.0.2.2/hello.php"); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity entity = response.getEntity(); 
    is = entity.getContent(); 
    }catch(Exception e){ 
     Log.e("log_tag", "Error in http connection"+e.toString()); 
    }} 

我的工作在Android模擬器,做有上網權限。一如既往的幫助非常感謝。 編輯1:它出現的問題是在Android中,而不是PHP代碼。

+0

數據包嗅探器說...? –

+0

試試print_r($ _ POST)來查看你發佈的內容。 (注意:使用$ _POST而不是$ _REQUEST因爲它更安全 - 當你有機會的時候你還應該檢查函數mysql_real_escape_string();) – Robbie

+0

嗨,好像print_r($ _ POST)返回Array() – jersam515

回答

1

你不發送您的查詢字符串propertly不知何故,所以在PHP端沒有$_REQUEST['IT']。做一個var_dump($_REQUEST)看看會發生什麼。或者更好的是,不要使用$ _REQUEST。這有點不安全。更好地使用與您的HTTP方法直接相關的超全球_GET或_POST。

同樣,Refresh頭是非標準的。它會工作,但你不應該依賴它。 HTTP級別重定向使用Location:標頭。刷新:是一個非標準的舊式Netscape擴展。

+0

的var_dump($ _ REQUEST)返回陣列(0){} – jersam515

+0

然後你有沒有查詢參數/發送形式的值,這意味着誤差是機器人側。 –

+0

謝謝,你認爲代碼有什麼問題? – jersam515