PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 22, 2022

[FIXED] Trying to echo a table but it's not working

 February 22, 2022     mysql, phpmyadmin     No comments   

Issue

The table is called bookings. I'm trying to echo rows from the table, but I'm getting errors in some places according to user_id. Dreamweaver tells me the last few lines have errors, but I'm not sure what's the problem.

<?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>';

Solution

I have made some change

And here is the code-

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>';


Answered By - sanjaya
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing