p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Ch 13 -- User Registration Script


Message #1 by "Michael Gilman" <gilman@a...> on Fri, 6 Sep 2002 04:16:09
I can't tell you how cool it is to get this script up and running (I haven't written 
code since FORTRAN!), but I've got one niggling little problem I can't track down.

If I enter a userid that's already in the database, I don't get the error message 
specified in the script. Rather I get a message that I assume is a generic error from 
MySQL, to wit:

Error: 1062: Duplicate entry for (userid) for key 2

I paste below the code I'm using, pretty much straight from the book with the user 
fields changed a bit. And it otherwise performs beautifully.

Any ideas? Is there an error in coding the "in_use" function -- or in the way I've 
called it?

I'd be grateful for any help. Thanks!

m

script follows:

<?php
//register.php
include "./common_db.inc";

$link_id = db_connect();
mysql_select_db("testbed");
mysql_close($link_id);

function in_use($userid) {
   global $user_tablename;
   
   $query = "SELECT userid FROM $user_tablename WHERE userid = '$userid'";
   $result = mysql_query($query);
   if(!mysql_num_rows($result)) return 0;
   else return 1;
}

function register_form() {
  global $userid, $userfirstname, $userfirstname, $usercity, $useremail, $userstate;
  global $PHP_SELF;
?>
<CENTER><H3>Create your account!</H3></CENTER>
<FORM METHOD="POST" ACTION="<?php echo $PHP_SELF ?>">
<INPUT TYPE="HIDDEN" NAME="action" VALUE="register">
  <DIV ALIGN="CENTER"><CENTER><TABLE BORDER="0" WIDTH="90%">
    <TR>
      <TH WIDTH="30%" NOWRAP>Desired ID</TH>
      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="userid" 
                             VALUE="<?php echo $userid ?>" 
                             SIZE="8" MAXLENGTH="8"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Desired Password</TH>
      <TD WIDTH="70%"><INPUT TYPE="PASSWORD" 
                             NAME="userpassword" SIZE="15"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Retype Password</TH>
      <TD WIDTH="70%"><INPUT TYPE="PASSWORD" 
                             NAME="userpassword2" SIZE="15"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>First Name</TH>
      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="userfirstname" 
                             VALUE="<?php echo $userfirstname ?>" SIZE="20"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Last Name</TH>
      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="userlastname" 
                             VALUE="<?php echo $userlastname ?>" SIZE="20"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Email</TH>
      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="useremail" SIZE="20"
                             VALUE="<?php echo $useremail ?>"></TD>
    </TR>
     <TR>
      <TH WIDTH="30%" NOWRAP>City</TH>
      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="usercity" SIZE="20"
                             VALUE="<?php echo $usercity ?>"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>State/Province</TH>
      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="userstate" SIZE="20"
                             VALUE="<?php echo $userstate ?>"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" COLSPAN="2" NOWRAP>
        <INPUT TYPE="SUBMIT" VALUE="Submit">
        <INPUT TYPE="RESET" VALUE="Reset"></TH>
    </TR>
  </TABLE>
  </CENTER></DIV>
</FORM>
<?php
}

function create_account() {
   global $userid, $userfirstname, $userlastname, $userpassword, 
$userpassword2, 
          $usercity, $userstate, $useremail;
   global $default_dbname, $user_tablename;
   if(empty($userid)) error_message("Enter your desired ID!");
   if(empty($userpassword)) error_message("Enter your desired password!");
   if(strlen($userpassword) < 4 ) error_message("Password too short!");
   if(empty($userpassword2)) 
                  error_message("Retype your password for verification!");
   if(empty($userfirstname)) error_message("Enter your first name!");
   if(empty($userlastname)) error_message("Enter your last name!");
   if(empty($useremail)) error_message("Enter your email address!");
   if(empty($usercity)) error_message("Enter your city!");
   if(empty($userstate)) error_message("Enter your state or province!");
  
   if($userpassword != $userpassword2)
      error_message("Your desired password and retyped password mismatch!");
   
   $link_id = db_connect($default_dbname);
   
   if(in_use($userid))
         error_message("$userid is in use. Please choose a different ID.");
   $query = "INSERT INTO users VALUES(NULL, '$userid', 
                                     password('$userpassword'), '$userfirstname',
                                    '$userlastname', '$useremail', '$usercity',
                                    '$userstate', curdate(), NULL)";
   $result = mysql_query($query);
   if(!$result) error_message(sql_error());
   $usernumber = mysql_insert_id($link_id);
   html_header();
