Hi. I have a register/login script (Very Simple one). The one I had was just a simple username, password, and confirm password type. Well, I inserted Name, Address, City, ect. into it. It all works expect I can't see any of the options I added in the database after they fill the form out. I can see where they are suppose to go.

Here are the codes:

functions.php

<?php

// Salt Generator
function generate_salt ()
{
     // Declare $salt
     $salt = '';

 // And create it with random chars
 for ($i = 0; $i < 3; $i++)
 {
      $salt .= chr(rand(35, 126));
 }
      return $salt;
}

function user_register($username, $password)
{
     // Get a salt using our function
     $salt = generate_salt();

 // Now encrypt the password using that salt
 $encrypted = md5(md5($password).$salt);

 // And lastly, store the information in the database
 $query = "insert into user (username, password, salt) values ('$username', '$encrypted', '$salt')";
 mysql_query ($query) or die ('Could not create user.');
}

function user_login($username, $password)
{
     // Try and get the salt from the database using the username
     $query = "select salt from user where username='$username' limit 1";
     $result = mysql_query($query);
     $user = mysql_fetch_array($result);

 // Using the salt, encrypt the given password to see if it
 // matches the one in the database
 $encrypted_pass = md5(md5($password).$user['salt']);

 // Try and get the user using the username & encrypted pass
 $query = "select userid, username from user where username='$username' and password='$encrypted_pass'";
 $result = mysql_query($query);
 $user = mysql_fetch_array($result);
 $numrows = mysql_num_rows($result);

 // Now encrypt the data to be stored in the session
 $encrypted_id = md5($user['userid']);
 $encrypted_name = md5($user['username']);

 // Store the data in the session
 $_SESSION['userid'] = $userid;
 $_SESSION['username'] = $username;
 $_SESSION['encrypted_id'] = $encrypted_id;
 $_SESSION['encrypted_name'] = $encrypted_name;

if ($numrows == 1)
{
    return 'Correct';
}
else
{
    return false;
}
}

function user_logout()
{
     // End the session and unset all vars
     session_unset ();
     session_destroy ();
}

function is_authed()
{
     // Check if the encrypted username is the same
     // as the unencrypted one, if it is, it hasn't been changed
     if (isset($_SESSION['username']) && (md5($_SESSION['username']) == $_SESSION['encrypted_name']))
     {
          return true;
     }
     else
     {
          return false;
     }
}

?>

register.php

<?php

// Include init file
include 'init.php';

if (!isset($_POST['submit']))
{
     // Show the form
     include 'register_form.inc.php';
     exit;
}
else
{
     // Check if any of the fields are missing
     if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['confirmpass']) || empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['address']) || empty($_POST['city']) || empty($_POST['state']) || empty($_POST['country']))
     {
          // Reshow the form with an error
          $reg_error = 'One or more fields missing';
          include 'register_form.inc.php';
          exit;
     }

 // Check if the passwords match
 if ($_POST['password'] != $_POST['confirmpass'])
 {
      // Reshow the form with an error
      $reg_error = 'Your passwords do not match';
      include 'register_form.inc.php';
      exit;
 }

 // Everything is ok, register
  user_register ($_POST['username'], $_POST['password'], $_POST['firstname'], $_POST['lastname'], $_POST['address'], $_POST['city'], $_POST['state'], $_POST['country']);

 echo 'Thank you for registering on our site, <a href="index.php">click here</a> to go back.';

}

?>

I have 3-4 more pages of coding, but those pages are mostly login and stuff.

Any help is always appreciated. THANKS! 🙂

    Your user_register() function wasn't changed to handle the extra arguments. 😉

      Thanks for the reply! 🙂

      How can I change it so it will work?

      Thanks 🙂

        function user_register($username, $password, $firstname, $lastname, $address, $city, $state, $country)
        {
        //skipping lines
        .
        . 
        . 
        // And lastly, store the information in the database
             $query = "insert into user (username, password, salt, firstname, lastname, address, city, state, country) values ('$username', '$encrypted', '$salt','$firstname','$lastname','$address','$city','$state','$country')";
        }
        

        I'm assuming these are the names of the fields in your table, but you should get the idea.

          I see what your saying, I don't know why I didn't think of that. Although I edited that portion of the script and it wouldn't make a user in the database. So I inserted the:

          mysql_query ($query) or die ('Could not create user.');

          Back into the script and tryed again, and it said "Could not create user.".

          This is so confusing. Sorry for the trouble and thanks for helping! 🙂

            change the query line to

            mysql_query ($query) or die ('Could not create user.<br />'.mysql_error());

            Also, make sure my fields in the INSERT statement match the field names in your database.

              YOU ROCK!!

              It works! 🙂

              I can't thank you enough. THANKS! 🙂

                Write a Reply...