PHP PDO多次更新查询将垃圾插入表中

时间:2013-01-29 09:42:18

标签: php json insert pdo

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

2 个答案:

答案 0 :(得分: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); 
    }
}

答案 1 :(得分:0)

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

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

像魅力一样。

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){
        $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;
}