?>   
<CENTER><H3>
<?php echo $userfirstname ?>, thank you for registering with us!
</H3></CENTER>

<DIV ALIGN="CENTER"><CENTER><TABLE BORDER="0" WIDTH="90%">
  <TR>
    <TH WIDTH="30%" NOWRAP>User Number</TH>
    <TD WIDTH="70%"><?php echo $usernumber ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Desired ID</TH>
    <TD WIDTH="70%"><?php echo $userid ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Desired Password</TH>
    <TD WIDTH="70%"><?php echo $userpassword ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Full Name</TH>
    <TD WIDTH="70%"><?php echo $userfirstname . " " . $userlastname ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Location</TH>
    <TD WIDTH="70%"><?php echo $usercity . ", " . $userstate ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Email</TH>
    <TD WIDTH="70%"><?php echo $useremail ?></TD>
  </TR>
</TABLE>
</CENTER></DIV>
<?php
	html_footer();
}

switch($action) {
   case "register": 
      create_account();
   break;
   default:
      html_header();
      register_form();
      html_footer();
   break;
}
?>
Message #2 by <jorge@d...> on Thu, 5 Sep 2002 21:08:28 -0500 (CDT)
Hi the problem is that you are trying to insert the same info into a
unique field, the field is unique doesnt allow duplicates.
if the info on the table is just testing info empty the table before
making another test an see what happens

$query="DELETE FROM mytable"

Jorge

:)






