繁体   English   中英

Bind_param()错误

[英]Bind_param() error

运行代码时,出现以下错误:

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

我用谷歌搜索了这个问题几个小时,但我不知道问题出在哪里。 我知道这个主题已经发布了很多次了,但是我是php的初学者,我不明白为什么这行不通。

这是我使用的代码:

类:

class Item {
    private $iname;

    function __construct($name)
    {
        $this->iname = $name;
    }

    public function addItem()
    {
        global $mysqli, $db_table_prefix;

            $stmt = $mysqli->prepare("INSERT INTO ".$db_table_prefix."items (
                    iname,
                    VALUES (
                    ?
                    )");
            $stmt->bind_param("s", $this->iname);
            $stmt->execute();
            $inserted_id = $mysqli->insert_id;
            $stmt->close();

    }
}

和发布表单页面:

require_once("models/config.php");


if(!empty($_POST))
    {
        $item_name = trim($_POST["iname"]);

        $item = new Item($item_name);
        $item->addItem();

    }

require_once("models/header.php");
echo "
<body onload='initialize()'>
    <div id='wrapper'>
        <div id='top'><div id='logo'></div></div>
        <div id='content'>
            <h>Add new Item</h>
            <div id='main'>
                <div id='regbox'>
                    <form name='newItem' action='".$_SERVER['PHP_SELF']."' method='post'>
                        <p>
                        <label>Item description:</label>
                        <input type='text' name='iname' />
                        </p>
                        <input type='submit' name='Submit' value='Add Item'/>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>";
?>

您有一个SQL语法错误:

INSERT INTO ".$db_table_prefix."items (
                iname,
                VALUES (
                ?
                )

应该是:

INSERT INTO ".$db_table_prefix."items (
                iname)
                VALUES (
                ?
                )

由于语法错误,未创建$ stmt对象,因此在Call to a member function bind_param() on a non-object错误Call to a member function bind_param() on a non-object

如果遇到问题,请始终输出MySQL错误消息:

echo $mysqli->error;

暂无
暂无

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

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