简体   繁体   English

Bind_param致命错误PHP SQL

[英]Bind_param Fatal error PHP SQL

I am very new to the subject of PHP and SQL working together and I have been fine so far except for updating a database row on my SQL database. 我对PHP和SQL协同工作的主题很陌生,到目前为止,除了在我的SQL数据库上更新数据库行之外,我还算不错。 I'm using parts of my lecturers code and doing exercises and my own tasks to modify the webpages and behaviour. 我正在使用讲师代码的一部分,并通过练习和自己的任务来修改网页和行为。

The purpose of this code is to update an article that I have set up, so I can edit the title or the code then click confirm but when I do this I get my failed return message telling me that it failed to bind my parameters. 这段代码的目的是更新我设置的文章,以便我可以编辑标题或代码,然后单击“确认”,但是当我这样做时,我收到失败的返回消息,告诉我它无法绑定参数。 I have often had trouble passing parameters in other languages and I have been looking and testing this for hours that I am hoping to receive some information and guidance on the subject. 我经常在传递其他语言的参数时遇到麻烦,并且我一直在寻找和测试它几个小时,希望能获得有关该主题的一些信息和指导。

All I want to do is update the articletext and articletitle fields. 我要做的就是更新articletext和articletitle字段。 On the database there is another 3 fields blogID, blogtime, blogposter which don't need changing. 在数据库上还有另外三个字段,它们不需要更改,即blogID,blogtime,blogposter。

If you look at RESULT at the bottom you will see that the variables do have information but are not being updated to the database instead the process crashes during the bind_param section. 如果您查看底部的RESULT,您将看到变量确实具有信息,但没有更新到数据库,而是在bind_param部分中,该过程崩溃。


My EDIT ARTICLE code section: 我的编辑文章代码部分:

 <?php $db=createConnection(); // get the first two articles $sql = "select blogID,articletitle,articletext,blogtime, blogposter,username,userid from blogarticle join registerdemo on blogposter = userid where blogID=?"; $stmt = $db->prepare($sql); $stmt->bind_param(i,$article); $stmt->execute(); $stmt->store_result(); $stmt->bind_result($articleid,$articletitle,$articletext,$blogtime, $blogposter,$username,$userid); //build article html while($stmt->fetch()) { echo "<article id='a$articleid'> <h1>$articletitle</h1> <p>".nl2br($articletext)."</p> <footer><p>Posted on <time datetime='$blogtime'>$blogtime</time> by <em>$username</em></p></footer>"; // if user is logged in and not suspended add comment button if($currentuser['userlevel']>2 || ($currentuser['userid']==$userid && $currentuser['userlevel']>1)) { ?> <form method='post' action='applychanges.php'> <input type="hidden" name="articleid" id="articleid" size="30" value="<?php echo $articleid; ?>"/><br /> <input type="text" name="articletitle" id="articletitle" size="30" required value="<?php echo $articletitle; ?>"/><br /> <textarea name="articletext" id="articletext" cols="60" rows="5"><?php echo $articletext; ?></textarea></br> <button type="submit">Confirm</button> </form> <?php } echo "</article>"; } $stmt->close(); $db->close(); ?> 


My APPLY CHANGES code: 我的APPLY CHANGES代码:

This is where the parameters fail 这是参数失败的地方

 <!doctype html> <html lang="en-gb" dir="ltr"> <head> </head> <body> <?php ini_set('display_errors', 'On'); ini_set('html_errors', 0); error_reporting(-1); print_r($_POST); include('php/functions.php'); if(isset($_POST['articleid']) && isset($_POST['articletitle']) && isset($_POST['articletext'])) { $db=createConnection(); $articleid=$_POST['articleid']; $articletitle=$_POST['articletitle']; $articletext=$_POST['articletext']; $updatesql = "UPDATE blogarticle SET articletitle=?, articletext=? WHERE articleid=?"; $doupdate=$db->prepare($updatesql); $doupdate->bind_param("ssi",$articletitle, $articletext, $articleid); $doupdate->execute(); $doupdate->close(); $db->close(); header("location: index.php"); } else { echo "<p>Some parameters are missing, cannot update database</p>"; print_r($_POST); } ?> </body> </html> 


Result: 结果:

Fatal error: Call to a member function bind_param() on boolean in /home/16018142/public_html/Assessment/applychanges.php on line 18 致命错误:在第18行的/home/16018142/public_html/Assessment/applychanges.php中的布尔值上调用成员函数bind_param()

when I use print_r($_POST) it displays these - 当我使用print_r($ _ POST)时,会显示这些-

Array ( [articleid] => 9 
        [articletitle] => THIS IS A TEST 
        [articletext] => Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test ).

According to http://php.net/manual/en/mysqli-stmt.prepare.php you should put your parameters as ? 根据http://php.net/manual/en/mysqli-stmt.prepare.php,您应该将参数设置为? .