error message  specified in the script. Rather I get a message that I
> assume is a generic error from  MySQL, to wit:
>
> Error: 1062: Duplicate entry for (userid) for key 2
>
> I paste below the code I'm using, pretty much straight from the book
> with the user  fields changed a bit. And it otherwise performs
> beautifully.
>
> Any ideas? Is there an error in coding the "in_use" function -- or in
> the way I've  called it?
>
> I'd be grateful for any help. Thanks!
>
> m
>
> script follows:
>
> <?php
> //register.php
> include "./common_db.inc";
>
> $link_id = db_connect();
> mysql_select_db("testbed");
> mysql_close($link_id);
>
> function in_use($userid) {
>   global $user_tablename;
>
>   $query = "SELECT userid FROM $user_tablename WHERE userid 
>   '$userid'"; $result = mysql_query($query);
>   if(!mysql_num_rows($result)) return 0;
>   else return 1;
> }
>
> function register_form() {
>  global $userid, $userfirstname, $userfirstname, $usercity, $useremail,
>  $userstate; global $PHP_SELF;
> ?>
> <CENTER><H3>Create your account!</H3></CENTER>
> <FORM METHOD="POST" ACTION="<?php echo $PHP_SELF ?>">
> <INPUT TYPE="HIDDEN" NAME="action" VALUE="register">
>  <DIV ALIGN="CENTER"><CENTER><TABLE BORDER="0" WIDTH="90%">
>    <TR>
>      <TH WIDTH="30%" NOWRAP>Desired ID</TH>
>      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="userid"
>                             VALUE="<?php echo $userid ?>"
>                             SIZE="8" MAXLENGTH="8"></TD>
>    </TR>
>    <TR>
>      <TH WIDTH="30%" NOWRAP>Desired Password</TH>
>      <TD WIDTH="70%"><INPUT TYPE="PASSWORD"
>                             NAME="userpassword" SIZE="15"></TD>
>    </TR>
>    <TR>
>      <TH WIDTH="30%" NOWRAP>Retype Password</TH>
>      <TD WIDTH="70%"><INPUT TYPE="PASSWORD"
>                             NAME="userpassword2" SIZE="15"></TD>
>    </TR>
>    <TR>
>      <TH WIDTH="30%" NOWRAP>First Name</TH>
>      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="userfirstname"
>                             VALUE="<?php echo $userfirstname ?>"
>                             SIZE="20"></TD>
>    </TR>
>    <TR>
>      <TH WIDTH="30%" NOWRAP>Last Name</TH>
>      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="userlastname"
>                             VALUE="<?php echo $userlastname ?>"
>                             SIZE="20"></TD>
>    </TR>
>    <TR>
>      <TH WIDTH="30%" NOWRAP>Email</TH>
>      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="useremail" SIZE="20"
>                             VALUE="<?php echo $useremail ?>"></TD>
>    </TR>
>     <TR>
>      <TH WIDTH="30%" NOWRAP>City</TH>
>      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="usercity" SIZE="20"
>                             VALUE="<?php echo $usercity ?>"></TD>
>    </TR>
>    <TR>
>      <TH WIDTH="30%" NOWRAP>State/Province</TH>
>      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="userstate" SIZE="20"
>                             VALUE="<?php echo $userstate ?>"></TD>
>    </TR>
>    <TR>
>      <TH WIDTH="30%" COLSPAN="2" NOWRAP>
>        <INPUT TYPE="SUBMIT" VALUE="Submit">
>        <INPUT TYPE="RESET" VALUE="Reset"></TH>
>    </TR>
>  </TABLE>
>  </CENTER></DIV>
> </FORM>
> <?php
> }
>
> function create_account() {
>   global $userid, $userfirstname, $userlastname, $userpassword,
> $userpassword2,
>          $usercity, $userstate, $useremail;
>   global $default_dbname, $user_tablename;
>   if(empty($userid)) error_message("Enter your desired ID!");
>   if(empty($userpassword)) error_message("Enter your desired
>   password!"); if(strlen($userpassword) < 4 ) error_message("Password
>   too short!"); if(empty($userpassword2))
>                  error_message("Retype your password for
>                  verification!");
>   if(empty($userfirstname)) error_message("Enter your first name!");
>   if(empty($userlastname)) error_message("Enter your last name!");
>   if(empty($useremail)) error_message("Enter your email address!");
>   if(empty($usercity)) error_message("Enter your city!");
>   if(empty($userstate)) error_message("Enter your state or province!");
>
>   if($userpassword != $userpassword2)
>      error_message("Your desired password and retyped password
>      mismatch!");
>
>   $link_id = db_connect($default_dbname);
>
>   if(in_use($userid))
>         error_message("$userid is in use. Please choose a different
>         ID.");
>   $query = "INSERT INTO users VALUES(NULL, '$userid',
>                                     password('$userpassword'),
>                                     '$userfirstname',
>                                    '$userlastname', '$useremail',
>                                    '$usercity', '$userstate',
>                                    curdate(), NULL)";
>   $result = mysql_query($query);
>   if(!$result) error_message(sql_error());
>   $usernumber = mysql_insert_id($link_id);
>   html_header();
> ?>
> <CENTER><H3>
> <?php echo $userfirstname ?>, thank you for registering with us!
> </H3></CENTER>
>
> <DIV ALIGN="CENTER"><CENTER><TABLE BORDER="0" WIDTH="90%">
>  <TR>
>    <TH WIDTH="30%" NOWRAP>User Number</TH>
>    <TD WIDTH="70%"><?php echo $usernumber ?></TD>
>  </TR>
>  <TR>
>    <TH WIDTH="30%" NOWRAP>Desired ID</TH>
>    <TD WIDTH="70%"><?php echo $userid ?></TD>
>  </TR>
>  <TR>
>    <TH WIDTH="30%" NOWRAP>Desired Password</TH>
>    <TD WIDTH="70%"><?php echo $userpassword ?></TD>
>  </TR>
>  <TR>
>    <TH WIDTH="30%" NOWRAP>Full Name</TH>
>    <TD WIDTH="70%"><?php echo $userfirstname . " " . $userlastname
>    ?></TD>
>  </TR>
>  <TR>
>    <TH WIDTH="30%" NOWRAP>Location</TH>
>    <TD WIDTH="70%"><?php echo $usercity . ", " . $userstate ?></TD>
>  </TR>
>  <TR>
>    <TH WIDTH="30%" NOWRAP>Email</TH>
>    <TD WIDTH="70%"><?php echo $useremail ?></TD>
>  </TR>
> </TABLE>
> </CENTER></DIV>
> <?php
> 	html_footer();
> }
>
> switch($action) {
>   case "register":
>      create_account();
>   break;
>   default:
>      html_header();
>      register_form();
>      html_footer();
>   break;
> }
> ?>



Message #3 by "Michael Gilman" <gilman@a...> on Fri, 6 Sep 2002 11:39:02
Thanks, Jorge.

But shouldn't I get the error message specified by this line:


>   if(in_use($userid))
>         error_message("$userid is in use. Please choose a different
>         ID.");


before the script actually accesses the database (and generates the 1062 error)?

m
Message #4 by "jorge" <jorge@d...> on Fri, 6 Sep 2002 08:47:24 -0500
Mysql send its own error message unless is turn off
with @

Jorge
:)
----- Original Message -----
From: "Michael Gilman" <gilman@a...>
To: "beginning php" <beginning_php@p...>
Sent: Friday, September 06, 2002 11:39 AM
Subject: [beginning_php] Re: Ch 13 -- User Registration Script


> Thanks, Jorge.
>
> But shouldn't I get the error message specified by this line:
>
>
> >   if(in_use($userid))
> >         error_message("$userid is in use. Please choose a different
> >         ID.");
>
>
> before the script actually accesses the database (and generates the 1062
error)?
>
> m


Message #5 by "jorge" <jorge@d...> on Fri, 6 Sep 2002 08:49:51 -0500
i would recommend to test all of your queries like

if(!($query...)){echo("ERROR IN  my query line number ".mysql_error());}

Jorge

----- Original Message -----
From: "Michael Gilman" <gilman@a...>
To: "beginning php" <beginning_php@p...>
Sent: Friday, September 06, 2002 11:39 AM
Subject: [beginning_php] Re: Ch 13 -- User Registration Script


> Thanks, Jorge.
>
> But shouldn't I get the error message specified by this line:
>
>
> >   if(in_use($userid))
> >         error_message("$userid is in use. Please choose a different
> >         ID.");
>
>
> before the script actually accesses the database (and generates the 1062
error)?
>
> m


Message #6 by "Michael Gilman" <gilman@a...> on Fri, 6 Sep 2002 19:44:31
But, Jorge, (and once again many thanks for taking the time to help), 
shouldn't the script hit and execute my coded error statement before it 
actually queries the database and generates the other one?

Thanks!

m

> i would recommend to test all of your queries like

if(!($query...)){echo("ERROR IN  my query line number ".mysql_error());}

Jorge

----- Original Message -----
From: "Michael Gilman" <gilman@a...>
To: "beginning php" <beginning_php@p...>
Sent: Friday, September 06, 2002 11:39 AM
Subject: [beginning_php] Re: Ch 13 -- User Registration Script


> Thanks, Jorge.
>
> But shouldn't I get the error message specified by this line:
>
>
> >   if(in_use($userid))
> >         error_message("$userid is in use. Please choose a different
> >         ID.");
>
>
> before the script actually accesses the database (and generates the 1062
error)?
>
> m


Message #7 by "Nikolai Devereaux" <yomama@u...> on Fri, 6 Sep 2002 13:48:51 -0700
Yes, you should get that error first, provided your in_use() function works
properly!  Have you tested it?

Also, you use error_message() to output all errors... where is this defined?
Could it be that error_message() is being called, but it (for some reason)
doesn't output anything?


Finally, you're not using an else block after the if(in_use(..)) block.  You
would think that you only would attempt to insert the new username when the
name was not already in use, right?

my suggested patches:

function in_use($userid)
{
   global $user_tablename;
   $query = "SELECT COUNT(*) FROM $usertablename
              WHERE userid='{$userid}'";
   $result = mysql_query($query);
   return (mysql_result($result, 0) > 0);
}


function create_account()
{
  ...

  if(in_use($userid))
  {
     error_message(...);
  }
  else
  {
    // do the insert here
  }
  ...
}


take care,

nik

Message #8 by "jorge" <jorge@d...> on Fri, 6 Sep 2002 16:27:42 -0500
i cant find your script
 i will suggest
if (mysql_num_rows==0){ register}else{echo "That user has been already
taken,please try agian";}

please send me your script

Jorge
----- Original Message -----
From: "Michael Gilman" <gilman@a...>
To: "beginning php" <beginning_php@p...>
Sent: Friday, September 06, 2002 7:44 PM
Subject: [beginning_php] Re: Ch 13 -- User Registration Script


> But, Jorge, (and once again many thanks for taking the time to help),
> shouldn't the script hit and execute my coded error statement before it
> actually queries the database and generates the other one?
>
> Thanks!
>
> m
>
> > i would recommend to test all of your queries like
>
> if(!($query...)){echo("ERROR IN  my query line number ".mysql_error());}
>
> Jorge
>
> ----- Original Message -----
> From: "Michael Gilman" <gilman@a...>
> To: "beginning php" <beginning_php@p...>
> Sent: Friday, September 06, 2002 11:39 AM
> Subject: [beginning_php] Re: Ch 13 -- User Registration Script
>
>
> > Thanks, Jorge.
> >
> > But shouldn't I get the error message specified by this line:
> >
> >
> > >   if(in_use($userid))
> > >         error_message("$userid is in use. Please choose a different
> > >         ID.");
> >
> >
> > before the script actually accesses the database (and generates the 1062
> error)?
> >
> > m
>
>


Message #9 by "jorge" <jorge@d...> on Fri, 6 Sep 2002 16:36:48 -0500
I found this piece of code which has missing {}in the if statement
function in_use($userid) {
   global $user_tablename;

   $query = "SELECT userid FROM $user_tablename WHERE userid = '$userid'";
   $result = mysql_query($query);
   if(!mysql_num_rows($result))
    {
        return 0;
      }else{
                return 1;
                }//ends if else
}//ends function

Jorge
:)
----- Original Message -----
From: "Michael Gilman" <gilman@a...>
To: "beginning php" <beginning_php@p...>
Sent: Friday, September 06, 2002 7:44 PM
Subject: [beginning_php] Re: Ch 13 -- User Registration Script


> But, Jorge, (and once again many thanks for taking the time to help),
> shouldn't the script hit and execute my coded error statement before it
> actually queries the database and generates the other one?
>
> Thanks!
>
> m
>
> > i would recommend to test all of your queries like
>
> if(!($query...)){echo("ERROR IN  my query line number ".mysql_error());}
>
> Jorge
>
> ----- Original Message -----
> From: "Michael Gilman" <gilman@a...>
> To: "beginning php" <beginning_php@p...>
> Sent: Friday, September 06, 2002 11:39 AM
> Subject: [beginning_php] Re: Ch 13 -- User Registration Script
>
>
> > Thanks, Jorge.
> >
> > But shouldn't I get the error message specified by this line:
> >
> >
> > >   if(in_use($userid))
> > >         error_message("$userid is in use. Please choose a different
> > >         ID.");
> >
> >
> > before the script actually accesses the database (and generates the 1062
> error)?
> >
> > m
>
>


Message #10 by "Nikolai Devereaux" <yomama@u...> on Fri, 6 Sep 2002 14:34:47 -0700
> I found this piece of code which has missing {}in the if statement
> function in_use($userid) {
>    global $user_tablename;
>
>    $query = "SELECT userid FROM $user_tablename WHERE userid = '$userid'";
>    $result = mysql_query($query);
>    if(!mysql_num_rows($result))
>     {
>         return 0;
>       }else{
>                 return 1;
>                 }//ends if else
> }//ends function


Uhh.... you don't _need_ curly braces there...  The grammar for an if
control block looks like this

  if(<condition>) <stmt> [else <stmt>]

where <condition> is some expression which evaluates to a boolean TRUE or
FALSE value, and <stmt> is just a single statement; for example "return 1;".

You only need the curly braces for a compound statement -- that tells the
interpreter that instead of just one statement, you have a block of
statements.

To continue my earlier notation,

<stmt> is either a single statement or a compound statement.


{
   return 1;
}

is technically a compound statement with only one actual statement in it.


At any rate, the entire return logic can be simplified like this:

return (mysql_num_rows($result) > 0);



Take care,

nik

Message #11 by "Michael Gilman" <gilman@a...> on Sun, 8 Sep 2002 00:14:20
Thanks to everyone for their help with this. After some serious time spent echoing 
every step of the script -- and following the pointers provided by you folks -- I 
finally discovered the problem was that the table field name in the script did not 
match the one in the table. So the in_user function returned "0", the script blew 
through my defined error message and went on to insert data into the table, 
finding a duplicate and generating its own message.

D'oh!!!

But I've got to say, I learned a lot of new coding tricks from this.

So thanks again to everyone for their help and patience with my newbie problem!

m

> 
> I found this piece of code which has missing {}in the if statement
> function in_use($userid) {
>    global $user_tablename;
>
>    $query = "SELECT userid FROM $user_tablename WHERE userid = '$userid'";
>    $result = mysql_query($query);
>    if(!mysql_num_rows($result))
>     {
>         return 0;
>       }else{
>                 return 1;
>                 }//ends if else
> }//ends function


Uhh.... you don't _need_ curly braces there...  The grammar for an if
control block looks like this

  if(<condition>) <stmt> [else <stmt>]

where <condition> is some expression which evaluates to a boolean TRUE or
FALSE value, and <stmt> is just a single statement; for example "return 1;".

You only need the curly braces for a compound statement -- that tells the
interpreter that instead of just one statement, you have a block of
statements.

To continue my earlier notation,

<stmt> is either a single statement or a compound statement.


{
   return 1;
}

is technically a compound statement with only one actual statement in it.


At any rate, the entire return logic can be simplified like this:

return (mysql_num_rows($result) > 0);



Take care,

nik


  Return to Index