简体   繁体   English

PHP的PDO更新查询变量

[英]php pdo update query with variables

I hope someone can point out where I am going wrong with this update, I have tried various ways but just can't seem to get it to work, probably a simple mistake but I just can't seem to find it. 我希望有人能指出我在此更新中出错的地方,我尝试了各种方法,但似乎无法使其正常工作,这可能是一个简单的错误,但我似乎找不到它。

function set_live($row_id, $mobile_number)
{
    global $conn;
    $live = 1;

    $sql = "
     UPDATE 
      connections 
     SET 
      live = :live, 
      voice_number = :mobile_number 
     WHERE 
      id = :row_id";

    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':mobile_number', $mobile_number, PDO::PARAM_INT);
    $stmt->bindParam(':row_id', $row_id, PDO::PARAM_INT);
    $stmt->bindParam(':live', $live, PDO::PARAM_INT);
    $stmt->execute();

    echo "Record edited successfully";
    $conn=null;
}

$conn is the PDO connection which works with SELECT's etc All variables are numbers and all echo OK so are in the function I can run the query with the actual values in phpmyadmin and it works OK $conn是与SELECT等一起使用的PDO连接,所有变量都是数字,并且都可以正常执行,因此在函数中,我可以使用phpmyadmin中的实际值运行查询,并且可以正常运行

Just replace this line 只需替换此行

$stmt->bindParam(':mobile_number', $mobile_number, PDO::PARAM_INT);

with this 有了这个

$stmt->bindParam(':mobile_number', $mobile_number, PDO::PARAM_STR);

Because the phone number length is more than integer. 因为电话号码的长度大于整数。

Why not try using an Array? 为什么不尝试使用数组? That approach may well do the Trick for you: 这种方法很可能为您完成了技巧:

    <?php
        function set_live($row_id, $mobile_number){
            global $conn;
            $live = 1;
            $sql    = "UPDATE connections SET live=:live, voice_number=:mobile_number WHERE id=:row_id";
            $stmt   = $conn->prepare($sql);
            $params = array(
                "live"          =>$live,
                "mobile_number" =>$mobile_number,
                "row_id"        =>$row_id,
            );

            $stmt->execute($params);
            echo "Record edited successfully";
            $conn=null;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM