Forum Moderators: coopster
I have this login page, it works and gives a welcome to the user, how ever, it gives this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
this is the code:
<html>
<head>
<title>Login</title>
<head>
<body>
<?PHP
//Connect to database
mysql_connect("localhost", "admin", "admin") or die(mysql_error());
mysql_select_db("ueros_db") or die(mysql_error());
session_start();
$username = $_POST['username'];
$password = md5($_POST['password']);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysql_query($query);
if (mysql_num_rows($result) != 1) {
$error = "Bad Login";
include "login.html";
} else {
$_SESSION['username'] = "$username";
print("<h3>Welcome $username!</h3>");
include "search.php";
}
?>
</body>
</html>
can anyone tell me whats the problem here?
1) You should only check the database if someone's POSTed a username and password:
if ($_POST[username]&&$_POST[password]) $result=mysql_query($query);
2) You must absolutely in all cases and without exception for any reason process your username with mysql_real_escape_string() before sending it to MySQL
Once you fix #1, but before you fix #2, try logging in with these details:
username=<script>top.location=\'http://www.webmasterworld.com\'</script>' OR 1 OR '1
password=anything
for the function, should it be like this?
$username = mysql_real_escape_string($_POST['username']);
I still have the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
<html>
<head>
<title>Login</title>
<head>
<body>
<?PHP
//Connect to database
mysql_connect("localhost", "admin", "admin") or die(mysql_error());
mysql_select_db("ueros_db") or die(mysql_error());
session_start();
$username = mysql_real_escape_string($_POST['username']);
$password = md5($_POST['password']);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
if ($_POST[username]&&$_POST[password]) $result=mysql_query($query);
if (mysql_num_rows($result) != 1) {
$error = "Bad Login";
include "login.html";
} else {
$_SESSION['username'] = "$username";
print("<h3>Welcome $username!</h3>");
include "search.php";
}
?>
</body>
</html>