I ran into this problem yesterday and I am still trying to figure it out. Before everyone downvotes this, I would like to tell that YES, I have referred to other similar questions, and yet I am unable to solve my problem.
So, here's my problem. I have a nav-top.php
file which basically contain the navigation bar of project. The code is
<?php
include('../includes/config.php');
$result1=mysqli_query($db,"SELECT * FROM `category`;");?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="../css/bootstrap.min.css">
<script src="../js/jquery.min.js"></script>
<!--font awesome
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/solid.css" integrity="sha384-wnAC7ln+XN0UKdcPvJvtqIH3jOjs9pnKnq9qX68ImXvOGz2JuFoEiCjT8jyZQX2z" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/fontawesome.css" integrity="sha384-HbmWTHay9psM8qyzEKPc8odH4DsOuzdejtnr+OFtDmOcIVnhgReQ4GZBH7uwcjf6" crossorigin="anonymous">-->
<link rel="stylesheet" href="../css/fawesome/css/all.css">
<title>Fast-Fries</title>
</head>
<style>
.bg-black{
background-color: #242323;
}
</style>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-black">
<div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo01">
<ul class="nav navbar-nav pull-sm-left">
<ul class="navbar-nav mr-right mt-2 mt-lg-0">
<li class="nav-item dropdown bg-primary">
<a class="nav-link dropdown-toggle bg-black" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Our Menu</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="../src/menu.php">View All</a>
<div class="dropdown-divider"></div>
<?php if($result1->num_rows>0){
while($row=$result1->fetch_assoc()){?>
<a class="dropdown-item" href="../src/menu.php?type=<?php echo $row['cat_id']; ?>"><?php echo $row['cat_title']; ?></a>
<div class="dropdown-divider"></div>
<?php }} ?>
</div>
</li>
</ul>
<li class="nav-item">
<a class="nav-link" href="#">Special Combos</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-logo mx-auto">
<li class="nav-item">
<a class="nav-link" href="../src/home.php"><img src="../images/logo.png" style="width:25px;"></a>
</li>
</ul>
<ul class="nav navbar-nav pull-sm-right">
<ul class="navbar-nav mr-right mt-2 mt-lg-0">
<li class="nav-item dropdown bg-primary">
<a class="nav-link dropdown-toggle bg-black" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">User</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="../src/all-products.php?id=<?php echo $row['cat_id']; ?>">Breakfast</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="../src/all-products.php?id=<?php echo $row['cat_id']; ?>">Lunch</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="../src/logout.php">Logout</a>
</div>
</li>
</ul>
</ul>
</div>
</div>
</nav>
I am trying to refresh my page when add to cart button is pressed on food-detail.php
page. The code for food-detail.php
is:
<?php
session_start();
if(!isset($_SESSION['id'])){
header('Location:index.php');
}
include('../includes/nav-top.php');
if(isset($_GET['id'])){
$id = $_GET['id'];
$result=mysqli_query($db,"SELECT * FROM `food`, `category` WHERE food.category=category.cat_id AND food.id=$id"); //The query was changed from select * from food to this query in order to extract cat_title
if($result->num_rows>0){
if($row=$result->fetch_assoc()){?>
<div class="container">
<div class="row">
<div class="col-lg-6 col-sm-6" style="margin-top:70px;">
<img src="../images/food/<?php echo $row['food_img']; ?>">
</div>
<div class="col-lg-6 col-sm-6" style="margin-top:70px;">
<label><h1><?php echo $row['food_name']; ?></h1></label><br>
<label><b>Category: </b><label><?php echo $row['cat_title']; ?> <a href="menu.php?type=<?php echo $row['cat_id']; ?>">(View More)</a></label></label><br>
<label><b>Veg/Non Veg: </b><?php
if(($row['veg_nonveg'])=='veg'){
echo '<label style="color:green">'.$row['veg_nonveg'].'</label>';
}
else{
echo '<label style="color:red">'.$row['veg_nonveg'].'</label>';
}
?></label><br>
<label><b>Price: </b>Rs.<?php echo $row['price']; ?> Per Burger</label><br>
<label><b>Quantity Left: </b><?php
if($row['quantity'] <= 5 ){ echo '<label style="color:red">'.$row['quantity']."</label>";
}
else{
echo '<label style="color:green">'.$row['quantity']."</label>";
}
?></label><br>
<form action="" method="POST">
<label><b>Enter Quantity: </b></label><input type="number" class="form-inline" name="quantity" value="1" min="1" max="<?php echo $row['quantity'];?>" style="text-align:center;">
<input type="submit" class="btn btn-outline-success btn-lg" style="margin-top:25px; margin-bottom: 30px;" value="Add To Cart" name="cart">
</form>
</div>
</div>
</div>
<?php }
}
//code for when add to cart is pressed
if(isset($_POST['cart'])){
$userid = $_SESSION['id'];
$qty = $_POST['quantity'];
$result2=mysqli_query($db,"INSERT INTO `cart`(`food_id`, `food_quantity`, `user_id`) VALUES ($id,$qty,$userid)");
$result3=mysqli_query($db,"UPDATE `food` SET `quantity`= `quantity` - $qty WHERE `id`=$id");
header('Location: food-detail.php');
}
} // end of outer if
else{
echo '<br><div class="container"><h1 style="color:red"> Uh-oh. It seems you are trying<br>to view the wrong food.</h1>';
echo '<a href="home.php">Click Here To Go Back To Home Page.</a></div>';
}
include('../includes/footer-bottom.php')?>
I am getting Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\x\includes\nav-top.php:46) error. Kindly help. Thank You.