Hello i am trying to check if the product_name is already existing in the database and if it is to display error!
$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;
}
Thanks indeed
Yes this should also work just edit this to
"SELECT * FROM products WHERE product_name = '".$product_name."'";
Also i think limit 1 is not need as you are just checking if it is greater then 0 then error. Also change this
if (($match > 0)===true) {
to
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
}
Have you tried this.
if ($match > 0) {
instead ,
if (($match > 0)===true) {
Cheers!
PS: Don't use mysql_query(), it's obsolete. Use mysqli_query() instead. So for mysql_num_rows(). Refer to PHP documentation : - http://www.php.net/manual/en/function.mysql-query.php