php预处理语句抛出null错误的问题

时间:2018-04-14 21:34:55

标签: php sql insert

我遇到的问题似乎是我正在创建准备好的sql语句以插入一些数据的方式出错。我的第一个想法是,某些东西是空的,但我知道数据是存在的,因为我用回声测试了它。我的想法是它必须与语句的编写方式有关,但我无法弄清楚它可能有什么问题。

代码:

// Set up the sql command
    $stmt = $mysqli->prepare("INSERT INTO tblcomments (imgpath, author, comment) VALUES(?, ?, ?)");
    // Bind the data
    $stmt->bind_param("imgpath", $imagePath);
    $stmt->bind_param("author", $author);
    $stmt->bind_param("comment", $comment);
    // Execute the sql
    $stmt->execute();
    // Close the statement
    $stmt->close();

问题是insert语句本身,至少根据错误,即:

Fatal error: Uncaught Error: Call to a member function prepare() on null in D:\Inetpub\vhosts\kreativecoating.com\httpdocs\commentDataPostHandler.php:55 Stack trace: #0 {main} thrown in D:\Inetpub\vhosts\kreativecoating.com\httpdocs\commentDataPostHandler.php on line 55

第55行是带有insert语句的行。所以我的问题是我在准备好的声明中做错了吗?我已经检查了数据库字段中的输入错误,所以不是这样。 sql中没有列出id字段,但是键和自动增量,所以它不应该在那里

2 个答案:

答案 0 :(得分:0)

Bind param是一次性的。

-z

S代表字符串

我是整数

d表示浮点数/十进制数

我创建了一个实际生成所有代码的查询生成器。只需插入列名称即可。它有助于准备好的查询:)

https://wbr.bz/QueryPro

答案 1 :(得分:-1)

如果您的数据库连接正常,可以在bindParam()语句中尝试此操作。由于您使用的是插入值的问号,所以它应该是这样的:

$ stmt-> bindParam(1,$ image);

$ stmt-> bindParam(2,$ author);

$ stmt-> bindParam(3,$ comment);

$ stmt->执行();