SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru james_littler's Avatar
    Join Date
    May 2007
    Location
    Portsmouth, UK
    Posts
    775
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Login - register new user

    Hi, I'm trying to make a login with a register new user facility.

    I've got my database and login working, now I just need to get the register new user bit to work.

    here's my database structure
    Code SQL:
    -- phpMyAdmin SQL Dump
    -- version 3.1.2
    -- [url]http://www.phpmyadmin.net[/url]
    --
    -- Host: localhost:3306
    -- Generation Time: Feb 09, 2009 at 10:42 PM
    -- Server version: 5.1.31
    -- PHP Version: 5.2.8
     
    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
     
    --
    -- Database: `users`
    --
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `members`
    --
     
    CREATE TABLE IF NOT EXISTS `members` (
      `id` INT(4) NOT NULL,
      `username` VARCHAR(20) NOT NULL,
      `password` INT(32) NOT NULL,
      `type` VARCHAR(5) NOT NULL,
      `email` VARCHAR(64) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
     
    --
    -- Dumping data for table `members`
    --
     
    INSERT INTO `members` (`id`, `username`, `password`, `type`, `email`) VALUES
    (1, 'john', 1234, '', ''),
    (2, 'Admin', 1234, 'Staff', '');

    here's my registration form
    Code PHP:
    <?php if (isset($reg_error)) { 
    	echo "There was an error $reg_error, please try again.";
    } ?>
     
    <div id="register">
    	<h2>Please enter your details below</h2>
    	<form name="register"  method="post" action="registration_function.php">
    		<fieldset>
    			<legend>Register</legend>
     
    			<label for="username"><input type="text" maxlength="20" value="Username" name="username" <?php if (isset($_POST['username'])) { ?> value="<?php echo $_POST['username']; ?>" <?php } ?> />Username</label>
    			<label for="password"><input type="password" maxlength="10" value="" name="password" />Password</label>
    			<label for="confirmpassword"><input type="password" maxlength="10" name="confirmpassword" />Confirm Password</label>
    			<label for="email"><input type="text" maxlength="65" name="email" />email*</label>
     
    		</fieldset>
     
    		<div id="submit">
    			<input id="submit_btn" type="submit" name="Submit" value="Submit" />
    		</div>
     
    	</form>
    </div>

    here's registration_function.php
    Code PHP:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html charset=utf-8" />
     
    <link href="favicon.ico" rel="shortcut icon"/>
     
    <link type="text/css" rel="stylesheet" href="global.css" />
    <link type="text/javascript" rel="alternate" href="JavaScript/swfobject.js" />
     
    <title>Hampshire Inventories</title>
    </head>
     
    <body>
    	<div id="wrapper">
    			<?php
    				include("_includes/header.php");
     
    			// Include init file
    			include ("Functions/init.php");
     
                            function user_register($username, $password){
     
         			// Now encrypt the password
       				// $encrypted = md5($password);
     
         			// And lastly, store the information in the database
    	 			$sql = "INSERT INTO users.members (username, password, email) VALUES ('$username','$password','$email')";
         			//$query = "insert into users.members (username, password) values ('$username', '$encrypted')";
         			mysql_query ($sql) or die ('Could not create user.');
    			}
     
    			if (!isset($_POST['submit']))
    			{
    				 // Show the form
    				 include '_includes/registration_content.php';
    				 exit;
    			}
    			else
    			{
    				 // Check if any of the fields are missing
    				 if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['confirmpass']))
    				 {
    					  // Reshow the form with an error
    					  $reg_error = 'One or more fields missing';
    					  include '_includes/registration_content.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 '_includes/registration_content.php';
    					  exit;
    				 }
     
    				 // Everything is ok, register
    				 user_register ($_POST['username'], $_POST['password']);
     
    				 echo 'Thank you for registering on our site, <a href="index.php">click here</a> to go back.';
     
    			}
    			include("_includes/clear_footer.php");
    			?>
     
    	</div>
     
    	<div id="footer">
    		<?php
    			include("_includes/footer_content.php");
    		?>
    	</div>
    </body>
    </html>

    What's happening is if i fill in the username password and confirmpassword it takes me to registration_function.php and displays the form again.

    Can anyone see why it is doing this rather than putting the new user into the database?

    I'm pretty new to PHP so go easy on me.

    Cheers
    James

  2. #2
    SitePoint Addict
    Join Date
    Sep 2007
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You've named the form submission button as Submit, but you're checking for submit in the registration_function.php file. You're looking for wrong array element in your if test.

  3. #3
    SitePoint Guru james_littler's Avatar
    Join Date
    May 2007
    Location
    Portsmouth, UK
    Posts
    775
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well spotted! A fresh set of eyes are always useful..and knowing what you're doing of course

    I'll make that change tonight when I get home and I'll post up the results.

    Thanks again
    James

  4. #4
    SitePoint Guru james_littler's Avatar
    Join Date
    May 2007
    Location
    Portsmouth, UK
    Posts
    775
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the above, the form now works but I've got a problem with something as no matter what is entered in the fields the reg_error displays and I'm given the message

    There was an error One or more fields missing please try again.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use var_dump() to inspect the values of your variables. Make sure they contain the value you think they do.

    print_r($_POST) is also handy.

  6. #6
    SitePoint Addict
    Join Date
    Sep 2007
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change the name of the form password input control named confirmpassword to confirmpass

  7. #7
    SitePoint Guru james_littler's Avatar
    Join Date
    May 2007
    Location
    Portsmouth, UK
    Posts
    775
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks again dman,

    I have another problem now
    My user_registration function is just returning the message 'Could not create user.'

    user_registration:
    Code PHP:
    function user_register($username, $password)
    			{
     
         			// Now encrypt the password
       				       // $encrypted = md5($password);
     
         			// And lastly, store the information in the database
    	 			//$sql = "INSERT INTO users.members (username, password, email) VALUES ('$username','$password','$email')";
         			$sql = "insert into members (username, password, email) values ('$username', '$password', '$email')";
         			mysql_query ($sql) or die ('<h2>Could not create user.</h2>');
     
    			}

    And this is where I connect to the database
    init.php
    Code PHP:
    <?php
    ob_start();
    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password="admin"; // Mysql password
    $db_name="users"; // Database name
    $tbl_name="members"; // Table name
     
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    ?>

    Here's a dump of the database
    Code SQL:
    -- phpMyAdmin SQL Dump
    -- version 3.1.2
    -- [url]http://www.phpmyadmin.net[/url]
    --
    -- Host: localhost:3306
    -- Generation Time: Feb 11, 2009 at 07:53 PM
    -- Server version: 5.1.31
    -- PHP Version: 5.2.8
     
    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
     
    --
    -- Database: `users`
    --
     
    -- --------------------------------------------------------
     
    --
    -- Table structure for table `members`
    --
     
    CREATE TABLE IF NOT EXISTS `members` (
      `id` INT(4) NOT NULL AUTO_INCREMENT,
      `username` VARCHAR(20) NOT NULL,
      `password` INT(32) NOT NULL,
      `type` VARCHAR(5) NOT NULL,
      `email` VARCHAR(64) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
     
    --
    -- Dumping data for table `members`
    --
     
    INSERT INTO `members` (`id`, `username`, `password`, `type`, `email`) VALUES
    (1, 'john', 1234, '', ''),
    (2, 'Admin', 1234, 'Staff', '');

    As you can probably tell, I'm new to php so the help is very much appreciated.
    Last edited by james_littler; Feb 11, 2009 at 12:54.

  8. #8
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My best guess is that you're trying to stuff a string into the password field which is defined as an int.

    A lot of information about errors and such can be extracted with checking variable values with var_dump() and print_r(). You can also get mysql errors with mysql_error() (put it in an exit() function or something) so you don't have to post every singly debugging problem here

  9. #9
    SitePoint Guru james_littler's Avatar
    Join Date
    May 2007
    Location
    Portsmouth, UK
    Posts
    775
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thanks for that, I've changed the password to a varchar and it's now working.

    Thanks again to all.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •