0

I am trying to create a login to database, using PHPMyAdmin with MAMP. I have made a simple login form in index.html and placed my php code in connect.php to open home.php once the correct username and password are put in. But when I input username and password (or even when I input random text) I am taken to localhost:8888/connect.php with no error message, just a blank page. Any ideas?

This is the index.html code

<head>
  <body>
    <font>
      <center><h1>Login</h1></center>
    </font>
    <form action="connect.php" method="POST">
      Username:<input type="text" name="username">
      Password:<input type="password" name="pass"><br>
      <input type="submit" value="login" name="submit">
    </form>
  </body>
</head>

And this is in connect.php

<?php

require ('msql_connect.php');
if (isset($_POST['submit'])){
    $username=mysql_escape_string($_POST['username']);
    $password=mysql_escape_string($_POST['pass']);

}
$inputuser = $_POST['user'];
$inputpass = $_POST['pass'];

$user = "root";

$database = "test";

@mysql_select_db($database) or ("Database not found");

$query = "SELECT * FROM 'login' WHERE 'user' = '$username' AND 'password' = '$password'";

$result = mysql_query($query);
$row = mysql_fetch_array($result);

    if(!$result){
        echo "Sorry, bad login";
        die("Username or password is invalid");
        //Might be better to redirect back to the login page with a freindly message
    } else {

    $valid_user = mysql_fetch_array($result); 
    mysql_close();
    header('Location: home.php');
  }
?>

UPDATE: There is no mysql_connect.php, I've changed the all mysql_ to mysqli_, and commented out the require() and it brings me to an error message of wrong username and password, but they should be working.

Now looks like this

<?php

// require ('connect.php');
if (isset($_POST['submit'])){
    $username=mysqli_escape_string($_POST['username']);
    $password=mysqli_escape_string($_POST['pass']);

}
$inputuser = $_POST['user'];
$inputpass = $_POST['pass'];

$user = "root";
$password = "";
$database = "fuf_db";
$connect = mysqli_connect("localhost",$user,$password);
@mysqli_select_db($database) or ("Database not found");

$query = "SELECT * FROM 'SuperAdminUsers' WHERE 'user' = '$username' AND 'password' = '$password'";

$result = mysqli_query($query);

    if(!$result){
        echo "Sorry, bad login";
        die("Username or password is invalid");
    } else {
        // $row = mysqli_fetch_array($result);

    $valid_user = mysqli_fetch_array($result); 
    mysqli_close();
    header('Location: home.php');
  }
?>

These are the errors I'm getting :-

Warning: mysqli_escape_string() expects exactly 2 parameters, 1 given in .../connect.php on line 5

Warning: mysqli_escape_string() expects exactly 2 parameters, 1 given in .../connect.php on line 6

Notice: Undefined index: user in .../connect.php on line 9

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO) in .../connect.php on line 15

Warning: mysqli_query() expects at least 2 parameters, 1 given in .../connect.php on line 20 Sorry, bad loginUsername or password is invalid

atak
  • 17
  • 4
  • mysql is not supporting any more, use myqli extension instead – Hujjat Nazari Feb 10 '16 at 12:40
  • 1
    *a blank page* means something is broken. Add these lines `error_reporting(E_ALL); ini_set('display_errors', 1);` at the top of your PHP scripts and see if it yields any error or not. – Rajdeep Paul Feb 10 '16 at 12:40
  • Do NOT use `mysql_*` it has been removed and will not work anymore., use `PDO` or `mysqli` instead. – Tom Feb 10 '16 at 12:42
  • show your msql_connect.php – Sanzeeb Aryal Feb 10 '16 at 12:54
  • I don't understand how this was a duplicate question to the one about quotes and backticks. These fixes have helped, I am still unable to access the database. – atak Feb 11 '16 at 10:57

1 Answers1

1

This code can be broke for many reason.

  1. You must get errors. So set this on start of file.

    ini_set('display_errors', true);

  2. Check if you have right data in your mysql config file.

  3. Maybe you have new PHP version and method mysql is not supported, so change every mysql_ method to mysqli_
TonyB
  • 423
  • 4
  • 10