Wrox Programmer Forums
|
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 6th, 2003, 10:15 AM
Registered User
 
Join Date: Aug 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Blank

Hi,

In chapter 13, in the code for register.php, what line(s) of code prevent a blank entry to be inputed into the DB whenever the page is accessed or refreshed?

Here's the code...

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

$link_id = db_connect();
mysql_select_db("sample_db");
$country_array = enum_options('usercountry', $link_id);
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, $username, $usercountry, $useremail, $userprofile, $country_array;
  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="1" 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>Full Name</TH>
      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="username"
                             VALUE="<?php echo $username ?>" SIZE="20"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Country</TH>
      <TD WIDTH="70%"><SELECT NAME="usercountry" SIZE="1">
<?php
  for($i=0; $i < count($country_array); $i++) {
    if(!isset($usercountry) && $i == 0) {
      echo "<OPTION SELECTED VALUE=\"". $country_array[$i] .
           "\">" . $country_array[$i] . "</OPTION>\n";
    }
    else if($usercountry == $country_array[$i]) {
      echo "<OPTION SELECTED VALUE=\"". $country_array[$i] . "\">" .
                                        $country_array[$i] . "</OPTION>\n";
    }
    else {
      echo "<OPTION VALUE=\"". $country_array[$i] . "\">" .
                               $country_array[$i] . "</OPTION>\n";
    }
  }
?>
      </SELECT></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>Profile</TH>
      <TD WIDTH="70%"><TEXTAREA ROWS="5" COLS="40"
                                NAME="userprofile"></TEXTAREA></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, $username, $userpassword, $userpassword2,
          $usercountry, $useremail, $userprofile;
   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($username)) error_message("Enter your full name!");
   if(empty($useremail)) error_message("Enter your email address!");
   if(empty($userprofile)) $userprofile = "No Comment.";

   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 user VALUES(NULL, '$userid',
                                     password('$userpassword'), '$username',
                                    '$usercountry', '$useremail',
                                    '$userprofile', curdate(), NULL)";
   $result = mysql_query($query);
   if(!$result) error_message(sql_error());
   $usernumber = mysql_insert_id($link_id);
   html_header();
?>
<CENTER><H3>
<?php echo $username ?>, thank you for registering with us!
</H3></CENTER>

<DIV ALIGN="CENTER"><CENTER><TABLE BORDER="1" 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 $username ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Country</TH>
    <TD WIDTH="70%"><?php echo $usercountry ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Email</TH>
    <TD WIDTH="70%"><?php echo $useremail ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Profile</TH>
    <TD WIDTH="70%"><?php echo htmlspecialchars($userprofile) ?></TD>
  </TR>
</TABLE>
</CENTER></DIV>
<?php
    html_footer();
}

switch($action) {
   case "register":
      create_account();
   break;
   default:
      html_header();
      register_form();
      html_footer();
   break;
}
?>


 
Old August 21st, 2003, 03:05 PM
Authorized User
 
Join Date: Jun 2003
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Check in Line No. 114: error_message("$userid is in use. Please choose a
different ID.");
The function error_message() is defined in "common_db.inc" which has an exit
statement.The call to exit results to program termination . When the page is
refreshed, the variable userid retains its older value only , thus the
error_message() function gets called and thus prevents from blank entry in
the database.

Jennifer D. Taylor
Wiley Technical Support





Similar Threads
Thread Thread Starter Forum Replies Last Post
Blank Final Consigliori Java GUI 0 September 7th, 2006 01:57 PM
Blank Lines DAnne XSLT 1 July 31st, 2006 01:53 PM
Blank Report BSkelding Crystal Reports 3 June 16th, 2004 04:26 AM
Blank subform Timom Access 5 May 7th, 2004 03:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.