Rebekah
Rebekah

Reputation: 77

why is nothing displaying when I run my php code using mamp

I have created a page with an image slide showing jQuery. I also have a search that that a user can search for a house from a database and this code works find. When I added php code in so that the user is allowed to log in and tried to run it the page comes up blank, why is this?

Here is my code

session_start();

  include "connect.php";

  if (isset($_POST['username']) and isset($_POST['password'])){

$username = $_POST['username'];

$password = $_POST['password'];

$query = ($con "SELECT * FROM login WHERE username='$username' and password= '$password'");

$result = mysqli_query($query) or die(mysqli_error());

$count = mysqli_num_rows($result);

if ($count == 1){

$_SESSION['username'] = $username;

}else {

echo "Invalid Login Credentials.";

}


if (isset($_SESSION['username'])){

$username = $_SESSION['username'];

echo "Hello " . $username . "";
echo "This is the Members Area";

echo "<a href='logout.php'>Logout</a>";

}
?>

Upvotes: 0

Views: 50

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Firstly, you're not passing your connection to your query and you have one missing brace.

The one for if (isset($_POST['username']) and isset($_POST['password'])) which should encapsulate your entire PHP.

Sidenote: Using $con as the connection variable's parameter.

<?php
session_start();

  include "connect.php";

  if (isset($_POST['username']) and isset($_POST['password'])){

$username = $_POST['username'];

$password = $_POST['password'];

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

$result = mysqli_query($con, $query) or die(mysqli_error());

$count = mysqli_num_rows($result);

if ($count == 1){

$_SESSION['username'] = $username;

}else {

echo "Invalid Login Credentials.";

}


if (isset($_SESSION['username'])){

$username = $_SESSION['username'];

echo "Hello " . $username . "";
echo "This is the Members Area";

}

} // closing brace for if (isset($_POST['username']) and isset($_POST['password']))

echo "<a href='logout.php'>Logout</a>";

?>

I noticed you may be storing passwords in plain text. If this is the case, it is highly discouraged.

I recommend you use CRYPT_BLOWFISH or PHP 5.5's password_hash() function. For PHP < 5.5 use the password_hash() compatibility pack.

Upvotes: 1

Related Questions