PHP中UPDATE准备语句的问题

时间:2011-02-07 12:07:27

标签: php mysql prepared-statement

UPDATE准备好的语句有问题,到处查找,浏览过这里的问题,语法似乎是对的,我错过了什么?

$update_page = $db->stmt_init();
$update_page = $db->prepare ("
UPDATE pages 
SET page_title = ?, meta_description = ?, meta_keywords = ?, $content = ? 
WHERE page_uri = ?
");
$update_page->bind_param('sssss', $page_title, $meta_description, $meta_keywords, $content, $page_uri);
$update_page->execute();

这让我失望

  

致命错误:调用成员函数   bind_param()在非对象

指的是第4行(最后一行之上)。

@Gaurav:这是完整的代码 - 我在这个页面上有SELECT和UPDATE语句,SELECT工作:

if (!isset($page_uri))
    {
    $page_uri = 'home'; 
    }

if (isset($_POST['update']))
    {
    $page_title = htmlspecialchars($_POST['page_title']);
    $meta_description = htmlspecialchars($_POST['meta_description']);
    $meta_keywords = htmlspecialchars($_POST['meta_keywords']);
    $content = htmlspecialchars($_POST['content']);

    $update_page = $db->stmt_init();
    $update_page->prepare ("
    UPDATE pages 
    SET page_title = ?, meta_description = ?, meta_keywords = ?, $content = ? 
    WHERE page_uri = ?
    ");

    $update_page->bind_param('sssss', $page_title, $meta_description, $meta_keywords, $content, $page_uri);
    $update_page->execute();
    }


$select_page = $db->stmt_init();
$select_page = $db->prepare ("
SELECT page_id, page_title, meta_description, meta_keywords, content, sidebar 
FROM pages 
WHERE page_uri = ? 
LIMIT 1
");
$select_page->bind_param('s', $page_uri);
$select_page->execute();
$select_page->bind_result($page_id, $page_title, $meta_description, $meta_keywords, $content, $sidebar);
$select_page->fetch();
$page_title = htmlspecialchars_decode($page_title, ENT_QUOTES);
$meta_description = htmlspecialchars_decode($meta_description, ENT_QUOTES);
$meta_keywords = htmlspecialchars_decode($meta_keywords, ENT_QUOTES);
$content = htmlspecialchars_decode($content, ENT_QUOTES);

2 个答案:

答案 0 :(得分:2)

您应该使用$update_page->prepare()

请参阅this

任何对mysqli_stmt函数的后续调用都将失败,直到调用mysqli_stmt_prepare()为止。

编辑:使用ssssi代替sssss

答案 1 :(得分:1)

解决:非常感谢帮助我的人。浏览后我找到了答案:

Why do I get this function call error on an non-object when I am calling a function on an object?

可以从赞成的答案中获得很好的洞察力

基本上,如果你在bind_param()之前做var_dump($ example),你会发现对象是否完全返回 - 在我的情况下它不是 - bool(false),所以我重新考虑了SQL查询本身写信,发现了一个愚蠢的拼写错误,你可以在我的问题中看到(列名为$ content而不是内容) - 有时你只是不能用肉眼捕捉错误所以这很好有一个方法论。我我正在学习:)