2012-05-08 14 views
2

phpスクリプトからJsonデータを読み込み、リストアクティビティに表示するアプリがあります。ストア名がクリックされると、そのストアの投票数に+1が追加され、phpサーバーに送り返されて新しい投票数がphpmyadminに格納されます。選択後、私はdb投票カウント値をチェックし、更新されません。私はlogcatでHTTP/1.1 200 okを取得しますが、データが正しく渡されたり取り込まれたとは思いません。誰かを助けることができます、私は立ち往生し、方向性がありません。データが正しくPHPサーバに渡されていない

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

?> 
+0

更新クエリに '='の代わりに '=='があり、SQLインジェクション –

+0

もあります。storeId == getStoreIdのときは同じレコードなので、更新を続行するためです。 – Skip

+0

[Back to school](= http://www.w3schools.com/sql/sql_where.asp) '= 'Equal –

答えて

0

私は、サーバー側からデバッグを開始し、後方あなたの方法を作業を開始示唆しています。

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

あなたのphpファイルのmysqlクエリテキストをエコーし​​、期待通りに見えるかどうか確認してください。

正しく表示されている場合は、データベースの構造を確認してください。

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

これらの手順を実行する場合は、問題の原因がわかります。

+0

あなたのご意見ありがとうございましたが、ついにそれが機能しました。私は次の問題でこれを採用します。 – Skip

0

これは全体の問題ではなく、可能性がありますmysql_queryでなければなりません

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

。ここで

+0

Danさんに感謝します。あなたの答えは、ローレンスと結局、仕事に私のプログラムを持っていました!あなたのご意見ありがとうございます。 – Skip

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; 
?> 
+0

もう一度ローレンス、ありがとう! – Skip

0

は、すべてのPHPコマンドの後or dieを使用してみてください。また、Androidでtry catchブロックを使用してください。 これはコード内のエラーを減らします。

関連する問題