在将产品添加到数据库方面需要一些帮助。 “mysqli_insert_id”行报告错误 这是我的代码:
// Add this product into the database now
$sql = mysqli_query($myConnection,"INSERT INTO products (product_name, price, details, category, subcategory, date_added)
VALUES('$product_name','$price','$details','$category','$subcategory',now())") or die (mysql_error());
$pid = mysqli_insert_id($sql);
// Place image in the folder
$newname = "$pid.jpg";
move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
header("location: inventory_list.php");
exit();
这是它产生的错误:
Warning: mysqli_insert_id() expects parameter 1 to be mysqli, boolean given in /.....line 45
“有趣的是,它仍然将文本插入数据库并显示在我的网站上,但如果我添加其他产品,我仍然会收到错误消息。
我的表确实在 id 上设置了 Auto_Increment
如有任何帮助,我们将不胜感激。拉尔夫
最佳答案
检查 documentation对于 mysqli_insert_id():
mixed mysqli_insert_id ( mysqli $link )
该函数的参数应该是您的 $myConnection
资源,而不是您的 INSERT 语句的 $sql
结果。
当您运行 INSERT 语句时,mysqli_query() 在成功时返回 TRUE,在失败时返回 FALSE。 因此,您传递的是 bool 值 TRUE 而不是连接资源。
关于mysql - "mysqli_insert_id"的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21639226/