我从客户端接收一个 JSON 对象,其中包含随机顺序的位置属性。
地址的几乎每个组成部分都可以跟在后面,也可以跟在另一个前面。例如:州可能出现在国家之前或之后,而城市可能介于两者之间。
所以我创建了一个函数,它将每个参数动态绑定到它所属的位置。但由于某种原因,我在我的数据库中得到的唯一东西是垃圾正整数和负整数。
简单的调试,通过回显生成的查询和绑定值并没有多大帮助。
这是例如的输出(首先是查询,然后是以下形式的绑定参数::param --- value):
[1]UPDATE locations SET active = :active,locality = :locality,local_area = :local_area,state = :state,state_short = :state_short,country_full = :country_full,country_short = :country_short,postal_code = :postal_code,ya = :ya,za = :za WHERE location_id = 385
Params:
:active --- true:locality --- Moscow:local_area --- gorod Moskva:state --- Moscow
:state_short --- Moscow:country_full --- Russia:country_short --- RU:postal_code --- 121069:ya --- 55.7427928:za --- 37.61540089999994
[2]UPDATE locations SET active = :active,street_number = :street_number,route = :route,locality = :locality,sub_local_area = :sub_local_area,local_area = :local_area,state = :state,state_short = :state_short,country_full = :country_full,country_short = :country_short,ya = :ya,za = :za WHERE location_id = 386+++:active --- true:street_number --- 31:route --- Glategny Esplanade:locality --- St Peter Port:sub_local_area --- St Peter Port:local_area --- Guernsey:state --- Bailiwick of Guernsey:state_short --- Bailiwick of Guernsey:country_full --- Grosney:country_short --- GG:ya --- 49.461137:za
--- -2.5333570000000236
代码:
function updateMultipile($table, $data = array()){
$update_query = array();
$i = 0;
foreach($data as $key => $value){
$sql = "UPDATE $table SET ";
foreach($value as $column => $updatedValue){
$sql .= $column . " = :" . $column . ",";
}
$sql = rtrim($sql, ", ");
$sql .= " WHERE location_id = " . intval($key);
$update_query[$i] = $sql;
$i++;
}
try{
$conn = DBLink::getInstance();
$j = 0;
$success = 0;
foreach($data as $fieldset => $value){
echo('[1]');
$sth = $conn->prepare($update_query[$j]);
echo $update_query[$j] . "+++";
foreach($value as $key => $myValue){
$myKey = ':' . $key;
$sth->bindValue($myKey, $myValue); //PDO::PARAM_STR);
echo($myKey . " --- " . $myValue);
}
if($sth->execute()){
$success++;
}
else{
echo $sth->errorCode();
}
$j++;
}
}
catch(PDOException $err){
echo $err::getMessage;
}
return $success;
}