在同一个PHP文件中执行插入和选择
在我的Web服务中,我有一个用于插入经度和纬度值的页面。成功插入数据库后,将在表中创建一个新行,并使用一个自动递增的键“id”(INT) 它返回一个JSON对象: {“成功”:1,“消息”:“已成功添加点!”} 我的问题是:我想将为每一行创建的id添加到生成的JSON对象中。像这样: {“success”:1,“message”:“Spot Successfully Added!”,“id”:54} 这就是我到目前为止得到的。行被插入到表中,但我无法在JSON对象中显示我的id: {“success”:1,“message”:“Spot Successfully Added!”,“like_id”:null} idtest.php在同一个PHP文件中执行插入和选择,php,mysql,json,web-services,Php,Mysql,Json,Web Services,在我的Web服务中,我有一个用于插入经度和纬度值的页面。成功插入数据库后,将在表中创建一个新行,并使用一个自动递增的键“id”(INT) 它返回一个JSON对象: {“成功”:1,“消息”:“已成功添加点!”} 我的问题是:我想将为每一行创建的id添加到生成的JSON对象中。像这样: {“success”:1,“message”:“Spot Successfully Added!”,“id”:54} 这就是我到目前为止得到的。行被插入到表中,但我无法在JSON对象中显示我的id: {“succe
<?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
}
?>
更改此行
if($row) {
到
因为$row在此之前是找不到的您需要在纬度='“$latt.”和经度='“$long.”周围加引号。
或者最好使用bind语句您是对的!现在我得到{“success”:1,“message”:“Spot Successfully Added!”,“like_id”:null}。你认为有办法解决这个问题吗?这可能是因为DB没有时间通过自动增量生成ID吗?我认为您应该使用$row[0]['ID']无论如何尝试打印($row)并检查您得到的数组。谢谢!现在它似乎工作得很好。我想我不明白$row是一个数组。我使用了$row[0]['id']
if(!$row) {