Php 使用mysqli\u num\u行错误检查重复项

Php 使用mysqli\u num\u行错误检查重复项,php,mysqli,Php,Mysqli,我正在为一个项目构建一个清单页面,当我试图插入一个新项目时,我正在尝试检查它是否已经在数据库中。我在mysqli_num_rows函数中遇到了一个问题,因为它需要一个参数。我很确定我事先的查询是有效的,所以我不明白它为什么这样做。是因为查询返回0行吗?下面是代码 此外,我正在为mysqli_connect使用include,因此它不在下面的代码中 <?php if(isset($_POST["product_name"])){ $product_name = mysqli_real_esc

我正在为一个项目构建一个清单页面,当我试图插入一个新项目时,我正在尝试检查它是否已经在数据库中。我在mysqli_num_rows函数中遇到了一个问题,因为它需要一个参数。我很确定我事先的查询是有效的,所以我不明白它为什么这样做。是因为查询返回0行吗?下面是代码

此外,我正在为mysqli_connect使用include,因此它不在下面的代码中

<?php
if(isset($_POST["product_name"])){
$product_name = mysqli_real_escape_string($link,$_POST["product_name"]);
$price = mysqli_real_escape_string($link,$_POST["price"]);
$category = mysqli_real_escape_string($link,$_POST["category"]);
$subcategory = mysqli_real_escape_string($link,$_POST["subcategory"]);
$details = mysqli_real_escape_string($link,$_POST["details"]);

$sql = mysqli_query($link,"SELECT id FROM products WHERE product_name = '$product_name'");

if(mysql_num_rows($sql) > 0){
echo "sorry you tried to add a duplicate <a href = Inventory_list.php> Click here </a>";
exit();
}

$sql = mysqli_query($link, "INSERT INTO products       (product_name,price,details,category,subcategory,date_added)
VALUES('$product_name','$price','$details','$category',
'$subcategory',now())") or die("Update Error: ".mysqli_error());
$pid = mysqli_insert_id();
$newname = $pid.jpg;
move_uploaded_file($_FILES['fileField']['tmp_name'],"../Images/$newname");
}

?>

更改

if(mysql_num_rows($sql) > 0){
echo "sorry you tried to add a duplicate <a href = Inventory_list.php> Click here </a>";
exit();
}
if(mysql\u num\u行($sql)>0){
echo“抱歉,您试图添加重复项”;
退出();
}

if(mysqli\u num\u行($sql)>0){
echo“抱歉,您试图添加重复项”;
退出();
}

您会遇到什么错误?你说,
要求一个参数
。mysql_num_row有一个参数,我已经通过将mysql_num_rows更改为mysqli_num_rows修复了这个愚蠢的问题。愚蠢的bug。
if(mysqli_num_rows($sql) > 0){
echo "sorry you tried to add a duplicate <a href = Inventory_list.php> Click here </a>";
exit();
}