Jump to content

Need help with this registration


phpnoobie9

Recommended Posts

I was looking over this tutorial and can't seem to get it to work.

http://www.dot-silver.co.uk/tutorials/view/14/Simple+Member+System/

 

I'm not able to add the username and password to the database. I did every step and still can't get it to work.

 

<?php

require "db.php";





if(!$_POST['register']){

print'<form method="post" action="'.$_SERVER['PHP_SELF'].'">

<label>Username</label><br/><input name="username"/><br/>

<label>Password</label><br/><input name="password" type="password"/><br/>

<input type="submit" name="register" value="register"/>

</form>';



}else{



$lock = 'true';

if($lock == 'true') {

print 'Copy and pasting gets you nowhere in life.';

die;



}else{



$name = mysql_real_escape_string($_POST['username']);

$pw = sha1($_POST['password']);



mysql_query("INSERT INTO members (id, username, password) VALUES (NULL, '$user', '$pw')") or die('For some reason, we could not process your registration.');

print'Registration Successful. Head over to <a href="login.php">the login page</a>.';

}

}

?>

Link to comment
Share on other sites

<?php

if(!$_POST['submit']){
// form
}else {
$user = mysql_real_escape_string($_POST['username']);
$pass = mysql_real_escape_string($_POST['password']);
$errors = array();

if(!$user){
	$errors[] = "You did not supply a username";
}else {

	if(!in_array(strlen($user),range('3','32'))){
		$errors[] = "Username must be between 3 and 32 characters";
	}

	if(!ctype_alnum($user)){
		$errors[] = "Username can only contain alphanumerical characters";
	}

	$sql = "SELECT * FROM `users` WHERE `username`='".$user."'";
	$res = mysql_query($sql) or die(mysql_error());
	if(mysql_num_rows($res) == 0){
		$errors[] = "The username already exists in the database";
	}
}

if(!$pass){
	$errors[] = "You did not supply a password";
}else {
	if(strlen($pass) < 6){
		$errors[] = "Your password must be greater than or equal to 6";
	}
}

if(count($errors) > 0){
	echo "The following errors occurred:<br>\n";
	foreach($errors AS $error){
		echo $error . "<br>\n";
	}
}else {
	$sql2 = "INSERT INTO `users` (`username`,`password`) VALUES('".$user."','".sha1($pass)."')";
	$res2 = mysql_query($sql2) or die(mysql_error());
	echo "You have successfully registered as ".$user."!\n";
}
}

?>

 

give that a go.

Link to comment
Share on other sites

You have this

 

if(!$_POST['register']){

 

and then a else statement that sets $lock to true and then the next line compares $lock to true, so of course it will echo your Copy and Paste line.

 

Why is that even there?  Either they are clicking Register or they are not.

 

You dont need two else statements like that and I dont see why any tutorial would do that unless you missed something.

Link to comment
Share on other sites

Doesn't make sense.  So it's say YOU shouldn't Copy and Paste their code?

 

It is in there so they dont copy and paste, they read the actual tutorial and are trying to learn PHP, not just copying off of everybody else.

Link to comment
Share on other sites

The author said nothing about the $lock variable in the tutorial. The only thing I missed was the last line where he said change it to false. I was wondering why that else statement was there. The password is registering correctly, but the username is blank when I look at the database.

 

Try using my example. That tutorial is horrible and shouldn't be used.

 

 

Nice, you can listen to a 26 minute tutorial, or change 1 word in you current file.  I would choose 1 word.

Your video is blurry.

Link to comment
Share on other sites

mysql_query("INSERT INTO members (id, username, password) VALUES (NULL, '$user', '$pw')") or die('For some reason, we could not process your registration.');

 

This should be your query line

 

mysql_query("INSERT INTO members (id, username, password) VALUES (NULL, '$name', '$pw')") or die('For some reason, we could not process your registration.');

 

You are setting the username to $name, not $user

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.