Cannot capture values entered in HTML form for MySQL

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
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Cannot capture values entered in HTML form for MySQL

Post by khushbush »

I profusely apologise if this is a repeated topic, however I have already searched this topic on the forum and tried to use the solution stated on the thread and I still cannot seem to get values recorded into MySQL.

Ok, so here's the full problem...

I can't seem to capture the values entered into the HTML registration form of a login/register system in order to create a new user record in the MySQL database.
When I run the code, the page shows a registration form, in which values can be entered. When I press the Submit button to enter the values, the next page shows a confirmation message stating that the details have been entered into the database. However, when I check the database to see if the values have been entered into the relevant table, a new row is created with the user details, but none of the details seem to have been captured. The cells in each column of the row are blank.

I have tested this code on its own as well as with the other bits of code to ensure that the security settings are not affecting the functioning of the login system, but it appears that there are no problems with the security settings or any other parts of the login system. It is just this bit of code that seems to be causing problems.

Please help me. I have tried everything I could possibly find on the internet. Nothing seems to work :cry:

This function is from a PHP file called database.php:

Code: Select all

function addNewUser(){
  $time = time();
       /* If admin sign up, give admin user level */
       if(strcasecmp($username, "admin") == 0){
         $ulevel = 9;
       }else{
          $ulevel = 1;
       }
 
  $username = $_POST['username'];
  $first = $_POST['userFirstName'];
  $last = $_POSt['userLastName'];
  $email = $_POST['userEmail'];
  $password = $_POST['userPassword'];
  $q = "INSERT INTO user (username, userFirstName, userLastName, userEmail, 
 userPassword, userLevel, timestamp) VALUES ('$username', 
 '$first','$last','$email','$password','1','1')";
       return mysql_query($q, $this->connection);
    }
 
 
The following code is from a PHP file called register.php:

Code: Select all

<?
include "session.php";
?>
 
<html>
<title>Registration Page</title>
<body>
 
<?
/**
 * The user is already logged in, not allowed to register.
 */
if($session->logged_in){
   echo "<h1>Registered</h1>";
   echo "<p>We're sorry <b>$session->username</b>, but you've already registered. "
       ."<a href=\"main.php\">Main</a>.</p>";
}
/**
 * The user has submitted the registration form and the
 * results have been processed.
 */
else if(isset($_SESSION['regsuccess'])){
   /* Registration was successful */
   if($_SESSION['regsuccess']){
      echo "<h1>Registered!</h1>";
      echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, your information has been added to the database, "
          ."you may now <a href=\"main.php\">log in</a>.</p>";
   }
   /* Registration failed */
   else{
      echo "<h1>Registration Failed</h1>";
      echo "<p>We're sorry, but an error has occurred and your registration for the username <b>".$_SESSION['reguname']."</b>, "
          ."could not be completed.<br>Please try again at a later time.</p>";
   }
   unset($_SESSION['regsuccess']);
   unset($_SESSION['reguname']);
}
/**
 * The user has not filled out the registration form yet.
 * Below is the page with the sign-up form, the names
 * of the input fields are important and should not
 * be changed.
 */
else{
?>
 
<h1>Register</h1>
<?
if($form->num_errors > 0){
   echo "<td><font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font></td>";
}
?>
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>First Name:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Last Name:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" maxlength="50" value="<? echo $form->value("email"); ?>"></td><td><? echo $form->error("email"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td colspan="2" align="right">
<input type="hidden" name="subjoin" value="1">
<input type="submit" name = "submit" value="Join!"></td></tr>
<tr><td colspan="2" align="left"><a href="main.php">Back to Main</a></td></tr>
</table>
</form>
 
<?
}
?>
 
</body>
</html>
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Cannot capture values entered in HTML form for MySQL

Post by flying_circus »

No worries! Your script is doing exactly what it's supposed to. The problem lies within your html form. You need to set the input tag's name property correctly:


Code: Select all

 
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="username" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>First Name:</td><td><input type="text" name="userFirstName" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Last Name:</td><td><input type="text" name="userLastName" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Email:</td><td><input type="text" name="userEmail" maxlength="50" value="<? echo $form->value("email"); ?>"></td><td><? echo $form->error("email"); ?></td></tr>
<tr><td>Password:</td><td><input type="userPassword" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td colspan="2" align="right">
<input type="hidden" name="subjoin" value="1">
<input type="submit" name = "submit" value="Join!"></td></tr>
<tr><td colspan="2" align="left"><a href="main.php">Back to Main</a></td></tr>
</table>
</form>
 

You see, in your database.php file, the following section tries to get the $_POST variable by the name specified in their respective <input> tag.

Code: Select all

<?php
  $username = $_POST['username'];
  $first = $_POST['userFirstName'];
  $last = $_POSt['userLastName'];
  $email = $_POST['userEmail'];
  $password = $_POST['userPassword'];
?>
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot capture values entered in HTML form for MySQL

Post by khushbush »

Thank you soooooooooooooooooooooooooooo much!! :D

It just about works...I can't seem to get it to enter the last name into the database, but I have a feeling that this may be a problem in the other files.

I also can't seem to log in, because it's coming up with the error "Username not found". Hmmmm...

Thank you again, flying_circus!
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: Cannot capture values entered in HTML form for MySQL

Post by khushbush »

Ok, I got the last name into the database...

It was a problem with $_POST, because I had typed it $_POSt, which obviously caused problems...

Now to deal with the login...hmmmm...
Post Reply