2015-10-02 45 views
0

在我的web服務中,我有一個用於插入經度和緯度值的頁面。當成功插入到數據庫中時,將在表中創建一個新行並使用自動遞增的「id」(INT)鍵。在同一個PHP文件中執行INSERT和SELECT操作

它返回一個JSON對象:

{ 「成功」:1, 「消息」: 「現貨添加成功!」}

我的問題:我想補充的編號是爲每個行創建的JSON對象創建的。就像這樣:

{ 「成功」:1, 「消息」: 「現貨添加成功!」, 「ID」:54}

這是我這麼遠。行插入表中,但我不能在JSON對象,以顯示我的ID:

{「成功」:1,「消息」:「現貨成功添加」,「like_id」:空}

idtest.php

<?php 
//load and connect to MySQL database stuff 
require("config.inc.php"); 

if (!empty($_POST)) { 


    //initial query 
    if(isset($_POST['user_id']) && !empty($_POST['user_id'])) { 
     $query = "INSERT INTO spot (latitude, longitude, user_id) VALUES (:lat, :long, :uid) "; 
     //Update query 
     $query_params = array(
      ':lat' => $_POST['latitude'], 
      ':long' => $_POST['longitude'], 
      ':uid' => $_POST['user_id'] 
      ); 

    } else { 
     $query = "INSERT INTO spot (latitude, longitude) VALUES (:lat, :long) "; 
     //Update query 
     $query_params = array(
      ':lat' => $_POST['latitude'], 
      ':long' => $_POST['longitude'] 
      ); 
    } 
    //execute query 
    try { 
     $stmt = $db->prepare($query); 
     $result = $stmt->execute($query_params); 
    } 
    catch (PDOException $ex) { 
     // For testing, you could use a die and message. 
     //die("Failed to run query: " . $ex->getMessage()); 

     //or just use this use this one: 
     $response["success"] = 0; 
     $response["message"] = "Database Error. Couldn't add lat/long-pair!"; 
     die(json_encode($response)); 
    } 

    $latt = $_POST['latitude']; 
    $longg = $_POST['longitude']; 

    $getId = "SELECT id FROM spot WHERE latitude=$latt AND longitude=$longg LIMIT 1"; 
    echo $getId; 
    //execute query 
    try { 
     $stmt2 = $db->prepare($getId); 
     $result2 = $stmt2->execute($query_params); 
    } 
    catch (PDOException $ex) { 
     // For testing, you could use a die and message. 
     //die("Failed to run query: " . $ex->getMessage()); 

     //or just use this use this one: 
     $response["success"] = 0; 
     $response["message"] = "Database Error. Couldn't retrieve like_id!"; 
     die(json_encode($response)); 
    } 

    $row = $stmt2->fetchAll(); 
    if($row) { 

    $response["success"] = 1; 
    $response["message"] = "Spot Successfully Added!"; 
    $response["like_id"] = $row["id"]; 
    echo json_encode($response); 
    } 

} else { 
    ?> 
    <h1>Registrer GPS-koordinater</h1> 
    <form action="idtest.php" method="post"> 
     Latitude:<br /> 
     <input type="text" name="latitude" value="" /> 
     <br /><br /> 
     Longitude:<br /> 
     <input type="text" name="longitude" value="" /> 
     <br /><br /> 
     Google ID:<br /> 
     <input type="text" name="user_id" value="" /> 
     <br /><br /> 
     <input type="submit" value="Opprett kolonne i 'spot'" /> 
    </form> 
    <?php 
} 

?> 
+0

您需要大約'緯度報價=地方找到 ' 「$ LATT。」' 和經度= ' 「$ longg」。''或它最好使用綁定語句 – Saty

回答

2

改變這一行

if($row) { 

if(!$row) { 

因爲$行不會在這之前

+0

你是對的!現在我得到了{「success」:1,「message」:「Spot Successfully Added!」,「like_id」:null}。你認爲有辦法解決這個問題嗎?這可能是因爲數據庫沒有時間通過​​AUTO_INCREMENT生成ID? – user1736049

+1

我想你應該使用$ row [0] ['id'] –

+0

無論如何嘗試print_r($ row)並檢查你得到的數組。 –

相關問題