0

I'm trying to get multiple bookings from my database for a hotel project when i tried to get the bookings from my database i only recieved one booking it should display/select multiple bookings.

This is my php code: (formulier.php)

    <?php
    $dt1 = $_POST['date1'];
    $dt2 = $_POST['date2'];

    $query = "SELECT * FROM dt_tb WHERE dt BETWEEN '$dt1' AND '$dt2'";
    $result = mysqli_query($connect,$query);
    if (mysqli_num_rows($result)) {
        while($row = $result->fetch_assoc()) { include_once 'source.php';
        }
    echo "<br>formulier.php";
    echo "<br>check-in:&nbsp;&nbsp; $dt1 <br>check-out: $dt2";

    }

    else {
        die;
    }
    ?>

And this is my other php code: (source.php)

<?php
$dt1 = $_POST['date1'];
$dt2 = $_POST['date2'];
$query = "SELECT * FROM dt_tb WHERE dt BETWEEN '$dt1' AND '$dt2'";
$result = mysqli_query($connect,$query);
if (mysqli_num_rows($result)) {
    while($row = $result->fetch_assoc()) {
    }
    echo "source.php";
    echo "<br>check-in:&nbsp;&nbsp; $dt1 <br>check-out: $dt2";
}

else {
    die;
}
?>

this is my database: (dt_tb)

https://gyazo.com/966efdf144a8cd1a74fa04a8127cd8f4


This is what i receive: (Website)

https://gyazo.com/1384bdafb5055f5777a40e88baccdbdd


I know my code is messed up badly and tried to solve it for quite some time.

4
  • 'I know my code is messed up badly and tried to solve it for quite some time.' - What have you tried? Commented Jun 22, 2018 at 8:34
  • 1
    include_once in while loop will executes only once. Why you query database multiple times with same query? Commented Jun 22, 2018 at 8:34
  • @Script47 I've asked my teachers they said the first code of mine was very wrong then i started redoing the booking system i believe that im close to getting it right. Commented Jun 22, 2018 at 8:40
  • @Justinas I know, my teacher told me that aswell and i thought that was wrong aswell do you have any idea what i can do to fix it? Commented Jun 22, 2018 at 8:41

2 Answers 2

1

I don't know why you need two files. You can join them to single loop.

<?php
$dt1 = !empty($_POST['date1']) ? $_POST['date1'] : null;
$dt2 = !empty($_POST['date2']) ? $_POST['date2'] : null;

if (!$dt1 || !$dt2) {
   throw new Exception("No dates passed!");
}

$query = "SELECT * FROM dt_tb WHERE dt BETWEEN '$dt1' AND '$dt2'";
$result = mysqli_query($connect, $query);

if (mysqli_num_rows($result)) {
    while($row = $result->fetch_assoc()) {
        echo "check-in:&nbsp;&nbsp; {$row['dt']}<br/>";
        echo "check-out: {$row['dt2']}<br/>";
        echo "<br/>";
    }
} else {
    echo "No bookings found between {$dt1} and {$dt2}";
    die();
}
?>

What was wrong:

  1. require_once is called only for first iteration, no further calls was made. It's used mostly when including classes or some configs, that must be included only once in whole logic.
  2. You try to echo your data outside loop inside source.php
  3. You echo passed parameters to server, but not actual database record entries. Changed $dt1 to $row['dt'] and $dt2 to $row['dt2']
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks bro! The code worked perfectly just added 1 HTML include for the bookin under its pretty much done now! Thanks!
0

You repeated the query in both files! Why? As you typed, you don't need to include 2 lines! , you can add them directly:

$dt1 = $_POST['date1'];
$dt2 = $_POST['date2'];

$query = "SELECT * FROM dt_tb WHERE dt BETWEEN '$dt1' AND '$dt2'";
$result = mysqli_query($connect,$query);
if (mysqli_num_rows($result)) {
    while($row = $result->fetch_assoc()) { 
       echo "source.php";
       echo "<br>check-in:&nbsp;&nbsp; $dt1 <br>check-out: $dt2";
    }
echo "<br>formulier.php";
echo "<br>check-in:&nbsp;&nbsp; $dt1 <br>check-out: $dt2";

}

else {
    die;
}

1 Comment

thats wrong :). Why?: It's coming as strings. not as bookings. gyazo.com/27ce708e2d303ec9ad532903b76832bb Thanks for your time though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.