Jump to content

[SOLVED] error handling


ngreenwood6

Recommended Posts

I am in need of some help with error handling. I have my main page:

 

<?php
include("includes/variables.php");
?>
<html>
<head>
<link href="includes/style.css" rel="stylesheet" type="text/css" />
<title><?php echo $sitename; ?></title>
</head>

<body>

<center>

<h1>Login Here!</h1>

<form name="login_form" method="post" action="check_user.php">
<table id="login_table">
<tr>
<td>Username</td>
<td>:</td>
<td><input name="username" type="text"></td>
<td id="username_empty"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="password"></td>
<td id="password_empty"></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" name="submit_login" value="Log In"></td>
</tr>
</table>
</form>

</center>

</body>
</html>

 

Then I have the check_user.php:

<?php
//include the variables
include("includes/variables.php");

//define the variables from the form
$username = strtolower($_POST['username']);
$password = $_POST['password'];

//connect to the database
$connect = mysql_connect($host, $db_user, $db_pass) or die("could not connect");

//select the database
$select_db = mysql_select_db($db) or die("could not select database");

//query for the database
$query = "SELECT * FROM $table WHERE username='$username'";

//get the results
$results = mysql_query($query);

//number the rows
$num_rows = mysql_num_rows($results);

//put it into an array
$row = mysql_fetch_array($results);

//give the username and password from the database a variable
$row_username = $row['username'];
$row_password = $row['password'];

//if the username is blank
if($username == "")
{
echo "Please enter a username!";
}
//if the password is blank
else if($password == "")
{
echo "Please enter a password!";
}
//if the username is not in the database
else if($num_rows < 1)
{
echo "That username is not in the database!";
}
//if the password is not correct
else if ($password != $row_password)
{
echo "The password you entered does not match!";
}
else
{
echo "You are good!";
}


?>

 

At this point it checks that the username and password are not empty and that they are in the database and match the database. If everything is good it says "You are good!". When the user enters something incorrect it displays the error on a separate page. I want it to display the error on the same page without submitting the form. I know you can do it with ajax but am lost. Can someone please help?

Link to comment
https://forums.phpfreaks.com/topic/127168-solved-error-handling/
Share on other sites

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.