2013-10-30 105 views
0

我一直在嘗試使用一個簡單的POST請求來添加一些基本信息到MySQL數據庫,但我似乎沒有多少運氣 - 它不會添加元組到數據庫。 我知道這段代碼運行它請求的PHP文件,就好像我告訴PHP文件向數據庫添加虛擬信息,它工作正常,但POST字符串中的信息不會被添加。Java和PHP POST請求不工作

這是我的Java文件:

public class Test { 

public static void main(String[] args) { 

    int id = 3696; 
    String name = "David"; 
    int steps = 6989; 

    String strData = encode(id, name, steps); 
    submit(strData); 
} 

public static String encode(int id, String name, int steps) { 

    String strToSend; 

    try 
    { 
     strToSend = URLEncoder.encode("id", "UTF-8") + "="; 
     strToSend += URLEncoder.encode("" + id, "UTF-8"); 
     strToSend += "&" + URLEncoder.encode("name", "UTF-8") + "="; 
     strToSend += URLEncoder.encode(name, "UTF-8"); 
     strToSend += "&" + URLEncoder.encode("steps", "UTF-8") + "="; 
     strToSend += URLEncoder.encode("" + steps, "UTF-8"); 
    } 

    catch (Exception e) { 
     return "Error encoding data for submission"; 
    } 

    return strToSend; 
} 

public static String submit(String strToSend) { 

    URL urlDest; 
    String strData = strToSend; 
    System.out.println(strData); 
    HttpURLConnection ucConnect; 

    try 
    { 
     urlDest = new URL("http://localhost/~David/posttest.php"); 
    } 
    catch (Exception e) 
    { 
     return "Error in submitting entry"; 
    } 


    try 
    { 
     ucConnect = (HttpURLConnection) urlDest.openConnection(); 
     ucConnect.setRequestProperty("Content-type","application/x-www-form-urlencoded"); 
     ucConnect.setRequestMethod("POST"); 
     ucConnect.setDoOutput(true); 

    } 

    catch (Exception e) 
    { 
     return "Error in connecting to URL"; 
    } 

    try 
    { 
     OutputStreamWriter streamWrite = new OutputStreamWriter(ucConnect.getOutputStream()); 
     streamWrite.write(strData); 
     System.out.println(streamWrite.toString()); 
     streamWrite.flush(); 

     //Get the response 

     ucConnect.getInputStream(); 
     System.out.println(ucConnect); 
     streamWrite.close(); 
    } 

    catch (Exception e) 
    { 
     return "Problem"; 
    } 

    System.out.println("SUCCESS"); 
    return "Yay!"; 
} 

}

而這裏的posttest.php

<?php 

$con=mysqli_connect(removed); 

//check connection 
if (mysqli_connect_errno($con)) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 


$id = $_POST["id"]; 
$strName = $_POST["name"]; 
$noSteps = $_POST["steps"]; 

mysqli_query ($con, "INSERT INTO People VALUES ($id, $strName, $noSteps)" || die(); 

mysqli_close($con); 
echo json_encode($arr); 

?> 

我送字符串的打印格式爲:

id=3696&name=David&steps=6989 

感謝您的 幫幫我。

+3

這種類型的請求'的ID = 3696&名稱=大衛&步= 6989'是一個'GET',而不是一個'POST' –

+0

做一個'的var_dump($ _ POST); '看看你是否真的發送數據。此外,如果數據將在稍後由用戶提供,則您需要擔心SQL注入問題。 – PeeHaa

+1

@SteAp - 修改OP的代碼不取決於你。請回復。 –

回答

1

mysqli_query ($con, "INSERT INTO People VALUES ($id, $strName, $noSteps)" 
    || die(); 

語法不正確。請包括:a)在mysqli_query結束:

mysqli_query ($con, "INSERT INTO People VALUES ($id, $strName, $noSteps)") 
    || die(); 
+0

修復了這個錯誤。謝謝。 –