HTTP Post不向数据库发送数据?

时间:2012-05-16 11:31:41

标签: php android mysql http http-post

我正在尝试从android应用程序向mysql数据库发送一些数据,数据是字符串形式的(它来自用户完成的测验的结果),我不确定是什么问题。

这是我的HTTP Post的安卓代码:

// http post
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ast.ncl-coll.ac.uk/~cmaughan/firesafety/app_results.php");

            try {
                //Link to quiz string results
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
                nameValuePairs.add(new BasicNameValuePair("StaffID","StaffID"));
                nameValuePairs.add(new BasicNameValuePair("Forename","Forename"));
                nameValuePairs.add(new BasicNameValuePair("Surname", "Surname"));
                nameValuePairs.add(new BasicNameValuePair("Score", "Score"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

,这是我的php:

<?php
include('./inc/connection.inc.php');
connect();
//retrieve the data
$StaffID = $_POST['StaffID'];
$Forename = $_POST['Forename'];
$Surname = $_POST['Surname'];
$Score = $_POST['Score'];

//Insert into the database
$sql = "INSERT INTO tblQuiz (staffid, forename, surname, score) VALUES ('$StaffID',     '$Forename', '$Surname', '$Score')";

$result = mysql_query($sql) or die("Could not run query");



    // Close connection to the database

    mysql_close();


    // Echo success   
    echo "Upload Successful";


?>

任何帮助都会非常感激,我认为数组错误,因为我正在尝试使用HTTP Post方法发送字符串

备注: * 测验的结果将进入DDMS中的mysqllite数据库,我只想将这些结果上传到php / mysql数据库。 *

7 个答案:

答案 0 :(得分:1)

试试这个:

$sql = "INSERT INTO tblQuiz (staffid, forename, surname, score) VALUES ('".$_POST['StaffID']."',     '".$_POST['Forename']."', '".$_POST['Surname']."', '".$_POST['Score']."')";

答案 1 :(得分:1)

  1. 如果要查看结果,可以对结果进行编码 Json格式,因为echo不能发送结构化值。

  2. 如果您的数据是测验的结果,为什么要放置静态 像这样的价值

    nameValuePairs.add(new BasicNameValuePair("StaffID","StaffID"));

    相反,把测验的真实结果放在

  3. 之前,在线测试您未在模拟器上测试的原因

  4. 在php端,请确保在使用值尝试静态时插入值。

答案 2 :(得分:0)

$sql = "INSERT INTO tblQuiz (staffid, forename, surname, score) VALUES ('$StaffID',     '$Forename', '$Surname', '$Score')";

您是否已将$ _POST数组中的值加载到变量“$ StaffID”等中?如果没有,那么你会追求像:

$sql = "INSERT INTO tblQuiz (staffid, forename, surname, score) VALUES ('{$_POST['StaffID']}',     '{$_POST['Forename']}', '{$_POST['Surname']}', '{$_POST['Score']}')";

答案 3 :(得分:0)

   Check ur mysql server connection is established or not

    $con=mysql_connect('host','username','password');
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }else{
    mysql_select_db('db_name',$con);
    $sql = "INSERT INTO tblQuiz (staffid, forename, surname, score) VALUES ("'.$StaffID.'","'.$Forename.'","'.$Surname.'","'.$Score.'")";
    $result = mysql_query($sql);}

答案 4 :(得分:0)

这里的空格是拼写错误还是代码(因为这会导致问题)

HttpPost httppost = new HttpPost("http://ast.ncl-      oll.ac.uk/~cmaughan/firesafety/app_results.php"); 

答案 5 :(得分:0)

$sql = "INSERT INTO tblQuiz (staffid, forename, surname, score) VALUES ('".$_POST['StaffID']."',     '".$_POST['Forename']."', '".$_POST['Surname']."', '".$_POST['Score']."')";

试试这个它会起作用

答案 6 :(得分:0)

如果您是Android应用程序中的webservice,那么您必须提及内容类型,如下面的代码

    public String postData(String result, JSONObject obj) {
            // Create a new HttpClient and Post Header
            String InsertTransactionResult = null;
            HttpClient httpclient = new DefaultHttpClient();
            HttpParams myParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(myParams, 1000);
            HttpConnectionParams.setSoTimeout(myParams, 1000);

            try {

                HttpPost httppost = new HttpPost(result.toString());
                httppost.s

etHeader("Content-type", "application/json");
            StringEntity se = new StringEntity(obj.toString());
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            httppost.setEntity(se);

            HttpResponse response = httpclient.execute(httppost);
            Result = EntityUtils
                    .toString(response.getEntity());

        } catch (ClientProtocolException e) {

        } catch (IOException e) {
        }
        return Result;
    }