数据未正确传递给php服务器

时间:2012-05-08 23:46:03

标签: php android mysql json phpmyadmin

我有一个应用程序通过php脚本从phpmyadmin读取Json数据并显示在列表活动中。单击商店名称后,将+1添加到该商店的投票计数中,并且应该将其发送回php服务器以在phpmyadmin中存储新的投票计数。选择后,我检查数据库投票计数值,它不会更新。虽然我在logcat中得到HTTP / 1.1 200 ok,但我认为数据没有被正确传递或接受。有人可以帮忙,我被困住了,没有方向。

Android代码:

public void writeJSON() {
    String convertedID;
    String convertedVote;

    //convert int to string value to passed
    convertedID = new Integer(selectedID).toString();
    convertedVote = new Integer(selectedVote).toString();

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.0.2.2/kcstores.php");

    try {

       //writes the output to be stored in creolefashions.com/test2.php
       ArrayList <NameValuePair> nvps = new ArrayList <NameValuePair>(2);
            nvps.add(new BasicNameValuePair("storeUpdate", "update"));
        nvps.add(new BasicNameValuePair("storeID", convertedID));
        nvps.add(new BasicNameValuePair("storeVote", convertedVote));

        httppost.setEntity(new UrlEncodedFormEntity(nvps));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        Log.i("writeJSON", response.getStatusLine().toString());

        } catch(Exception e) {
            Log.e("log_tag", "Error in http connection"+e.toString()); 
        } 
}

PHP代码:

<?php
    $link = mysql_connect("localhost", "root", "") or die (mysql_error());
    mysql_select_db("king_cake_stores")or die (mysql_error());

    $query = "SELECT * FROM storeInfo";
    $result = mysql_query($query);
    $getUpdate = "noupdate";

    if (isset($_POST['storeUpdate'])) {
      echo "receiving data from app";
      $getUpdate = $_POST['storeUpdate'];
      $getStoreID = $_POST['storeID'];
      $getStoreVote = $_POST['storeVote'];
    }

    // If command == getStoreID, it updates the table storeVote value
    // with the android storeVote value based upon correct storeID 
    if ($getUpdate == "update") {
       mysql_select_db("UPDATE storeInfo SET storeVote = $getStoreVote
          WHERE storeID == $getStoreID");
    } else {
    // stores the data in an array to be sent to android application
    while ($line = mysql_fetch_assoc($result)) $output[]=$line;
        print(json_encode($output));
    }
     mysql_close($link);

?>

4 个答案:

答案 0 :(得分:0)

我建议你从服务器端开始调试,然后开始向后工作。

首先,从HttpResponse开始记录响应文本。

回显你的php文件中的mysql查询文本,并确保它看起来像你期望的那样。

如果看起来正确,请检查您的数据库结构。

如果没有,请尝试执行var_dump($_POST),然后检查您的参数是否正确发送。

如果您执行这些步骤,您应该更好地了解问题所在。

答案 1 :(得分:0)

这可能不是整个问题,但是:

mysql_select_db("UPDATE storeInfo SET storeVote = $getStoreVote
      WHERE storeID == $getStoreID");

那应该是mysql_query

答案 2 :(得分:0)

这里尝试使用PDO:

<?php 
//Db Connection Class
Class db{
    private static $instance = NULL;
    private function __construct() {}

    public static function getInstance($DBUSER,$DBPASS) {
        if (!self::$instance){
            try {
                self::$instance = new PDO("mysql:host=localhost;dbname=king_cake_stores", $DBUSER, $DBPASS);
                self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }catch (Exception $e){
                die('Cannot connect to mySQL server.');
            }
        }
        return self::$instance;
    }
    private function __clone(){}
}

//Connect to PDO
$db = db::getInstance('username','password');

//Inser Update
if (isset($_POST['storeUpdate'])) {

    try {
        /*** UPDATE data ***/
        $query = $db->prepare("UPDATE storeInfo
                               SET storeVote = :storeVote
                               WHERE storeID = :storeID");

        $query->bindParam(':storeVote', $_POST['storeVote'], PDO::PARAM_STR);
        $query->bindParam(':storeID',   $_POST['storeID'], PDO::PARAM_INT);

        /*** execute the prepared statement ***/
        $query->execute();

    }catch(PDOException $e){
        echo $e->getMessage();
    }

//Output Current
}else{
    $result = $db->query('SELECT * FROM storeInfo')->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($result);
}
/*** close the database connection ***/
$db = null;
?>

答案 3 :(得分:0)

在每个PHP命令后尝试使用or die。也可以在Android中使用try catch块 这将减少代码中的错误。