1
<?php
//$game_id = ['gameid'];
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$address = $_POST['address'];
$postal_code = $_POST['postalcode'];
$start_date = $_POST['startdate'];
$rental_days = $_POST['rentaldays'];
$borrow_game = $_POST['borrowgame'];

Variables are set here^!

$end_date = date('Y-m-d', strtotime($start_date.' + '.$rental_days.'days'));

How the end_date function works^!

$conn = mysqli_connect("localhost", "evansbwg_user", "1password1", "evansbwg_database");

if ($conn->connect_error)
{
    die("Connection failed: " . $conn->connect_error);
}

Connection^!

$result = mysqli_query($conn, "SELECT * FROM tbl_games WHERE game_name = '$borrow_game'");

//$result_present = mysqli_query($conn, $result);

$num_rows = mysqli_num_rows($result);

if ($num_rows == 1)

{
    $row2 = mysqli_fetch_array($result);
    $id = $row2['game_id'];

Getting game_id to equal borrow_game^!

    $sql_search = "SELECT * FROM tbl_rental WHERE game_id = '$id' 
    AND (start_date <= '$start_date' AND end_date >= '$end_date')
    OR (start_date >= '$start_date' AND start_date <= '$end_date')
    OR (end_date >= '$start_date' AND end_date <= '$end_date')
    OR (start_date >= '$start_date' AND end_date <= '$end_date')";

    $result1 = mysqli_query($conn, $sql_search);

    $num = mysqli_num_rows($result1);

    if ($num == 0) 
    {
        $sql = "INSERT INTO tbl_rental (game_id, first_name, last_name, address, postal_code, start_date, rental_days, borrow_game, end_date)
       VALUE ('$id', '$first_name', '$last_name', '$address', '$postal_code', '$start_date', '$rental_days', '$borrow_game', '$end_date')";

Inserting the data into the table^!

    if (mysqli_query($conn, $sql))
    {
        echo "New record created successfully";
    }
    else
    {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
    }

}
while ($row = mysqli_fetch_array($result)) 
{
    $id = $row['game_id'];
}
mysqli_close($conn);
?>

I have fixed my main problem however if any changes need to be made please suggest.

2 Answers2

2

You closed your connection prematurely, so the query following that has no connection to the query.

mysqli_close($conn);
^^^^^^^^^^^^^^^^^^^^
}
while ($row = mysqli_fetch_array($result)) 
{
$id = $row['game_id'];
}

place it at the end of your script or just remove it.

Also this line makes no sense $end_date = ['enddate'];

You probably meant to do:

$end_date = $_POST['enddate'];

since the others above that are using POST arrays.

However, seeing

$end_date = date('Y-m-d', strtotime($start_date.' + '.$rental_days.'days'));

You would need to remove $end_date = ['enddate'];, that's invalid syntax.


Sidenote:

Your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements, they're much safer.


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Also, add or die(mysqli_error($conn)) to mysqli_query().

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

What happen if

$num = mysqli_num_rows($result1);

Return than more than one zero row

if ($num == 0) 

You code not enter into if condition and your $sql variable is empty

if (mysqli_query($conn, $sql))// here $sql is empty if $num is greater then zero

So close proper your if condition

if ($num == 0) 
{
$sql = "INSERT INTO tbl_rental (game_id, first_name, last_name, address, postal_code, start_date, rental_days, borrow_game, end_date)
VALUE ('$id', '$first_name', '$last_name', '$address', '$postal_code', '$start_date', '$rental_days', '$borrow_game', '$end_date')";

Inserting the data into the table^!

if (mysqli_query($conn, $sql))
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}// if condition close here

Also connection close at the end of your code

while ($row = mysqli_fetch_array($result)) 
{
$id = $row['game_id'];
}

mysqli_close($conn);// at last
Saty
  • 22,443
  • 7
  • 33
  • 51