douliang2935
2018-06-22 08:32
浏览 99
已采纳

从数据库中获取多个数据(或左右)

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.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsxrq28228 2018-06-22 09:07
    已采纳

    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']
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongpeng0127 2018-06-22 08:45

    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;
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题