Login form - Viewing Errors!

Ask about a PHP problem here.
Post Reply
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Login form - Viewing Errors!

Post by Shahlin »

This is my code :

[syntax=php]<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>[/syntax]

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!
Just A PHP Beginner!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Login form - Viewing Errors!

Post by Temor »

[syntax=php]<div id="phpErrors" style="color:#F00"><?php if(!empty($errors)){
foreach($errors as $error){
echo $error;
}
}?></div>
[/syntax]
I don't see why that wouldn't work.
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Login form - Viewing Errors!

Post by Shahlin »

Temor wrote:[syntax=php]<div id="phpErrors" style="color:#F00"><?php if(!empty($errors)){
foreach($errors as $error){
echo $error;
}
}?></div>
[/syntax]
I don't see why that wouldn't work.


Thanks! =)
Just A PHP Beginner!
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Login form - Viewing Errors!

Post by Shahlin »

One more thing!

[syntax=php]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() ;
}
} [/syntax]
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=
Just A PHP Beginner!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Login form - Viewing Errors!

Post 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.
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Login form - Viewing Errors!

Post by Shahlin »

I'll try to fix it. But what about this :

[syntax=php]
if ($rows == 1) {
$_SESSION['user_id'] = $user_id;
header('Location: profile.php?id='.$_SESSION['user_id'].'');
exit() ;
} [/syntax]

When I do this, It takes me to this page : profile?id=

Any problem with sessions?
Just A PHP Beginner!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Login form - Viewing Errors!

Post 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.
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Login form - Viewing Errors!

Post by Shahlin »

This is my complete code :

[syntax=php]<?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() ;
}
}
}
?>[/syntax]

What more should I add?
Just A PHP Beginner!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Login form - Viewing Errors!

Post by Temor »

Line 17 and 18:
[syntax=php]} else if ($rows == 1) {
$_SESSION['user_id'] = $user_id;[/syntax]
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
[syntax=php]mysql_result($login_check,0);[/syntax]
and store that result in the $user_id variable.
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Login form - Viewing Errors!

Post by Shahlin »

Thanks! It works now!
Just A PHP Beginner!
Post Reply