Rus Paul Adrian Rus Paul Adrian - 5 months ago 12
PHP Question

Strange php error at login page

I am currently working on a log-in page which accesses a mysql database with a user and password table. Whenever I try to run my code I get the following error no matter what I comment:


Parse error: syntax error, unexpected '=', expecting ',' or ';' in /home/scs/licenta/an2/gr927/rpie1814/public_html/testphpathome/login.php on line 15


Here is the main page in HTML:

<!DOCTYPE html>
<html>
<head>
<title>
Gaming site
</title>

</head>
<body>

<h1>
Log-In
</h1>

<form action="login.php" method="post">
Username: <br>
<input type="text" name="user"> <br>
Password: <br>
<input type="text" name="pass"><br>

<input type="submit" value="Submit">
</form>

</body>
</html>


And here is the php code:

<?php

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


global $conn = mysqli_connect("localhost", "rpie1814", "rpie1814", "rpie1814");

if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM Users";
global $result = mysqli_query($conn, $sql);

if(mysqli_num_rows($result) > 1)
{
header("Location: /game.php");
exit;
}
else
{
echo "Unknown user";
}

mysqli_close($conn);
exit;

?>

Answer

You have to remove global from connection ($conn) and result ($result):-

$conn = mysqli_connect("localhost", "rpie1814", "rpie1814", "rpie1814");

AND

Replace:

$result = mysqli_query($connect, $sql);

To:-

$result = mysqli_query($conn, $sql); // actually your connection variable is $conn not $connect