2

私はphpmyadminからphpスクリプトを介してJsonデータを読み込み、リストアクティビティに表示するアプリを持っています。ストア名がクリックされると、+1 がそのストアの投票数に追加され、phpmyadmin に新しい投票数を保存するために php サーバーに送り返されます。選択後、データベースの投票数の値を確認しましたが、更新されません。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

4 に答える 4

0

これがすべての問題ではないかもしれませんが、

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

それはする必要がありますmysql_query

于 2012-05-09T00:03:33.243 に答える
0

サーバー側からデバッグを開始し、逆方向に作業を開始することをお勧めします。

まず、HttpResponse からの応答テキストのログ記録を開始します。

php ファイルで mysql クエリ テキストをエコーし​​、期待どおりに表示されることを確認します。

正しいように見える場合は、データベース構造を確認してください。

そうでない場合は、 を実行して、var_dump($_POST)パラメータが正しく送信されているかどうかを確認してください。

これらの手順を実行すると、問題がどこにあるかをよりよく理解できるはずです。

于 2012-05-08T23:56:29.423 に答える
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;
?>
于 2012-05-09T00:39:06.290 に答える