Page 1 of 1
check if an inputted value has match in the database
Posted: Sun Sep 07, 2008 12:47 am
by zplits
Hi everyone. Good day. Can anyone please help me? Here is my problem.
Sequence:
If the user enters a data into the "code" field, and when he press save, the script will check if that code inserted by the user is already existing in the column name "code" in the database.
If there is an existing entry in the code column, a message will pop-up saying "Code already exist." And if the inputted data of the user doesn't exist in the database, he/she will be able to save it.
As an example:
1. User enters 1211 in the code field and press save. (as to what you can see in the image, the code 1211 which has a description of chess already exists in the database. So a message will be dislayed saying "Code already exist".)
2. And if the user enters the code 7777 which doesn't exist yet in the database. There will be a message saying, "No match found. Please save your item."
Re: check if an inputted value has match in the database
Posted: Sun Sep 07, 2008 12:59 am
by Cut
SELECT the code from your table and then check if mysql_num_rows is >0 for the result.
e.g.,
Code: Select all
if(!ctype_digit($_POST['code']) || strlen($_POST['code'] > 4) { //validate input
echo "Your code must be numeric and less than 5 characters.";
}
else {
$q = "SELECT * FROM mytable WHERE code = '".$_POST['code']."'";
if (mysql_num_rows(mysql_query($q)) != 0) {
echo "Code already exists.";
}
else {
echo "No match found. Please save your item.";
}
}
Re: check if an inputted value has match in the database
Posted: Sun Sep 07, 2008 2:42 am
by zplits
Thanks for that code Cut. It works. Thank you very mind. Can you please explain what this line of code means?
Code: Select all
if (mysql_num_rows(mysql_query($q)) != 0)
Thank you very much.

Re: check if an inputted value has match in the database
Posted: Sun Sep 07, 2008 8:12 am
by markusn00b
zplits wrote:Thanks for that code Cut. It works. Thank you very mind. Can you please explain what this line of code means?
Code: Select all
if (mysql_num_rows(mysql_query($q)) != 0)
Thank you very much.

mysql_num_rows() returns the number of rows affected from the result set of a mysql_query().
So, if you, say, have 3 rows in a DB that match your query
Code: Select all
$result = mysql_query("SELECT * FROM `tbl_name` WHERE `Status` = 'Online'") or die(mysql_error());
a mysql_num_rows() of $result
Would return an (int) 3
Kind regards.
Re: check if an inputted value has match in the database
Posted: Sun Sep 07, 2008 8:23 am
by zplits
I know that sir, but why is it !=0?
Code: Select all
if (mysql_num_rows($codeCheck) != 0)
Re: check if an inputted value has match in the database
Posted: Sun Sep 07, 2008 8:29 am
by markusn00b
zplits wrote:I know that sir, but why is it !=0?
Code: Select all
if (mysql_num_rows($codeCheck) != 0)
'!=' means 'not equal to'. Therefore, it's saying: if this result is not equal to 0 (there is atleast 1 row).
Re: check if an inputted value has match in the database
Posted: Sun Sep 07, 2008 8:37 am
by zplits
Okay i get it. So, value of row will be 1?
Re: check if an inputted value has match in the database
Posted: Sun Sep 07, 2008 9:19 am
by markusn00b
zplits wrote:Okay i get it. So, value of row will be 1?
Yes, that is correct.
Re: check if an inputted value has match in the database
Posted: Sun Sep 07, 2008 9:28 am
by zplits
in my code column in the database, here are some of the listed values:
0010
0020
0001
0007
2001
2990
How could that be processed by php using this code below. If a user enters 0010 in the code field, the script will echo "Code already exists." How is this possible? I still don't fully understand this line of code
Code: Select all
# $q = "SELECT code FROM mytable WHERE code = $code";
# if (mysql_num_rows($result) != 0) {
# echo "Code already exists.";
# }
# else {
# echo "No match found. Please save your item.";
# }
Thanks for helping me out.
Re: check if an inputted value has match in the database
Posted: Sun Sep 07, 2008 10:41 am
by Cut
We SELECT'd every row from your table which contained the user's code. We found one row where the code was equal to '0010'. Thus, the num_rows is not equal to zero, but one. If there are no rows with the user's code, then the num_rows is, of course, zero.