register prob

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
S A N T A
Forum Newbie
Posts: 10
Joined: Sat May 10, 2008 4:43 pm

register prob

Post by S A N T A »

ok so i have this code i wrote that is suppose to allow someone to register but if the username is taken it denies them same with incorrect password repeat

BUT the problem is that (I fixed an error with it not displaying) when i try and register it reloads the page and displays

Code: Select all

Column count doesn't match value count at row 1
o it gives me a 404 message

here is the code:

Code: Select all

<?php
 
session_start();
 
require("config.php");
 
$db = mysql_connect($dbhost, $dbuser);
mysql_select_db($dbdatabase, $db);
 
if($_POST['submit']) {
   if($_POST['password'] == $_POST['password2']) {
      $checksql = "SELECT * FROM user WHERE username = '" . $_POST['username'] . "';";
      $checkresult = mysql_query($checksql)or die(mysql_error());
      $checknumrows = mysql_num_rows($checkresult);
 
if($checknumrows == 1) {
   header("Location: " . $config_basedir . "reg.php?error=taken");
   }
   
else {
   
      for($i = 0; $i < 16; $i++) {
         $randomstring .= chr(mt_rand(32,126));
         }
         
         
         $verifystring = urlencode($randomstring);
         $validusername = $_POST['username'];
         
         $sql = "INSERT INTO user(username, password, name, lastname) VALUES('" . $_POST['username'] . "', '" . $_POST['password'] . "' '" . $_POST['name'] . "' '" . $_POST['lastname'] . "' '" . addslashes($randomstring) . "', 0);";
         $query = mysql_query($sql)or die(mysql_error());
         require("header.php");
         }
         if(!$query) {
            header("Location: " . $config_basedir . "reg.php?error=pass");
         }
         if(!empty($_GET['error'])){
         
            require("header.php");
            
            switch($_GET['error']) {
            case "pass":
               echo "Passwords do not match!";
               break;
            case "taken":
               echo "Username taken, please use another.";
            break;
               case "no":
                  echo "Incorect login details!";
               break;
               
            }
        
        }
        }
        }
        
        
         
?>
<h2>Register</h2>
To register on <?php echo $config_blogname; ?>, filll out the form below.
<form action="<?php echo $SCRIPT_NAME ?>" method="POST">
<table>
<tr>
   <td>Username</td>
   <td><input type="text" name="username"></td>
</tr>
<tr>
   <td>Password</td>
   <td><input type="password" name="password"></td>
</tr>
<tr>
   <td>Re-type Password</td>
   <td><input type="password" name="password2"></td>
</tr>
<tr>
   <td>First Name</td>
   <td><input type="text" name="name"></td>
</tr>
<tr>
   <td>Last Name</td>
   <td><input type="text" name="lastname"></td>
</tr>
<tr>
   <td></td>
   <td><input type="submit" name="submit" value="Register!"></td>
</tr>
</table>
</form>
<?php
 
 
require("footer.php");
 
?>
 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: register prob

Post by aceconcepts »

You have the answer in the error. You have different amount of column names compared to VALUES() in your sql query.

Here is your problem:

Code: Select all

 
user(username, password, name, lastname) 
VALUES('" . $_POST['username'] . "', '" . $_POST['password'] . "' '" . $_POST['name'] . "' '" . $_POST['lastname'] . "' '" . addslashes($randomstring) . "', 0)
 
Notice the difference in column counts?
S A N T A
Forum Newbie
Posts: 10
Joined: Sat May 10, 2008 4:43 pm

Re: register prob

Post by S A N T A »

Thanks fixed that error now i get

Code: Select all

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2
EDIT: ok i fixed it for some reason there were "#" signs throughout the code now i get the same error
JacobT
Forum Commoner
Posts: 43
Joined: Tue May 13, 2008 11:07 am
Location: Los Angeles, CA

Re: register prob

Post by JacobT »

You're missing commas in your VALUES statement,

Code: Select all

VALUES('" . $_POST['username'] . "', '" . $_POST['password'] . "' '" . $_POST['name'] . "' '" . $_POST['lastname'] . "' '" . addslashes($randomstring) . "', 0)
should be

Code: Select all

VALUES('" . $_POST['username'] . "', '" . $_POST['password'] . "', '" . $_POST['name'] . "', '" . $_POST['lastname'] . "', '" . addslashes($randomstring) . "', 0)
S A N T A
Forum Newbie
Posts: 10
Joined: Sat May 10, 2008 4:43 pm

Re: register prob

Post by S A N T A »

i get the same error
User avatar
lafever
Forum Commoner
Posts: 99
Joined: Sat Apr 05, 2008 2:03 pm
Location: Taylor, MI

Re: register prob

Post by lafever »

SANTA, It seems you're trying to insert 6 columns of data but you only declared 4 columns, which is what the first responder said. Let's explain this in your query:

Code: Select all

 
INSERT INTO user (username, password, name, lastname)
VALUES('" . $_POST['username'] . "', '" . $_POST['password'] . "', '" . $_POST['name'] . "', '" . $_POST['lastname'] . "', '" . addslashes($randomstring) . "', 0)
 
Breaking it down, you're telling the query to INSERT into `user` columns username, password, name, lastname.

When you enter your values you're entering username, password, name, lastname, randomstring, 0

You'll need to define the other two columns your trying to insert into in the (I used COLUMN NAME)
user (username, password, name, lastname, COLUMN NAME, COLUMN NAME)


Get what's happening now?
S A N T A
Forum Newbie
Posts: 10
Joined: Sat May 10, 2008 4:43 pm

Re: register prob

Post by S A N T A »

Woot! thanks man u rock!
Post Reply