How to detect conflicted id during registration?

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
azhan
Forum Commoner
Posts: 68
Joined: Fri Jun 27, 2008 6:05 am

How to detect conflicted id during registration?

Post by azhan »

hey guys,

I need a code that could detect similar ID entered from registration, i've try this code that i made by myself but it doesnt work, help me dude...

Code: Select all

<?php
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("hospital") or die(mysql_error());
if (isset($_POST['submit'])) {
 
$take = @mysql_query("SELECT * FROM patientlist");
while($take2 = @mysql_fetch_array($take)){
 if( $_REQUEST['id'] == $take['id'])  {die('Conflicted ID entered');}
 
 else { print ' registered ' ;}
 exit;
 }
 }
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<tr><td>Patient's ID:</td><td>
<input type="INT" name="id" maxlength="50">
</td></tr>
<input type="submit" name="submit" value="ENTER">
 
the result always showing "registered" eventhough the entered id are conflicted...

Regards,
Azhan
_________________________
http://www.productcoverdesign.com-"Cheapest E-Cover Design"
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: How to detect conflicted id during registration?

Post by Bill H »

First, you should use $_POST['id'] rather than $_REQUEST['id'].

Second you are comparing the user input to the result set returned by the query ($take) instead of the array returned by the fetch ($take2).

Third, you are telling it to print "Conflicted ID" if there is a match, and "registered" if there is no match, so it is doing precisely what you programmed it to do. (Although there will never be a match because you are not comparing the input to the fetched array.)

You are also looking at a single result, so if there is more than one patient, only the first one will be examined for a match.

"A computer always does what you tell it to do, not what you want it to do."
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: How to detect conflicted id during registration?

Post by SidewinderX »

I believe what you are looking for it this:

Code: Select all

while($take2 = @mysql_fetch_assoc($take)){
if( $_POST['id'] == $take2['id'])  {die('Conflicted ID entered');}
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: How to detect conflicted id during registration?

Post by Bill H »

That would mean that a match would present the message that the id's did not match rather than that they did.

Code: Select all

<?php
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("hospital") or die(mysql_error());
if (isset($_POST['submit'])) 
{
     $fnd = 0;
     $take = @mysql_query("SELECT * FROM patientlist");
     while($take2 = @mysql_fetch_array($take))
     {
          if ($_POST['id'] == $take2['id'])
          {    print ' registered ' ;
                $fnd = 1;
                break;
           }
      }
      if (!$fnd) print('Conflicted ID entered');
 ?>
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: How to detect conflicted id during registration?

Post by SidewinderX »

Bill H wrote:That would mean that a match would present the message that the id's did not match rather than that they did.
If there is a match, the two ID's should conflict, else it should register the account. At least thats how I interpreted it.
Post Reply