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
https://forums.phpfreaks.com/topic/85883-need-help-with-this-registration/
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.

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.

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.

You can obviously tell he only copied and pasted it, I'm sure he'd've read it if he really wanted to.

 

Yeah I copied it. I understand it somewhat. I'm not even using it on any websites. I'm just using it on localhost with wampserver trying to learn it.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.