您好我正在尝试检查 product_name 是否已存在于数据库中,是否显示错误!
$product_name = mysql_real_escape_string($_POST['product_name']);
$product_name = sanitize($product_name);
$query = mysql_query("SELECT * FROM products WHERE product_name = '$product_name' LIMIT 1");
$match = mysql_num_rows($query); // count the output amount
if (($match > 0)===true) {
$errors[]='Sorry you tried to place a duplicate "Product Name" into the system!';
break 1;
}
非常感谢
答案 0 :(得分:1)
你试过这个吗?
if ($match > 0) {
而不是
if (($match > 0)===true) {
干杯!
PS:不要使用mysql_query(),它已经过时了。请改用mysqli_query()。所以对于mysql_num_rows()。请参阅PHP文档: - http://www.php.net/manual/en/function.mysql-query.php
答案 1 :(得分:-1)
是的,这也应该只是编辑到
"SELECT * FROM products WHERE product_name = '".$product_name."'";
此外,我认为不需要限制1,因为您只是检查它是否大于0然后错误。 也改变这个
if (($match > 0)===true) {
到
if ($match > 0) {
echo 'Sorry you tried to place a duplicate "Product Name" into the system!';
}
else
{
//Code for insertion or whatever you want to do if its not an error
}