4 Replies - 513 Views - Last Post: 02 November 2013 - 11:30 AM Rate Topic: -----

#1 dangmnx  Icon User is offline

  • D.I.C Regular

Reputation: -1
  • View blog
  • Posts: 428
  • Joined: 10-April 09

Simple Register / Login help

Posted 31 October 2013 - 01:09 AM

I'm a newbie at this, so please bare with me.

Here is my login_activation code, can someone please correct me on why it isn't working?

<?php

if(!empty($_POST['username']) || !empty($_POST['password'])){
  
  $username = $_POST['username'];
  $password = $_POST['password'];
  $check = mysql_query ("SELECT * FROM users WHERE username = '".$username."' ");
  
  if(mysql_num_rows($check) == 1){
    
    echo "Sorry, That username has already been taken!";
  }
  
  else{
   $register = mysql_query("INSERT INTO users (Username, Password) VALUES ('".$username."', '".$password."')");
   if($register){
     
     echo "Congrats! Your account has been made!";

   }
   else{
    echo "Something was wrong, Please go back and check."; 
   }
  }

}

?>




register.php
<form action="active.php" method="post">
<input type="text" name="name"><br />
<input type="password" name="password"><br />
<input type="submit" value="submit">
</form>

This post has been edited by dangmnx: 31 October 2013 - 01:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Simple Register / Login help

#2 Hubbie  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 24-October 13

Re: Simple Register / Login help

Posted 31 October 2013 - 04:01 AM

you are reffering to a variable, $username that does not exist. your form has a variable called name, so its either you change the form here

<input type="text" name="name"><br />

to
<input type="text" name="username"><br />

or you call the variable $_POST['name'] instead of $_POST['username']
because as far as your code is concerned, $_POST['username'] does not exist
Was This Post Helpful? 0
  • +
  • -

#3 dangmnx  Icon User is offline

  • D.I.C Regular

Reputation: -1
  • View blog
  • Posts: 428
  • Joined: 10-April 09

Re: Simple Register / Login help

Posted 31 October 2013 - 05:13 PM

Well I got it working now, except when i re-register an username that is already register, it doesn't give me an error ?

<?php

if(!empty($errors)){ //Errors found?
	echo '<h2>Uh Oh!</h2>';
	
	//For every error as individual
	foreach($errors as $error){
		echo $error.'<br />'; //Show error
	}
}

//Check if fields are submitted
if(isset($_POST['username']) && isset($_POST['password'])){
	//Define empty variable as array
	$errors = array();

	//Error checking
	if(empty($_POST['username'])){ //Empty username?
		$errors[] = 'Please insert username.'; //Add to list of errors
	}
	if(empty($_POST['password'])){ //Empty password?
		$errors[] = 'Please insert password.'; //Add to list of errors
	}
	
	//Currently no errors found?
	if(empty($errors)){
		//Sanitizing: security
		$username = mysql_real_escape_string($_POST['username']);
		$password = mysql_real_escape_string($_POST['password']);
		
		$check = mysql_query ("SELECT * FROM users WHERE username = '".$username."' ");
		
		if(mysql_num_rows($check) > 0){
			$errors[] = 'Sorry, That username has already been taken!'; //Add to list of errors
		} else if(empty($errors)){ //No errors found?
			$query = "INSERT INTO users (Username,
										 Password
					  ) VALUES ('".$username."', 
								'".$password."'
					  )
			";
			
			if(mysql_query($query)){
				echo 'Your account has been registered!';
			} else {
				echo 'Something was wrong, Please go back and check.'; 
			}
		}
	}
}
?>


This post has been edited by dangmnx: 31 October 2013 - 05:14 PM

Was This Post Helpful? 0
  • +
  • -

#4 Hubbie  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 24-October 13

Re: Simple Register / Login help

Posted 02 November 2013 - 04:50 AM

is the user being registered twice or its overwriting the existing user?
Was This Post Helpful? 0
  • +
  • -

#5 Peter O  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 199
  • Joined: 19-October 13

Re: Simple Register / Login help

Posted 02 November 2013 - 11:30 AM

You are printing the errors before you have checked if there are any errors. You should probably do it the other way around.

When you use double quoted strings you can put the PHP variables directly inside the string.
$check = mysql_query ("SELECT * FROM users WHERE username = '".$username."' ");
could be written as
$check = mysql_query ("SELECT * FROM users WHERE username = '$username' ");

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1