mysqli prep语句错误(bind_param)

时间:2013-06-21 08:22:03

标签: php database mysqli prepared-statement

这是this question的欺骗,但它没有得到回答。所以我告诉我的故事!

<?php

include("connect.php");

$keyz = $_GET['id'];

mysqli_real_escape_string($db, $keyz);

if(!empty($_POST)){

    $query = $db->prepare("UPDATE talents SET (firstName,lastName,gender,dob,email,streetAddress,phoneNumber,city,state,country,zip,eyeColor,hairColor,height,weight,chest,waist,hips,dressSize,shoeSize) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) WHERE pk = ?");
    $query->bind_param('sssssssssssssssssssss',$_POST['firstName'],$_POST['lastName'],$_POST['gender'],$_POST['dob'],$_POST['email'],$_POST['streetAddress'],$_POST['phoneNumber'],$_POST['city'],$_POST['state'],$_POST['country'],$_POST['zip'],$_POST['eyeColor'],$_POST['hairColor'],$_POST['height'],$_POST['weight'],$_POST['chest'],$_POST['waist'],$_POST['hips'],$_POST['dressSize'],$_POST['shoeSize'],$keyz);
    $query->execute();
    $db->close();

    print_r($_POST['firstName'] . " " . $_POST['lastName'] . " has been updated.");

}

$query = "SELECT * FROM talents WHERE pk = " . $keyz;

if(!$result = $db->query($query)){
    die('There was an error running the query [' . $db->error . ']');
}

while($row = $result->fetch_assoc()){

?>
.
.
.
On to populate edit form

Connect.php(这是在我的本地开发机器上):

$db = new mysqli('localhost', 'root', '', '[password]');

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

我收到错误:

Fatal error: Call to a member function bind_param() on a non-object in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\FuseTalent\editTalent.php on line 80

我已尝试过danzan在其他问题和var_dump $ query中提出的建议,但它返回的只是bool(false)

我做错了什么?我从另一个工作页面复制/粘贴/调整了代码。

1 个答案:

答案 0 :(得分:1)

如果出现问题,

mysqli->prepare()返回FALSE。这将导致{FALSE}->bindParam(...);被调用,这将解释错误消息call to a member function ... on a non-object 为脚本添加更多错误处理

$query = $db->prepare("UPDATE talents SET ...");
if ( !$query ) {
    // something went wrong
    // $db->errno and $db->error should contain more information about the error
    // see http://docs.php.net/manual/en/mysqli.error.php
    ...
}

还要看一下MySQL的UPDATE syntax。这是

UPDATE talents SET
firstName=?,
lastName=?,
...
WHERE
...