-3

i'm getting the following error

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login.php

everything else work fine... except for this !

Here's my query :

<?php

$inputuser = $_POST["user"];
$inputpass = $_POST["pass"];

$user = "root";
$password = "";
$database = "share";

$connect = mysql_connect("localhost:3306",$user,$password);
@mysql_select_db($database) or ("Database not found");

$query = "SELECT * FROM 'users' WHERE 'username'= '$inputuser'";
$querypass = "SELECT * FROM 'users' WHERE 'password'= '$inputpass'";

$result = mysql_query($query); 
$resultpass = mysql_query($querypass); 

$row = mysql_fetch_array($result);
$rowpass = mysql_fetch_array($resultpass);

$serveruser = $row['user'];
$serverpass = $row['password'];

if ($serveruser && $serverpass) {
    if (!$result) {
       die ("Invalid Username/Password");

 }
        header('Location: Fail.php');
        mysql_close();


if ($inputpass == $serverpass) {

        header('Location: Home.php');

} else {


}
}

?>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252

1 Answers1

0

Do not use mysql_* functions. They are deprecated.

You have an error in your SQL Syntax. Change your queries to this:

SELECT * FROM `users` WHERE `username`= '$inputuser';
SELECT * FROM `users` WHERE `password`= '$inputpass';

You must use backticks, ` and not ' quotes.

And please try to combine them like this:

SELECT * FROM `users` WHERE `username`= '$inputuser' AND `password`= '$inputpass';

What if there are two users with the same password? You cannot expect all the users to use different passwords right?

Other things. You are passing the user input directly to the SQL. This is very bad and leads to SQL Injection. So you need to sanitize the inputs before you can send it to the Database server:

$inputuser = mysql_real_escape_string($_POST["user"]);
$inputpass = mysql_real_escape_string($_POST["pass"]);

Again, do not use mysql_* functions.

Update the Code

Use the following code.

// single query
$query = "SELECT * FROM `users` WHERE `username`='$inputuser' AND `password`='$inputpass'";
// your original query
$query = "SELECT * FROM `users` WHERE `username`= '$inputuser'";

Final Code

<?php
    $inputuser = mysql_real_escape_string($_POST["user"]);
    $inputpass = mysql_real_escape_string($_POST["password"]);

    $user = "root";
    $password = "";
    $database = "share";

    $connect = mysql_connect("localhost", $user, $password);
    @mysql_select_db($database) or ("Database not found");

    $query = "SELECT * FROM `users` WHERE `username`= '$inputuser' AND `password`= '$inputpass'";

    $result = mysql_query($query); 

    if (mysql_num_rows($result) == 1) {
        header('Location: Home.php');
        die();
    }
    else {
        header('Location: Fail.php');
        die ("Invalid Username/Password");
    }
?>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252