Help with my php login form?

My problem is when i enter a username that’s in my database it should go to another page link but when u type in a username that doesn’t exists it should stay at the login page and display the error “Username is wrong”. But all my form does is display the error how do i make it so if username is right it should go to another page?Here’s my login page code:
[php]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xh… Form <?php $conn = mysql_connect("localhost", "user", "password"); mysql_select_db("twa051", $conn) or die(mysql_error()); $sql = "SELECT * FROM school_info"; $rs = mysql_query($sql, $conn) or die ('Problem with query' . mysql_error()); if(isset($_POST['user']) && isset($_POST['password'])) { $errors = array(); $user = $_POST['user']; $pass = $_POST['password']; if(empty($user) || empty($pass)) { $errors[] = 'Enter both username and pasword'; }else{ $query = "select * from `school_info` where 'username' = '" . $user . "' "; $res = mysql_query($query) or die ("ERROR: sql = ".$query."
".mysql_error()); $row = mysql_fetch_array($res); if ($row['username'] != $user) { $errors[] = 'Wrong username.Try again!'; }else{ header("location:home.php"); } } if(!empty($errors)) { foreach ($errors as $error) { echo '',$error,''; } }else{ echo "Success"; } } ?>

Back to homepage



Username:

Password:

<input type="submit" />      <input type="reset" value="reset"/></p></td>

[/php]

The Header must be called before any HTML: http://php.net/manual/en/function.header.php

So try this, notice the exit(); as well:

[php]

<?php $conn = mysql_connect("localhost", "user", "password"); mysql_select_db("twa051", $conn) or die(mysql_error()); $sql = "SELECT * FROM school_info"; $rs = mysql_query($sql, $conn) or die ('Problem with query' . mysql_error()); if(isset($_POST['user']) && isset($_POST['password'])) { $errors = array(); $user = $_POST['user']; $pass = $_POST['password']; if(empty($user) || empty($pass)) { $errors[] = 'Enter both username and pasword'; }else{ $query = "select * from `school_info` where 'username' = '" . $user . "' "; $res = mysql_query($query) or die ("ERROR: sql = ".$query."
".mysql_error()); $row = mysql_fetch_array($res); if ($row['username'] != $user) { $errors[] = 'Wrong username.Try again!'; }else{ header("location:home.php"); exit(); // <--- This stops any more code on this page being executed } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xh… Form <?php if(!empty($errors)) { foreach ($errors as $error) { echo '',$error,''; } }else{ echo "Success"; } } ?>

Back to homepage



Username:

Password:

<input type="submit" />      <input type="reset" value="reset"/></p></td>
[/php]

Replace your query line with this code:

[php]$query = “select * from school_info where username = '” . $user . "’ ";[/php]

In where clause, you have put username in quotes, which is wrong syntax.

Sponsor our Newsletter | Privacy Policy | Terms of Service