繁体   English   中英

Bind_param致命错误PHP SQL

[英]Bind_param Fatal error PHP SQL

我对PHP和SQL协同工作的主题很陌生,到目前为止,除了在我的SQL数据库上更新数据库行之外,我还算不错。 我正在使用讲师代码的一部分,并通过练习和自己的任务来修改网页和行为。

这段代码的目的是更新我设置的文章,以便我可以编辑标题或代码,然后单击“确认”,但是当我这样做时,我收到失败的返回消息,告诉我它无法绑定参数。 我经常在传递其他语言的参数时遇到麻烦,并且我一直在寻找和测试它几个小时,希望能获得有关该主题的一些信息和指导。

我要做的就是更新articletext和articletitle字段。 在数据库上还有另外三个字段,它们不需要更改,即blogID,blogtime,blogposter。

如果您查看底部的RESULT,您将看到变量确实具有信息,但没有更新到数据库,而是在bind_param部分中,该过程崩溃。


我的编辑文章代码部分:

 <?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(); ?> 


我的APPLY CHANGES代码:

这是参数失败的地方

 <!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> 


结果:

致命错误:在第18行的/home/16018142/public_html/Assessment/applychanges.php中的布尔值上调用成员函数bind_param()

当我使用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 ).

根据http://php.net/manual/en/mysqli-stmt.prepare.php,您应该将参数设置为?

所以,线

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

应该成为

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

更新此外,该错误可能取决于您的查询。 检查的返回值

    $db->prepare

如在

致命错误:在布尔值上调用成员函数bind_param()

在使用$article代替$article之前,您似乎仍未为其设置值? 在查询中使用bind_param()

同样, bind_param的数据类型参数也必须用引号引起来

还有一些错误检查将是很好的。

<?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;
}

还要在此代码中添加一些错误检查,基本上,如果绑定由于布尔错误而失败,则准备失败,因此您可能遇到了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);
}

在布尔值上调用成员函数bind_param()

我猜第18行是这样的:

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

这意味着$doupdate是布尔值。 它的值在上一行设置:

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

您提供的代码没有透露有关数据库集成的任何信息,也没有在代码中提供任何解释,但是方法名称看起来像mysqli,因此您可能要插入:

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

该错误可能是密码错误,网络问题,或者是由于未选择数据库/未在查询中指定数据库。

感谢大家发布。 最终,通过发布来研究如何设置和传递值的每个人的建议,最终弄清了这一点。

另请注意,如果我发布了更多详细信息和有关我正在使用的数据库的信息,则用户会更早解决此问题。

我正在查看$ articleid并将其作为该变量传递回数据库,但$ articleid具有数据库中blogID的设置值。 articleid在数据库中不作为一列存在。

为了解决这个问题,我只添加了以下两行代码:

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

然后更改:

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

暂无
暂无

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

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