SQL selecting from table in user session - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Wednesday, July 5, 2023

SQL selecting from table in user session

Have made a restaurant web site that takes customer order online. I am trying to create a previous orders page which displays all users orders - but only the orders of the logged in user.

For this I am incorporating sessions. I can select data from a table without using sessions using the following code:

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "fos_db";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


$sql="SELECT * FROM food_customer WHERE name='{$_SESSION['name']}'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table><tr><th>Name</th><th>Email</th><th>Address</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
    echo "<tr><td>" . $row["name"]. "</td> <td>" . $row["email"]. " </td> <td>" . $row["address"]. "</td> </tr>";
                
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>

But when I incorporate a session id to select from the food_orders table it will not always fetch the data.

This coded does works:

$sql="SELECT * FROM food_customer WHERE name='{$_SESSION['name']}'";

This code doesnt work

$sql="SELECT * FROM food_orders WHERE id='{$_SESSION['name']}'";

So here I think I am rightly putting the user session 'name' into the ID of the food orders-so only orders for that customer will be displayed. However, this does not work.

This is the 3 sql tables that I am working with:

CREATE TABLE `food_customer` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `password` varchar(50) NOT NULL,
  `phone` varchar(50) NOT NULL,
  `address` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `food_items` (
  `id` int(30) NOT NULL,
  `name` varchar(30) NOT NULL,
  `price` int(30) NOT NULL,
  `description` varchar(200) NOT NULL,
  `images` varchar(200) NOT NULL,
  `status` varchar(10) NOT NULL DEFAULT 'ENABLE'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `food_items`
--
INSERT INTO `food_items` (`id`, `name`, `price`, `description`, `images`, `status`) VALUES
(1, 'Pizza', 7.50, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris dignissim at dolor in posuere. Sed eleifend ipsum in sem placerat, sed semper felis tincidunt.', './assets/images//menu/menubig1.jpg', 'ENABLE'),
(2, 'Salad', 5, 'Maecenas eleifend sit amet magna et consequat. Nunc a erat non arcu efficitur semper ut et mauris. Aenean sed faucibus purus. Duis hendrerit diam at leo imperdiet, vel placerat dolor porta.', './assets/images/menu/menubig2.jpg', 'ENABLE'),
(3, 'Bistecca Alla florentina', 5, 'Nam sagittis fringilla leo, eget eleifend orci vestibulum sed. Proin a sem eu tortor hendrerit sollicitudin. Vivamus tempus ullamcorper nibh vitae viverra.', './assets/images/menu/menubig3.jpg', 'ENABLE'),
(4, 'Sicillian Ricotta Cake', 14, 'Sed sit amet neque fringilla, eleifend libero gravida, tempus nisl.', './assets/images/menu/menubig4.jpg', 'ENABLE'),

CREATE TABLE `food_orders` (
  `id` int(30) NOT NULL,
  `item_id` int(30) NOT NULL,
  `name` varchar(30) NOT NULL,
  `price` int(30) NOT NULL,
  `quantity` int(30) NOT NULL,
  `order_date` date NOT NULL,
  `order_id` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Excuse the long codes but really want to get problem clear. Am new to sql. Any help would be most appreciated as have been stuck on this for days.



source https://stackoverflow.com/questions/76615007/sql-selecting-from-table-in-user-session

No comments:

Post a Comment