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

数据未正确传递到php服务器,php,android,mysql,json,phpmyadmin,Php,Android,Mysql,Json,Phpmyadmin,我有一个应用程序,通过php脚本从phpmyadmin读取Json数据,并显示在列表活动中。单击存储名称后,+1将添加到该存储的投票计数中,并应发送回php服务器以将新的投票计数存储在phpmyadmin中。选择之后,我检查db投票计数值,它不会更新。虽然我在logcat中得到了HTTP/1.1200,但我认为数据传递或接收不正确。有人能帮忙吗,我被困在这里,没有方向 Android代码: public void writeJSON() { String convertedID;

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

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()); 
        } 
}
public void writeJSON(){
字符串转换ID;
字符串转换投票;
//将int转换为字符串值以传递
convertedID=新整数(selectedID).toString();
convertedVote=新整数(selectedVote).toString();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://10.0.2.2/kcstores.php");
试一试{
//写入要存储在creolefashions.com/test2.php中的输出
ArrayList nvps=新的ArrayList(2);
添加(新的BasicNameValuePair(“存储更新”、“更新”);
添加(新的BasicNameValuePair(“storeID”,convertedID));
添加(新的BasicNameValuePair(“storeVote”,convertedVote));
setEntity(新的UrlEncodedFormEntity(nvps));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
Log.i(“writeJSON”,response.getStatusLine().toString());
}捕获(例外e){
e(“Log_标记”,“http连接错误”+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);

?>

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

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

在php文件中回显mysql查询文本,并确保其外观符合预期

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

如果没有,请尝试执行
var\u转储($\u POST)
,并检查参数是否正确发送


如果您完成了这些步骤,您应该能够更好地了解问题所在。

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

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

在php文件中回显mysql查询文本,并确保其外观符合预期

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

如果没有,请尝试执行
var\u转储($\u POST)
,并检查参数是否正确发送


如果您完成了这些步骤,您应该能够更好地了解问题所在。

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

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

这应该是mysql\u query

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

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

应该是
mysql\u query

在这里使用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;
?>

在这里尝试使用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;
?>

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

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

在更新查询中有
=
而不是
=
,还有sql injectionI,因为当storeId==getStoreId时,这意味着它是相同的记录,因此继续更新<代码>=equal为什么在其他情况下,只有“=”一个变量会将值分配给右边的变量和左边的变量。使用更新状态时是否有所不同?谢谢Lawrence!你的回答加上丹的话,我的程序终于开始运行了!我真的很感激您在更新查询中使用了
=
而不是
=
,还有sql Injection,因为当storeId==getStoreId时,这意味着它是同一条记录,因此继续更新<代码>=equal为什么在其他情况下,只有“=”一个变量会将值分配给右边的变量和左边的变量。使用更新状态时是否有所不同?谢谢Lawrence!你的回答加上丹的话,我的程序终于开始运行了!我真的很感激汉克斯·丹!你的回答加上劳伦斯的,我的程序终于开始运行了!谢谢你的意见。谢谢丹!你的回答加上劳伦斯的,我的程序终于开始运行了!谢谢你的意见。谢谢你的意见,但我终于让它起作用了。我将在下一期中使用这个。谢谢你的投入,但我终于让它起作用了。我将在下一期中使用这个。