Page 1 of 1

Login form - Viewing Errors!

Posted: Thu Mar 15, 2012 5:27 pm
by Shahlin
This is my code :
<body>
	<?php
	include 'config.php' ;
		
		if (isset($_POST['username'], $_POST['password'])) {
			$username = $_POST['username'] ;
			$password = md5($_POST['password']) ;
	
			$errors = array() ;
				
			if (empty ($username) && empty ($password) ) {
				$errors[] = 'Please enter your username and password'; 
				} else 
					if (empty ($username)) {
						$errors[] = 'Please enter your username';
						} else
							if (empty($password)) {
								$errors[] = 'Please enter your password' ;
			} else {
				$login_check = mysql_query("SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password'");
				$rows = mysql_num_rows ($login_check);
				if ($rows == 0) {
					$errors[] = 'Incorrect username/password' ;
				} else if ($rows == 1) {
					$_SESSION['user_id'] = $user_id;
					header('Location: profile.php');
					exit() ;
				} 
			} /* else if (!empty ($errors)) {
				foreach ($errors as $error) {
				echo $error ; */ 
                               /* I want this to be below in the div tag.
				 If I put it in the div, it won't work.
				 I need to place it where it'll work! */
			}
		}		
	?>
	<!--The whole body is wrapped in 'whole_body_wrapper'-->
	<div id="whole_body_wrapper">
	
    	
    	
    <!--Header with login form and title-->
    <div id="main_header">
    <h1>Budzzem</h1>
    <!--Login form-->
    <div id="login_form">
    <div id="phpErrors" style="color:#F00"><?php //HERE ?></div>
	<form action="" method="POST">
    <input type="text" class="login_inputs" placeholder="Username" name="username" />
    <input type="password" value="" class="login_inputs" placeholder="Password" name="password"/>
    <input type="submit" value="Sign In" class="login_inputs" id="signInBtn" />
    </form>
	</div>
    </div>
I don't know where to place the foreach loop! I want the errors to be viewed in the div tag where 'HERE' is commented!

Thank you!

Re: Login form - Viewing Errors!

Posted: Thu Mar 15, 2012 5:51 pm
by Temor
<div id="phpErrors" style="color:#F00"><?php if(!empty($errors)){
 foreach($errors as $error){
 echo $error;
}
}?></div>
I don't see why that wouldn't work.

Re: Login form - Viewing Errors!

Posted: Thu Mar 15, 2012 6:17 pm
by Shahlin
Temor wrote:
<div id="phpErrors" style="color:#F00"><?php if(!empty($errors)){
 foreach($errors as $error){
 echo $error;
}
}?></div>
I don't see why that wouldn't work.
Thanks! =)

Re: Login form - Viewing Errors!

Posted: Thu Mar 15, 2012 6:44 pm
by Shahlin
One more thing!
if (empty ($username) || empty ($password)) {
				$errors[] = 'Please enter your username and password'; 
			} else {
				$login_check = mysql_query("SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password'");
				$rows = mysql_num_rows ($login_check);
				if ($rows == 0) {
					$errors[] = 'Incorrect username/password' ;
				} else if ($rows == 1) {
					$_SESSION['user_id'] = $user_id;
					header('Location: profile.php?id='.$user_id.'');
					exit() ;
				} 
			} 
When I login, I get this error : Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 7 in C:\xampp\htdocs\Budzzem\func.php on line 6

And the URL is profile.php?id=

Re: Login form - Viewing Errors!

Posted: Thu Mar 15, 2012 8:17 pm
by Temor
Well, the error does not make much sense to me to be honest.
I can't see the mysql_result function being used anywhere so I can't see how it can fail :S

$user_id is not set anywhere. What you need to do is store the ID you took from the database and then use that to put in the $_SESSION['user_id'] variable.

Re: Login form - Viewing Errors!

Posted: Fri Mar 16, 2012 12:44 pm
by Shahlin
I'll try to fix it. But what about this :
if ($rows == 1) {
$_SESSION['user_id'] = $user_id;
header('Location: profile.php?id='.$_SESSION['user_id'].'');
exit() ;
} 
When I do this, It takes me to this page : profile?id=

Any problem with sessions?

Re: Login form - Viewing Errors!

Posted: Fri Mar 16, 2012 1:54 pm
by Temor
where are you giving $user_id a value? if you're not giving it a value, it will be blank, which means you're making $_SESSION['user_id'] an empty variable.

you need to fetch the users ID and then store it in the $user_id variable.

Re: Login form - Viewing Errors!

Posted: Fri Mar 16, 2012 7:17 pm
by Shahlin
This is my complete code :
<?php
	include 'init.php' ;
		session_start() ;
		if (isset($_POST['username'], $_POST['password'])) {
			$username = $_POST['username'] ;
			$password = md5($_POST['password']) ;
	
			$errors = array() ;
				
			if (empty ($username) || empty ($password)) {
				$errors[] = 'Please enter your username and password'; 
			} else {
				$login_check = mysql_query("SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password'");
				$rows = mysql_num_rows ($login_check);
				if ($rows == 0) {
					$errors[] = 'Incorrect username/password' ;
				} else if ($rows == 1) {
					$_SESSION['user_id'] = $user_id;
					header('Location: profile.php?id='.$_SESSION['user_id'].'');
					exit() ;
				} 
			} 
		}		
	?>
What more should I add?

Re: Login form - Viewing Errors!

Posted: Fri Mar 16, 2012 11:40 pm
by Temor
Line 17 and 18:
} else if ($rows == 1) {
                                        $_SESSION['user_id'] = $user_id;
you're giving $_SESSION['user_id'] the value that is inside $user_id;
$user_id is not given a value, which makes it empty, which makes the code above useless. You're just making sure that $_SESSION['user_id'] is empty.

On line 13, you're running a query to get the user ID from the database, but you never assign the ID to a variable.

What you need to do is run
mysql_result($login_check,0);
and store that result in the $user_id variable.

Re: Login form - Viewing Errors!

Posted: Sat Mar 17, 2012 10:04 am
by Shahlin
Thanks! It works now!