auto incrementing uniq Id field preventing form to post

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
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

auto incrementing uniq Id field preventing form to post

Post by edawson003 »

auto incrementing unique Id field preventing login form to post. Code worked fine before I added the auto increment field in the MySQL backend. It seems like the insert query is being blocked because the number of rows dont match. Any thoughts on how to fix? I'll bet the solution is simple :wink:

Fields in SQL backend:
UserName
Password
CreateDt
AcctID --- new field with auto increment

my code:

Code: Select all

<?
session_start(); 
include("database.php");
 
/**
 * Returns true if the username has been taken
 * by another user, false otherwise.
 */
function usernameTaken($UserName){
   global $conn;
   if(!get_magic_quotes_gpc()){
      $UserName = addslashes($UserName);
   }
   $q = "select UserName from TestUsers where UserName = '$UserName'";
   $result = mysql_query($q,$conn);
   return (mysql_numrows($result) > 0);
}
 
/**
 * Inserts the given (username, password) pair
 * into the database. Returns true on success,
 * false otherwise.
 */
function addNewUser($UserName, $Password, $CreateDt){
   global $conn;
   $q = "INSERT INTO TestUsers VALUES ('$UserName', '$Password', '$CreateDt')";
   return mysql_query($q,$conn);
}
 
/**
 * Displays the appropriate message to the user
 * after the registration attempt. It displays a 
 * success or failure status depending on a
 * session variable set during registration.
 */
function displayStatus(){
   $uname = $_SESSION['reguname'];
   if($_SESSION['regresult']){
?>
 
<h1>Registered!</h1>
<p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>
 
<?
   }
   else{
?>
 
<h1>Registration Failed</h1>
<p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>, could not be completed.<br>
Please try again at a later time.</p>
 
<?
   }
   unset($_SESSION['reguname']);
   unset($_SESSION['registered']);
   unset($_SESSION['regresult']);
}
 
if(isset($_SESSION['registered'])){
/**
 * This is the page that will be displayed after the
 * registration has been attempted.
 */
?>
 
<html>
<title>Registration Page</title>
<body>
 
<? displayStatus(); ?>
 
</body>
</html>
 
<?
   return;
}
 
/**
 * Determines whether or not to show to sign-up form
 * based on whether the form has been submitted, if it
 * has, check the database for consistency and create
 * the new account.
 */
if(isset($_POST['subjoin'])){
   /* Make sure all fields were entered */
   if(!$_POST['user'] || !$_POST['pass']){
      die('You didn\'t fill in a required field.');
   }
 
   /* Spruce up username, check length */
   $_POST['user'] = trim($_POST['user']);
   if(strlen($_POST['user']) > 30){
      die("Sorry, the username is longer than 30 characters, please shorten it.");
   }
 
   /* Check if username is already in use */
   if(usernameTaken($_POST['user'])){
      $use = $_POST['user'];
      die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
   }
 
   /* Add the new account to the database */
   $md5pass = md5($_POST['pass']);
   $date = date("Y-m-d");
   $_SESSION['reguname'] = $_POST['user'];
   $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass, $date);
   $_SESSION['registered'] = true;
   echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
   return;
}
else{
/**
 * This is the page with the sign-up form, the names
 * of the input fields are important and should not
 * be changed.
 */
?>
 
<html>
<title>Registration Page</title>
<? print(Date("m/d/Y")); ?> 
<body>
<h1>Register</h1>
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Email:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr>
</table>
</form>
</body>
</html>
 
 
<?
}
?>
Last edited by edawson003 on Mon Sep 28, 2009 12:20 am, edited 3 times in total.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: auto incrementing uniq Id field preventin login form to post

Post by edawson003 »

When I run this SQL query directly, it works just fine:

INSERT INTO `TestUsers` ( `username` , `password` , `createdt` ) VALUES ( 'john', 'john@sitename.com', 2009-8-22 )

The auto increment field acctid automatically updates with the inserted record.

When I run the code above I get:
Registration Failed
We're sorry, but an error has occurred and your registration for the username randomusernamex, could not be completed.
Please try again at a later time.
--- Error message that is programmed in. Basically the table is not appended. Any takers?
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: auto incrementing uniq Id field preventin login form to post

Post by Cirdan »

It's not your MySQL code that is causing the problem then, it's your if statement. What is the value of $_SESSION['regresult']? Echo it before the if statement.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: auto incrementing uniq Id field preventin login form to post

Post by Benjamin »

:arrow: Moved to PHP - Code
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: auto incrementing uniq Id field preventin login form to post

Post by edawson003 »

I figured out what was blocking the data post. I had to modify the bit of the code to call out every data field in the SQL table (even the ones that allow NULLS....weird). It worked, but is that the right way to proceed?

One piece code I had to modify:

Code: Select all

 
   /* Add the new account to the database */
   $acctid = '';
   $md5pass = md5($_POST['pass']);
   $date = date("Y-m-d");
   $enddt = '';
   $status = '00';
   $_SESSION['reguname'] = $_POST['user'];
   $_SESSION['regresult'] = addNewUser($acctid, $_POST['user'], $md5pass, $date, $enddt, $status);
   $_SESSION['registered'] = true;
   echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
   return;
}
 
and the other piece:

Code: Select all

 
function addNewUser($acctid, $username, $password, $createdt, $enddt, $status){
   global $conn;
   $q = "INSERT INTO Users VALUES ('$acctid', '$username', '$password', '$createdt', '$enddt', '$status')";
   return mysql_query($q,$conn);
}
 
Post Reply