2014-12-04 46 views
1

我试图为用户创建可编辑的配置文件。他们点击编辑按钮(form-post)返回带有可编辑信息的页面(只有当isset($ _ POST [“edit”])在文本区域,输入和“完成编辑”按钮时 当我点击完成编辑。它需要启动更新的功能,新的信息到数据库,但它`不更新其返回一个错误:在非对象上调用成员函数bind_param()

Call to a member function bind_param() on a non-object 

我的代码:

if(isset($_POST["cedit"]) && !empty($_POST["fn"]) && !empty($_POST["ln"]) && !empty($_POST["desc"])){ 

    if($stmtq = $mysqli->prepare("UPDATE `sites`.`accounts` SET `fullname` = ? ,`description` = ? WHERE `id` = ? ") && !empty($_POST["fn"]) && !empty($_POST["ln"]) && !empty($_POST["desc"])){ 
     $stmtq->bind_param("ssd", $_POST["fn"]." ".$_POST["ln"], $_POST["desc"], $_SESSION["user_id"]); 
     $stmtq->execute(); 
     $stmtq->close(); 
    } 
} 
+0

在'prepare()'之后执行'print_r($ stmtq);''。还要检查错误日志并在脚本中添加'error_reporting(E_ALL);'作为第一件事 – 2014-12-04 18:18:29

+0

Marcin print_r($ stmtq)返回数字1. – 2014-12-04 18:21:54

回答

5

你有操作问题导致$stmtq成为truefalse布尔型的优先顺序,而不是它正在执行的准备好的语句d是。

这是由于链条&&条件包装成相同的条件。他们发生在作业=之前。加一些()。基本上,这相当于:

// $x is always assigned a boolean 
if ($x = (object && true && true)) 

相反的预期

// $x is assigned the object 
if (($x = object) && (true && true)) 

要解决这个问题:

// Wrap the assignment in its own() group to isolate from the && conditions 
if (($stmtq = $mysqli->prepare("UPDATE `sites`.`accounts` SET `fullname` = ? ,`description` = ? WHERE `id` = ? ")) && !empty($_POST["fn"]) && !empty($_POST["ln"]) && !empty($_POST["desc"]) { 
    // Successful prepare() assignment and all other conditions 
    // Proceed with bind_param()/execute() 
} 

虽然增加了几行,它会更容易阅读和少容易出现这些优先级问题,先执行prepare()并进行赋值,然后验证其他条件,反之亦然。

if (!empty($_POST["fn"]) && !empty($_POST["ln"]) && !empty($_POST["desc"])) { 
    if ($stmtq = $mysqli->prepare(.....)) { 
    // All is well, proceed with bind_param()/execute() 
    } 
} 

对于美味的细节,here is PHP's operator precedence chart。逻辑运算符&&的优先级高于=赋值。

+0

不客气的Michael。 – 2014-12-04 18:25:59

相关问题