0

我从客户端接收一个 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;
}
4

2 回答 2

2

Mysql 不支持多次更新也不支持多次查询(我的默认)。
你需要单独运行这些更新

我现在看到你已经单独运行它们了。你知道很难用你的头脑而不是计算机来运行代码。

除了这段代码的问题之外,为什么不使用一些数据库包装器呢?使代码尽可能简单

function updateMultipile($table, $data = array())
{
    $db = DBLink::getInstance();
    foreach($data as $id => $update)
    {
        $sql = "UPDATE ?n SET ?u WHERE location_id = ?i";
        $db->query($table,$update,$id); 
    }
}
于 2013-01-29T09:58:21.067 回答
0

无论如何,问题似乎是我试图多次执行相同的语句,或者当提供值($myValue)时,bindParam 在 foreach 循环中不起作用,我会使用 $value[ $myKey] 代替,因为此函数接收引用而不是值。

无论如何,我已经决定在这种情况下,对于我的需要,bindValue 就足够了。

奇迹般有效。

函数 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){
        $sth[$j] = $conn->prepare($update_query[$j]);
        foreach($value as $key => $myValue){
            $myKey = ':' . $key;
            $sth[$j]->bindValue($myKey, $myValue); //PDO::PARAM_STR);

        }   
        if($sth[$j]->execute()){
            $success++;
        }
        else{
            echo $sth[$j]->errorCode();
        }

        $j++;
    }   
}
catch(PDOException $err){
    echo $err::getMessage();
}   

return $success;
}
于 2013-01-29T10:17:09.087 回答