check if an inputted value has match in the database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

check if an inputted value has match in the database

Post by zplits »

Hi everyone. Good day. Can anyone please help me? Here is my problem.

Image

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."
Cut
Forum Commoner
Posts: 39
Joined: Sat Aug 23, 2008 8:01 pm

Re: check if an inputted value has match in the database

Post 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.";
 }
}
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: check if an inputted value has match in the database

Post 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. :)
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: check if an inputted value has match in the database

Post 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

Code: Select all

 
echo mysql_num_rows($result);
 
Would return an (int) 3

Kind regards.
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: check if an inputted value has match in the database

Post by zplits »

I know that sir, but why is it !=0?

Code: Select all

if (mysql_num_rows($codeCheck) != 0)
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: check if an inputted value has match in the database

Post 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).
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: check if an inputted value has match in the database

Post by zplits »

Okay i get it. So, value of row will be 1?
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: check if an inputted value has match in the database

Post by markusn00b »

zplits wrote:Okay i get it. So, value of row will be 1?
Yes, that is correct.
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: check if an inputted value has match in the database

Post 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

mysql_num_rows($result) != 0

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.
Cut
Forum Commoner
Posts: 39
Joined: Sat Aug 23, 2008 8:01 pm

Re: check if an inputted value has match in the database

Post 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.
Post Reply