尝试回显表,但不起作用

时间:2019-02-22 03:20:13

标签: mysql phpmyadmin

该表称为预订。我试图从表中回显行,但是根据user_id在某些地方出现错误。 Dreamweaver告诉我最后几行有错误,但是我不确定是什么问题。

<?php
require 'connect.php';  
include("auth.php");

$user = $_SESSION['username'];
$uid = $_SESSION['user_id'];

$sql = "SELECT * FROM bookings WHERE user_id =". $uid;
$result = mysqli_query($conn, $sql);  

if ($result->num_rows > 0) { // if the mysqli_query performed above works 

echo "<br>";
echo '<table border="1">';
echo '<tr>
<th>booking_id</th>
<th>"Room_name"</th>
<th>"booking_date"</th>
<th>"period_name"</th>
<th>"booking_id"</th>
<th>"Cancel Booking"</th>
</tr>';

while ($row = mysqli_fetch_assoc($result)) { // important line
    echo '<tr>';
    echo '<td>'.$row["booking_id"].'</td>';
    echo '<td>'.$row["Room_name"].'</td>';
    echo '<td>'.$row["booking_date"].'</td>';
    echo '<td>'.$row["date_booked"].'</td>';
    echo '<td>'.$row["period_name"].'</td>';
    echo '<td>'.$row["booking_id"].'</td>';
    echo "</tr>";

echo "</table>";

}

else  echo '<p>You have no bookings.</p>';

1 个答案:

答案 0 :(得分:0)

我做了一些更改

这是代码-

require 'connect.php';  
include 'auth.php';

    $user = $_SESSION['username'];
    $uid = $_SESSION['user_id'];

        $sql = "SELECT * FROM bookings WHERE user_id ='". $uid."'";
        $result = mysqli_query($conn, $sql);  

        if ($result->num_rows > 0) { // if the mysqli_query performed above works 

                    echo "<br>";
                    echo '<table border="1">';
                    echo '<tr>
                    <th>booking_id</th>
                    <th>Room_name</th>
                    <th>booking_date</th>
                    <th>period_name</th>
                    <th>booking_id</th>
                    <th>Cancel Booking</th>
                    </tr>';
                    while ($row = mysqli_fetch_assoc($result)) { // important line

                        echo '<tr>';
                            echo '<td>'.$row["booking_id"].'</td>';
                            echo '<td>'.$row["Room_name"].'</td>';
                            echo '<td>'.$row["booking_date"].'</td>';
                            echo '<td>'.$row["date_booked"].'</td>';
                            echo '<td>'.$row["period_name"].'</td>';
                            echo '<td>'.$row["booking_id"].'</td>';
                        echo '</tr>';

                    }
                    echo '</table>';
        } else  echo '<p>You have no bookings</p>';