So, line 所以,线

     $updatesql="UPDATE blogarticle SET articletitle='$articletitle', articletext='$articletext' WHERE articleid='$articleid'";

should become 应该成为

 $updatesql="UPDATE blogarticle SET articletitle=?, articletext=? WHERE articleid=?";

UPDATE Also the error may depend on your query. 更新此外,该错误可能取决于您的查询。 Check the return value of 检查的返回值

    $db->prepare

as discussed in 如在

Fatal error: Call to a member function bind_param() on boolean 致命错误:在布尔值上调用成员函数bind_param()

You still dont appear to be setting a value for $article before using it to replace the ? 在使用$article代替$article之前,您似乎仍未为其设置值? in the query using bind_param() 在查询中使用bind_param()

Also the datatype parameter in bind_param need to be in quotes 同样, bind_param的数据类型参数也必须用引号引起来

Also some error checking would be good. 还有一些错误检查将是很好的。

<?php
$db=createConnection();
// get the first two articles
$sql = "select blogID,articletitle,articletext,
                blogtime,blogposter,username,userid 
        from blogarticle 
            join registerdemo on blogposter = userid 
        where blogID=?";

$stmt = $db->prepare($sql);
if (!$stmt) {
    echo $stmt->error;
    exit;
}

// need to give $article a value before you try and use it
// also the datatype parameter need to be in quotes
$stmt->bind_param('i',$article);

$stmt->execute();
if (!$stmt) {
    echo $stmt->error;
    exit;
}

Also add some error checking in this code, basically if the bind is failing with boolean error the prepare failed, so you probably have a SQL syntax error 还要在此代码中添加一些错误检查,基本上,如果绑定由于布尔错误而失败,则准备失败,因此您可能遇到了SQL语法错误

<?php
// set full debugability on for testing
ini_set('display_errors', 1); 
ini_set('log_errors',1); 
error_reporting(E_ALL); 
// next line makes MYSQLI generate exceptions on error
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

print_r($_POST);

include('php/functions.php');
if( isset($_POST['articleid']) && 
    isset($_POST['articletitle']) && 
    isset($_POST['articletext'])) 
{
    $db=createConnection();

    $updatesql = "UPDATE blogarticle 
                    SET articletitle=?, articletext=? 
                  WHERE articleid=?";

    $doupdate=$db->prepare($updatesql);
    if (!$doupdate) {
        echo $doupdate->error;
        exit;
    }

    $doupdate->bind_param("ssi",$_POST['articletitle'],
                                $_POST['articletext']
                                $_POST['articleid']);
    $doupdate->execute();
    if (!$doupdate) {
        echo $doupdate->error;
        exit;
    }

    header("location: index.php");
    // exit after a header as header does no stop
    // the script processing it just send a HTTP header
    exit;       
} else {
    echo "<p>Some parameters are missing, cannot update database</p>";
    print_r($_POST);
}

Call to a member function bind_param() on boolean 在布尔值上调用成员函数bind_param()

I am guessing that line 18 is this: 我猜第18行是这样的:

$doupdate->bind_param("ssi",$articletitle, $articletext, $articleid);

Which means that $doupdate is boolean. 这意味着$doupdate是布尔值。 Its value is set in the preceding line: 它的值在上一行设置:

$doupdate=$db->prepare($updatesql);

The code you ave presented reveals nothing about the the database integration, nor have you provided any explanation in your code, but the method names look like mysqli, hence you might want to insert: 您提供的代码没有透露有关数据库集成的任何信息,也没有在代码中提供任何解释,但是方法名称看起来像mysqli,因此您可能要插入:

if ($db->error) {
    die ($db->error);
}

The error might be a bad password, network issue, or due to not selecting a DB / specifying it in the query. 该错误可能是密码错误,网络问题,或者是由于未选择数据库/未在查询中指定数据库。

Thanks to everyone for posting. 感谢大家发布。 This was eventually figured this out by the recommendations of everyone that posted to look at how values were set and passed. 最终,通过发布来研究如何设置和传递值的每个人的建议,最终弄清了这一点。

Please also note that this would have been resolved earlier by users if I posted more details and information about the database I was using. 另请注意,如果我发布了更多详细信息和有关我正在使用的数据库的信息,则用户会更早解决此问题。

I was looking at $articleid and passing it back to the database as this variable but $articleid has a set value from blogID from the database. 我正在查看$ articleid并将其作为该变量传递回数据库,但$ articleid具有数据库中blogID的设置值。 articleid does not exist as a column on the database. articleid在数据库中不作为一列存在。

To resolve this I simply added the following two lines of code: 为了解决这个问题,我只添加了以下两行代码:

$blogID=$_POST['blogID'];
$blogID=$articleid;

Then changed: 然后更改:

'WHERE articleid=?;' to 'WHERE blogID=?;'.

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

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