将bind_param与MySQLi一起使用时出错

时间:2014-10-15 04:42:33

标签: php mysqli prepared-statement

为什么我的代码出错?

$stmt = $mysqli->prepare(
    "INSERT INTO buku VALUES (
        NULL,
        (SELECT id_penerbit FROM penerbit WHERE nm_penerbit = ?),
        (SELECT id_pengarang FROM pengarang WHERE nm_pengarang = ?),
        (SELECT id_kategori FROM kategori WHERE nm_kategori = ?),
        (SELECT id_lemari FROM lemari WHERE nm_lemari = ?),
        ?, ?, ?, ?, ?, 0, ?");
$stmt->bind_param("ssssssisss", $penerbit, $pengarang, $kategori, $lemari, $judul, $isbn, $hal, $terbit, $bahasa, $sinopsis);
if($stmt->execute()) showAlert("Berhasil mengubah kategori", "success");
else showAlert($stmt->error);

我得到的错误是:

  

致命错误:在第34行的C:\ xampp \ htdocs \ eperpus \ admin \ buku-tambah.php中的非对象上调用成员函数bind_param()

1 个答案:

答案 0 :(得分:0)

您在非对象上调用->bind_param() - 这意味着创建对象时出错($stmt)并返回false - as documented

解决问题的唯一方法是调查->prepare()调用失败的原因 - 作为一个只能访问一行代码的局外人有点难以解决的问题:

$stmt = $mysqli->prepare("INSERT INTO buku VALUES (NULL, (SELECT id_penerbit FROM penerbit WHERE nm_penerbit = ?), (SELECT id_pengarang FROM pengarang WHERE nm_pengarang = ?), (SELECT id_kategori FROM kategori WHERE nm_kategori = ?), (SELECT id_lemari FROM lemari WHERE nm_lemari = ?), ?, ?, ?, ?, ?, 0, ?");

修改:查询本身很糟糕,因为它没有关闭VALUES (括号。这就是问题所在。