【问题标题】:Data not properly passed to php server数据未正确传递到 php 服务器
【发布时间】:2012-05-17 11:51:54
【问题描述】:

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

安卓代码:

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);

?>

【问题讨论】:

  • 你的更新查询中有==而不是=,还有sql注入
  • 我有它,因为当storeId == getStoreId时,它意味着它是相同的记录,因此继续更新。
  • Back to school = 相等
  • 为什么在其他实例中单独的“=”将值分配给左侧的右侧变量。使用更新语句有什么不同吗?
  • 谢谢劳伦斯!你的回答加上丹的回答终于让我的程序开始工作了!我真的很感激

标签: php android mysql json phpmyadmin


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这里试试这个,使用 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】:

      这可能不是问题的全部,但是:

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

      应该是mysql_query

      【讨论】:

      • 谢谢丹!你的回答加上劳伦斯的回答终于让我的程序开始工作了!感谢您的意见。
      【解决方案4】:

      我建议您从服务器端开始调试并开始向后工作。

      首先,开始记录来自 HttpResponse 的响应文本。

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

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

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

      如果您完成了这些步骤,您应该会更好地了解问题出在哪里。

      【讨论】:

      • 感谢您的意见,但我终于让它工作了。我将在下一个问题中使用它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多