所以我想:
我从输入中获得的电话号码称为 phonenumber 。该行称为 id
这是我的代码:
<?php
$db = new PDO('mysql:host=host;dbname=dbname;charset=UTF-8', 'user', 'pass', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
if (isset($_REQUEST['data'])){
$data = json_decode($_REQUEST['data']);
$stmt = $db->prepare("SELECT `id` FROM `locatie` where `phonenumber`=:id");
$stmt->execute(array(':phonenumber' => $data->id));
$row_count = $stmt->rowCount();
if($row_count && ($data->phonenumber != '')){
$stmt = $db->prepare("UPDATE `locatie` SET `longitude`=:longitude, `latitude`=:latitude, `timestamp`=:timestamp WHERE `phonenumber`=:id");
$stmt->execute(array(':longitude' => $data->longitude,':latitude' => $data- >latitude,':timestamp' => $data->timestamp,':phonenumber' => $data->id));
}
}
?>
我做错了什么?
答案 0 :(得分:0)
你的准备有:id,但执行参数有:phone_number。这些需要匹配。
编辑:实际上,你可以做一个简单的更新 - 如果电话号码不存在,那么它就不会改变。一个查询,而不是两个。
$stmt = $db->prepare("UPDATE `locatie` SET `longitude`=:longitude, `latitude`=:latitude, `timestamp`=:timestamp WHERE `id`=:id AND `phonenumber`=:phonenumber");
$stmt->execute(array(':longitude' => $data->longitude,':latitude' => $data- >latitude,':timestamp' => $data->timestamp,':id' => $data->id,':phonenumber' => $data->phonenumber));