PHP categories showing only one item - php

I'm trying to create a categories list, where user will be able to choose item with certain category, but as soon as I'm opening any of the categories, where "soon = '2'", there is only one item coming up. Everything else works.
The code bellow is for categories, where user can chose which caegory s/he want to see.
//navbar.php
<!-- Categories -->
<div class="dropdown-item menu_cat" type="button" id="catMenuButton"><i class="fas fa-caret-left"></i> Categories</div>
<form method="POST" class="dropdown-menu dropleft categories_menu row" id="cat_dropdown">
<button class="dropdown-item" name="candle"> Candles</button>
<button class="dropdown-item" name="cloth"> Clothes</button>
<button type="submit"class="dropdown-item" name="tech"> Tech</button>
</form>
Than here is everything else, that is suppose to exectute commands that will show products from category that user choose. The issue comes upp where 'if($row["soon"] == "2")' is.
//products.php
<?php
if(isset($_POST['candle'])){
$sql = "SELECT * FROM products WHERE category = 'candle'";
}else if(isset($_POST['cloth'])){
$sql = "SELECT * FROM products WHERE category = 'cloth'";
}else if(isset($_POST['tech'])){
$sql = "SELECT * FROM products WHERE category = 'tech'";
}else{
$sql = "SELECT * FROM products";
}
$result = $conn->query($sql);
if (!empty($result) && $result->num_rows > 0) {
while ($row = $result->fetch_assoc()){
if($row["soon"] == "2"){
?>
<div class="product_card col-xl-3 col-lg-4 col-md-5 col-sm-12 box-shadow">
<div class="card-header">
<h3 class="product_name my-0 font-weight-normal"> <?php echo $row["productName"] ?> </h3>
</div>
<div class="card-body">
<h2 class="card-title pricing-card-title"> </h2>
<img class="pic" src="productsImages\<?php echo $row["productID"]; ?>.jpeg" height="150px" width="120px"></img>
<br>
<p>
<?php echo $row["productPrice"]; ?> SEK
</p>
<a class="toProduct" href="products_index.php?id=<?php echo $row['productID'] ?>">
<div class="row">
<button class="btn_to_product btn btn-lg btn-outline-dark col-lg-10 col-md-10"> Go to product <i type="button" class="fas fa-arrow-alt-circle-right"></i></button>
</div>
</a>
</div>
</div>
<?php }else if($row['soon'] == "1" && isset($_POST['tech']) || isset($_POST['candle'])){?>
<div class="product_card col-xl-3 col-lg-4 col-md-5 col-sm-12 box-shadow">
<div class="card-header">
<h3 class="product_name my-0 font-weight-normal"> <?php echo $row["productName"] ?> </h3>
</div>
<div class="card-body">
<h2 class="card-title pricing-card-title"> </h2>
<img class="pic_soon" src="productsImages/comingSoon.png" height="150px" width="120px"></img>
<style>
</style>
<br>
<p>
<?php echo $row["productPrice"]; ?> SEK
</p>
<a class="toProduct" href="products_index.php?id=<?php echo $row['productID'] ?>">
<div class="row">
<button class="btn_to_product btn btn-lg btn-outline-dark col-lg-10 col-md-10"> Go to product <i type="button" class="fas fa-arrow-alt-circle-right"></i></button>
</div>
</a>
</div>
</div>
<?php
}
}
}
else if(empty($result) || $result->num_rows == 0){
echo "<h2 class='col-12'> Sorry! </h2>";
echo "<h3 class='col-12'> There is not any product in this category yet! </h3>";
}
$conn->close();
?>

In the original post was duplicate $row = $result->fetch_assoc(); statement.
In updated post there is first condition out of the while loop.
Solution bellow is wrapping all the if($row['soon'] into the while loop:
<div class="card-deck col-lg-12 col-md-12 col-sm-12 justify-content-center text-center">
<?php
if(isset($_POST['candle'])){
$sql = "SELECT * FROM products WHERE category = 'candle'";
}else if(isset($_POST['cloth'])){
$sql = "SELECT * FROM products WHERE category = 'cloth'";
}else if(isset($_POST['tech'])){
$sql = "SELECT * FROM products WHERE category = 'tech'";
}else{
$sql = "SELECT * FROM products";
}
$result = $conn->query($sql);
if (!empty($result) && $result->num_rows > 0) {
while ($row = $result->fetch_assoc()){
if($row['soon'] == "1" && isset($_POST['tech']) || isset($_POST['candle'])){
echo "<h2 class='col-12'> Sorry! </h2>";
echo "<h3 class='col-12'> This product will be soon avaiable </h3>";
}
if($row["soon"] == "2"){
?>
<div class="product_card col-xl-3 col-lg-4 col-md-5 col-sm-12 box-shadow">
<div class="card-header">
<h3 class="product_name my-0 font-weight-normal"> <?php echo $row["productName"] ?> </h3>
</div>
<div class="card-body">
<h2 class="card-title pricing-card-title"> </h2>
<img class="pic" src="productsImages\<?php echo $row["productID"]; ?>.jpeg" height="150px" width="120px"></img>
<br>
<p>
<?php echo $row["productPrice"]; ?> SEK
</p>
<a class="toProduct" href="products_index.php?id=<?php echo $row['productID'] ?>">
<div class="row">
<button class="btn_to_product btn btn-lg btn-outline-dark col-lg-10 col-md-10"> Go to product <i type="button" class="fas fa-arrow-alt-circle-right"></i></button>
</div>
</a>
</div>
</div>
<?php
}
}
}else if($result->num_rows == 0){
echo "<h2 class='col-12'> Sorry! </h2>";
echo "<h3 class='col-12'> There is not any product in this category yet! </h3>";
}
$conn->close();
?>
</div>
}
}else if($result->num_rows == 0){
echo "<h2 class='col-12'> Sorry! </h2>";
echo "<h3 class='col-12'> There is not any product in this category yet! </h3>";
}
$conn->close();
?>
</div>
Recommendation: Always debug your scripts with enabled PHP Error Reporting!

products.php
<?php
if(isset($_POST['candle'])){
$sql = "SELECT * FROM products WHERE category = 'candle'";
}else if(isset($_POST['cloth'])){
$sql = "SELECT * FROM products WHERE category = 'cloth'";
}else if(isset($_POST['tech'])){
$sql = "SELECT * FROM products WHERE category = 'tech'";
}else{
$sql = "SELECT * FROM products";
}
$result = $conn->query($sql);
if (!empty($result) && $result->num_rows > 0) {
while ($row = $result->fetch_assoc()){
if($row["soon"] == "2"){
?>
<div class="product_card col-xl-3 col-lg-4 col-md-5 col-sm-12 box-shadow">
<div class="card-header">
<h3 class="product_name my-0 font-weight-normal"> <?php echo $row["productName"] ?> </h3>
</div>
<div class="card-body">
<h2 class="card-title pricing-card-title"> </h2>
<img class="pic" src="productsImages\<?php echo $row["productID"]; ?>.jpeg" height="150px" width="120px"></img>
<br>
<p>
<?php echo $row["productPrice"]; ?> SEK
</p>
<a class="toProduct" href="products_index.php?id=<?php echo $row['productID'] ?>">
<div class="row">
<button class="btn_to_product btn btn-lg btn-outline-dark col-lg-10 col-md-10"> Go to product <i type="button" class="fas fa-arrow-alt-circle-right"></i></button>
</div>
</a>
</div>
</div>
<?php }else if($row['soon'] == "1" && isset($_POST['tech']) || isset($_POST['candle'])){?>
<div class="product_card col-xl-3 col-lg-4 col-md-5 col-sm-12 box-shadow">
<div class="card-header">
<h3 class="product_name my-0 font-weight-normal"> <?php echo $row["productName"] ?> </h3>
</div>
<div class="card-body">
<h2 class="card-title pricing-card-title"> </h2>
<img class="pic_soon" src="productsImages/comingSoon.png" height="150px" width="120px"></img>
<style>
</style>
<br>
<p>
<?php echo $row["productPrice"]; ?> SEK
</p>
<a class="toProduct" href="products_index.php?id=<?php echo $row['productID'] ?>">
<div class="row">
<button class="btn_to_product btn btn-lg btn-outline-dark col-lg-10 col-md-10"> Go to product <i type="button" class="fas fa-arrow-alt-circle-right"></i></button>
</div>
</a>
</div>
</div>
<?php
}
}
}
else if(empty($result) || $result->num_rows == 0){
echo "<h2 class='col-12'> Sorry! </h2>";
echo "<h3 class='col-12'> There is not any product in this category yet! </h3>";
}
$conn->close();
?>

Related

Disable and enable an anchor tag based

How do i disable and enable an anchor tag based on order_status condition? I want to make my receipt button only be able to click when the order_status been updated to ($irow['order_status'] == 5. It would be much appreciated if you all can provide me a code demo to show me how should I implement this using jquery. Thanks!
<div class="container mt-3 mb-5">
<div class="row form-group">
<?php
$total_price = 0.00;
$total_quantity = 0;
$sql = "SELECT *, sum(purchase_price) purchase_price, sum(quantity) quantity FROM ordered_items WHERE user_id = '$user_id' GROUP BY order_id ORDER BY order_datetime DESC";
$query = $conn->query($sql);
if (!mysqli_num_rows($query)) {
echo '
<div class="col-12 text-center">
<div class="alert alert-danger">
<strong><span class="iconify" data-icon="ic:round-remove-shopping-cart" data-width="30px" data-height="30px" data-inline="false"></span> You have no order</strong>
</div>
</div>
';
} else {
while ($row = $query->fetch_assoc()) {
$total_price = $row['purchase_price'];
if($total_price < 500.00 && $row['item_deliver_method'] == 'Delivery'){
$grand_total = $total_price + 10.00;
}else{
$grand_total = $total_price;
}
$total_quantity = $row['quantity'];
//$grand_total = $row['grand_total'];
?>
<div class="col-12 form-group orderidfocus">
<div class="product-wrapper" id="<?php echo $row['order_id']; ?>">
<p class="pl-2 pt-2 pr-2">OrderID: <?php echo $row['order_id']; ?>
</br><?php echo $row['item_deliver_method']; ?> Date: <?php echo $row['delivery_date']; ?></p>
<hr style="border: 0.5px dashed #DBDBDB;">
<?php
$isql = "SELECT * FROM ordered_items LEFT JOIN products ON ordered_items.product_id = products.id WHERE ordered_items.order_id = '".$row['order_id']."' ";
$iquery = $conn->query($isql);
while ($irow = $iquery->fetch_assoc()) {
if($irow['order_status'] == 1) {
$order_status = '<div class="badge-secondary font-italic p-1">Waiting for Response </div>';
}
if($irow['order_status'] == 2) {
$order_status = '<div class="badge-warning font-italic p-1">Preparing</div>';
}
if($irow['order_status'] == 3) {
$order_status = '<div class="badge-info font-italic p-1">In Delivery</div>';
}
if($irow['order_status'] == 4) {
$order_status = '<div class="badge-danger font-italic p-1">Ready to Pick up</div>';
}
if($irow['order_status'] == 5) {
$order_status = '<div class="badge-success font-italic p-1">Completed</div>';
}
?>
<div class="row h-100 pl-2 pr-2 form-group">
<div class="col-12">
<small class="float-right"><?php echo $order_status; ?></small>
</div>
<div class="col-3 my-auto form-group">
<img class="order-page-img-thumbnail" src="images/product-main/<?php echo $irow['product_photo']; ?>" alt="">
</div>
<div class="col-5 pl-0 pr-0 my-auto form-group">
<div class="product-title"><?php echo $irow['product_title']; ?></div>
</div>
<div class="col-4 pl-0 my-auto form-group">
<div class="product-price" style="text-align: right;">
RM<?php echo $irow['product_price']; ?>/<?php echo $irow['product_quantity']; ?><br>
<span style="color: #00644C;">X<?php echo $irow['quantity']; ?></span>
</div>
</div>
</div>
<?php } ?>
<hr style="border: 0.5px dashed #DBDBDB;">
<div class="row justify-content-end pl-2 pr-1">
<div class="col-3 my-auto form-group pr-0">
<p class="cat-title" style="text-align: left;font-size: 13px;">Qty: <?php echo $total_quantity; ?>KG</p>
</div>
<div class="col-5 my-auto form-group pl-0">
<p class="cat-title" style="text-align: right;font-size: 13px;">Amount: RM<?php echo number_format($grand_total,2); ?></p>
</div>
<div class="col-4 form-group pull-right text-center pl-0">
<a id="receiptbtn" target="_blank" href="receipt.php?order_id=<?php echo $row['order_id']; ?>" class="btn addtocart" style="font-size: 12px;"><span class="iconify" data-icon="bx:bx-download" data-inline="false"></span> Receipt</a>
</div>
</div>
</div>
</div>
<?php
}
}
?>

Update multiple subtitles photos in Mysql database

I have a system that after uploading multiple photos it generates a subtitles field for each photo, I want to update the database of these subtitles. How to do this. My table in database - tb_fotos_portfolio table (id, portfolio_id, legenda, arquivo).
Obs .: It would be only the part of UPDATE the subtitles
<?php
$sql = "SELECT * FROM tb_fotos_portfolio WHERE id_portfolio = $id";
$query = $DB->Select($sql);
if (count($query) == 0) { ?>
<div class="box-body">
<h3 class="text-center text-danger" style="margin: 140px 0;">Imagem não enviada!</h3>
</div>
<?php } else { ?>
<div class="box-body no-padding">
<ul class="users-list clearfix">
<?php foreach($query as $item) { ?>
<li>
<a class="popup-link" href="../../../upload/portfolio/imagem/<?php echo $item['arquivo']; ?>">
<img src="../../../upload/portfolio/miniatura/<?php echo $item['arquivo']; ?>" alt="<?php echo $item['arquivo']; ?>"/>
<span class="users-list-name"><?php echo $item['arquivo']; ?></span>
</a>
<a class="btn btn-label btn-danger btn-xs" href="acoes.php?acao=deleteFotos&id=<?php echo $_GET['id'];?>&arquivo=<?php echo $item['arquivo'];?>"><i class="fal fa-trash-alt"></i>Excluir</a>
<span class="users-list-name"><?php echo $item['legenda']; ?></span>
<form id="" action="acoes.php?acao=legendas_fotos" method="post" enctype="multipart/form-data">
<input type="text" name="legenda[]" id="legenda" class="form-control" placeholder="Legenda" value="<?php echo $item['legenda']; ?>"/>
</li>
<?php } ?>
</ul><!-- /.users-list -->
</div>
<button type="submit" class="btn btn-success btn-label btn-sm"><i class="fa fa-check"></i> Atualizar Legendas</button>
</form>
acao.php
case 'legendas_fotos':
$legenda = $_POST['legenda'];
...
$sql = "UPDATE tb_fotos_portfolio SET legenda= $legenda ";
$vCampos = array('id'=>$id);
$DB->Execute($sql, $vCampos);
// Volta para o form
header("location:form.php?id=" . $id);
exit;
break

Trying to populating bootstrap pills with contents from database

i'm new to web development; I have been trying to populate bootstrap pills dynamically from the database, generating the pill itself and also the content dynamically. So far only the Pills are being generated but the contents don't seem to be generated. The pills are meant to display contents that are individual pdf files obtained from the database sorted based on the category matching the pill being iterated in the while loop. Here is my code. Thanks.
//HTML Bootstrap
<div class="row bd-sidebar">
<div class="col-2 border-right">
<h3 class="pl-2">Categories</h3><hr>
<div class="nav flex-column nav-pills overflow-auto" id="v-nav-tab" role="tablist" aria-orientation="vertical">
<?php include_once '.assets/_server/category_data.php'; ?>
<?php echo $category_menu; ?>
<div class="col-10">
<div class="tab-content" id="v-pills-tabContent">
<?php echo $category_content;?>
</div>
</div>
</div>
</div>
</div>
//category_data.php
<?php
include_once("dbConfig.php");
$query = "SELECT * FROM `categories` GROUP BY `categoryName` ";
$categoryResult = mysqli_query($link, $query);
$category_menu = "";
$category_content = "";
$count = 0;
while($row = mysqli_fetch_array($categoryResult)){
$value = $row['categoryId'];
$categoryName = $row['categoryName'];
if($count == 0){
$category_menu .= '
<a class="nav-link active" id="v-pills-'.$value.'-tab" data-toggle="pill" href="#v-pills-'.$value.'" role="tab" aria-controls="v-pills-'.$value.' aria-selected="false">'.$categoryName.'</a>
';
$category_content .= '
<div class="tab-pane fade show active" id="v-pills-'.$value.'" role="tabpanel" aria-labelledby="v-pills-'.$value.'-tab">
<div class="row">
';
}else{
$category_menu .= '
<a class="nav-link" id="v-pills-'.$value.'-tab" data-toggle="pill" href="#v-pills-'.$value.'" role="tab" aria-controls="v-pills-'.$value.' aria-selected="false">'.$categoryName.'</a>
';
$category_content .= '
<div class="tab-pane fade" id="v-pills-'.$value.'" role="tabpanel" aria-labelledby="v-pills-'.$value.'-tab">
<div class="row">
';
}
$content_query = "SELECT * FROM `books` WHERE `categoryId` = '.$value.' GROUP BY `file_name`";
$content_result = mysqli_query($link, $content_query);
while($sub_row = mysqli_fetch_array($content_result)){
$category_content .= '
</div>
<div class="col-1">
<a class="material text-secondary text-decoration-none" href=".assets/pdf.js/web/viewer.html?file=materials/'.$sub_row['file_name'].'" data-toggle="tooltip" data-delay="300" data-animation="" data-html="true" title="'.$sub_row['file_name'].'">
<div class="mycard justify-content-center" style="width: 7rem;">
<img style="width: 70px; height: 70px;" src="img/book-thumbs/pdf_ico.png" class="mx-auto d-block" alt="pdf thumbnail">;
<div class="bookcardTitle">
<p class="text-center" id="bookcardTitle">'.$sub_row['file_name'].'</p>
</div>
</div>
</a>
</div>
';
}
$category_content .= '<div style="clear:both"></div></div></div>';
$count++;
}
?>
The immediate error in your code is the $content_query line:
$content_query = "SELECT * FROM `books` WHERE `categoryId` = '.$value.' GROUP BY `file_name`";
The string is quoted with double quotes but the $value part is surrounded by single quotes. The solution would be to replace the quotes:
$content_query = "SELECT * FROM `books` WHERE `categoryId` = ".$value." GROUP BY `file_name`";
You can see how the syntax highlight shows the error.
But there's a deeper problem here regarding sql injection. You should not concatenate values obtained from somewhere else in a query. Please see this question: How can I prevent SQL injection in PHP? and this website: https://phpdelusions.net/sql_injection for more information.
<?php
include_once("dbConfig.php");
$query = "SELECT * FROM `categories` GROUP BY `categoryName` ";
$categoryResult = mysqli_query($link, $query);
$category_menu = "";
$category_content = "";
$count = 0;
while($row = mysqli_fetch_array($categoryResult)){
$value = $row['categoryId'];
$categoryName = $row['categoryName'];
if($count == 0){
$category_menu .= '
<a class="nav-link " id="v-pills-'.$value.'-tab" data-toggle="pill" href="#v-pills-'.$value.'" role="tab" aria-controls="v-pills-'.$value.' aria-selected="false">'.$categoryName.'</a>
';
$category_content .= '
<div class="tab-pane fade " id="v-pills-'.$value.'" role="tabpanel" aria-labelledby="v-pills-'.$value.'-tab">
<div class="nav justify-content-center navbar-light bg-light">
<form class="form-inline v-pills-search" action="">
<input class="form-control rounded-pill mr-2" type="search" name="query" id="book_query" placeholder="Search...">
<button class="btn rounded-pill btn-outline-primary my-2 my-sm-0" type="submit"><i class="fas fa-search"></i></button>
</form>
</div>
<div class="row">
';
}else{
$category_menu .= '
<a class="nav-link" id="v-pills-'.$value.'-tab" data-toggle="pill" href="#v-pills-'.$value.'" role="tab" aria-controls="v-pills-'.$value.' aria-selected="false">'.$categoryName.'</a>
';
$category_content .= '
<div class="tab-pane fade" id="v-pills-'.$value.'" role="tabpanel" aria-labelledby="v-pills-'.$value.'-tab">
<div class="nav justify-content-center navbar-light bg-light">
<form class="form-inline v-pills-search" action="">
<input class="form-control rounded-pill mr-2" type="search" name="query" id="book_query" placeholder="Search...">
<button class="btn rounded-pill btn-outline-primary my-2 my-sm-0" type="submit"><i class="fas fa-search"></i></button>
</form>
</div>
<div class="row">
';
}
$content_query = "SELECT * FROM `books` WHERE `categoryId` = '".$row['categoryId']."' GROUP BY `file_name`";
$content_result = mysqli_query($link, $content_query);
if(mysqli_num_rows($content_result) < 0) {
$conRow_html .= '<br>No items found in this category!';
}
while($sub_row = mysqli_fetch_array($content_result)){
$category_content .= '
<div class="col-1 mr-4">
<a class="material text-secondary text-decoration-none" href=".assets/pdf.js/web/viewer.html?file=materials/'.$sub_row['file_name'].'" data-toggle="tooltip" data-delay="0" data-animation="true" data-html="true" title="'.$sub_row['file_name'].'">
<div class="mycard justify-content-center" style="width: 7rem;">
<img style="width: 70px; height: 70px;" src="img/book-thumbs/pdf_ico.png" class="mx-auto d-block" alt="pdf thumbnail">
<div class="bookcardTitle">
<p class="text-center book_name" id="bookcardTitle">'.$sub_row['file_name'].'</p>
</div>
</div>
</a>
</div>
';
}
$category_content .= '<div class="clear:both"></div></div></div>';
$count++;
}
?>

how to give a if else here

i want to include a else statement after the completion of the block after the second if for category equal to helathcare ....now i want another else for finance but i am not getting where excatly to place that else
i have a confusion on where to keep the else for finance and 2 more categories
now whenever i try to place the one else at the last goes unrecable to its if statement i donno its very confusing as to where to place 2 more else if statments for 2 more categories
<?php
global $row2;
if(isset($_POST['category']))
{
if($_POST['category']== 'Healthcare')
{
$query = "select *from event where category = 'Healthcare';";
$result=mysqli_query($conn,$query)or die(mysqli_error($conn));
while($row2= mysqli_fetch_array($result))
{
?>
<div class="events events-full event-list">
<div class="container">
<div class="row">
<div class="col-md-9 col-sm-8">
<!--Blog Post Start-->
<div class="blog-post">
<div class="post-thumb">
<div class="link-wrap"> <i class="fa fa-search"></i> <i class="fa fa-link"></i> </div>
<img src="images/gallery/<?php echo $row2['event_image']?>" alt='user'></div>
<div class="event-text">
<div class="event-counter"></div>
<h4> <?php echo($row2['title']); ?> </h4>
<p><?php echo($row2['descrption']); ?></p>
<p><span class="glyphicon glyphicon-map-marker"><?php echo($row2['location']); ?></span></p>
<p><span class="glyphicon glyphicon-grain"><?php echo($row2['organizer']); ?></span></p>
<a class="nd" href="">
<form action="eventdetail.php" method="post">
<input type='hidden' value="<?php echo $row2['id']; ?>" name='id'/>
<button type="submit" class="btn btn-primary" name="detail" value=”detail”>Event Detail</button>
</div>
</a> </div>
</div>
</form>
<!--Blog Post End-->
<?php }
}
}
else
{
global $row2;
$query = "select *from event;" ;
$result=mysqli_query($conn,$query)or die(mysqli_error($conn));
while($row2= mysqli_fetch_array($result))
{
?>
<div class="events events-full event-list">
<div class="container">
<div class="row">
<div class="col-md-9 col-sm-8">
<!--Blog Post Start-->
<div class="blog-post">
<div class="post-thumb">
<div class="link-wrap"> <i class="fa fa-search"></i> <i class="fa fa-link"></i> </div>
<img src="images/gallery/<?php echo $row2['event_image']?>" alt='user'></div>
<div class="event-text">
<div class="event-counter"></div>
<h4> <?php echo($row2['title']); ?> </h4>
<p><?php echo($row2['descrption']); ?></p>
<p><span class="glyphicon glyphicon-map-marker"><?php echo($row2['location']); ?></span></p>
<p><span class="glyphicon glyphicon-grain"><?php echo($row2['organizer']); ?></span></p>
<a class="nd" href="">
<form action="eventdetail.php" method="post">
<input type='hidden' value="<?php echo $row2['id']; ?>" name='id'/>
<button type="submit" class="btn btn-primary" name="detail" value=”detail”>Event Detail</button>
</div>
</a> </div>
</div>
</form>
<!--Blog Post End-->
<?php } } ?>
?>
You can't put multiple else blocks along with the if block. However, you can use multiple(as many as you want) elseif/else if blocks along with the if block, like this:
if($_POST['category'] == 'Healthcare'){
...
}elseif($_POST['category'] == 'Finance'){
...
}elseif($_POST['category'] == '...'){
...
}else{
...
}
Here's the reference: http://php.net/manual/en/control-structures.elseif.php
Part of your problem seems to be poor formatting. If you lined up your open and close tags for your if statements I think you would be able to see this better. Having said that, you can add any elseif/if clauses for the category if where I marked below:
<!--Blog Post End-->
<?php
} // closes while loop
} else{ } // closes healthcare if, add extra else here
} // closes POST if
else
{

Something wrong while deleting a row in sql table on clicking a button

Trying to delete a row from my SQL table in my shopping cart page,but my code deletes all the rows from the table on clicking a button.Even when I update quantity of the one product the quantity of all the products are changing.The answer might be the same for these two issues.Could you please review my code and tell me where I've done wrong.
Thankyou in advance
<?php
include "header.php";
require "includes/connect.php";
require "includes/product.php";
class Cartitem{
public function fetch_cart(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM cart_items WHERE user_id = ?");
$query->bindvalue(1, $_SESSION['user_id']);
$query -> execute();
return $query->fetchAll();
}
}
$cartitem= new Cartitem;
$cartitems=$cartitem-> fetch_cart();
?>
<div class="cart">
<div class = "container">
<div class="col-md-9 cart-items">
<h1 class="cart-items-h1">Cart</h1>
<hr>
<?php foreach($cartitems as $cartitem) { ?>
<div class="cart-header">
<h3>
<?php echo $cartitem['product_name'] ; ?></h3>
<br>
<?php
if(isset($_POST['delete'])){
$query = $pdo->prepare('DELETE FROM cart_items WHERE product_id=?');
$query->bindValue(1 , $cartitem['product_id']);
$query->execute();
}
?>
<form method = "POST" action="">
<button type="submit" class="close btn btn-default" name="delete"><span class="glyphicon glyphicon-remove " aria-hidden="true"></span></button>
</form>
<div class="cart-sec simpleCart_shelfItem">
<div class="cart-item cyc">
<img src="Tiger.jpg" class="img-responsive" alt=" No image"/>
</div>
<div class="cart-item-info">
<ul class="item-properties">
<li>
<form action="" method="post">
<?php
if(isset($_POST['qty'])){
$quantity=$_POST['qty'];
$query = $pdo->prepare('UPDATE cart_items SET quantity= ? WHERE product_id=?');
$query->bindValue(1 , $quantity);
$query->bindValue(2 , $cartitem['product_id']);
$query->execute();
}
?>
<select class="quantity" name="qty">
<?php
echo '<option >'.$cartitem['quantity'].'</option>';
for($q = 1 ; $q<10 ; $q++){
echo '<option >'.$q.'</option>';
}
?>
</select>
<button type="submit" class="update" name="update">Update</button>
</li>
<li><p>Rs.<?php echo $cartitem['price']; ?></p></li>
</ul>
<div class="delivery">
<p>Service Charges : Rs.190.00</p>
<span>Delivered in 2-3 bussiness days</span>
<div class="clearfix"></div>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
<?php }?>
</div>
This is (productpage.php) from where I'm storing the values to the cart table.Probably not so important to this issue. If yes
<?php
ob_start();
include ('header.php');
require('includes/connect.php');
require('includes/product.php');
$product = new Product;
if(isset ($_GET['id'])) {
$id = $_GET['id'];
$data = $product -> fetch_data($id);
if(isset($_POST['add'])){
if (isset($_SESSION['logged_in'])) {
$query = $pdo->prepare("SELECT product_id FROM cart_items WHERE product_id= ?");
$query -> bindValue(1, $id);
$query ->execute();
$num=$query->rowCount();
if($num == 0){
if(isset($_POST['qty'])){
$qty=$_POST['qty'];
}
$query = $pdo -> prepare("INSERT INTO cart_items(product_id , user_id, quantity,price,product_name) VALUES (?,?,?,?,?)");
$query -> bindValue(1, $id);
$query -> bindValue(2, $_SESSION['user_id']);
$query -> bindValue(3, $qty);
$query -> bindValue(4, $data['new_price']);
$query -> bindValue(5, $data['product_name']);
$query ->execute();
header('location:cart.php');
}
else{
echo " The product is already in your Cart";
}
}
else{
header('location:Login Page.php');
}
}
?>
<div class="showcase-grid">
<div class="container ">
<?php if(isset($error)){ ?>
<small style = "color : #aa0000"; ><?php echo $error ?></small>
<br><br>
<?php } ?>
<div class="col-md-8 showcase" id="showcase-div">
<div class="img-showcase" >
<br>
<div id="myCarousel" class="carousel slide text-center" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class=" img-responsive"></li>
<li data-target="#myCarousel" data-slide-to="1" class="img-responsive"></li>
<li data-target="#myCarousel" data-slide-to="2" class="img-responsive"></li>
<li data-target="#myCarousel" data-slide-to="3" class="img-responsive"></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<img class="slide-img" src="images/product2.jpg" alt="jwellery" >
</div>
<div class="item">
<img src="images/product3.jpg" alt="jwellery">
</div>
<div class="item">
<img src="images/product4.jpg" alt="jwellery" >
</div>
<div class="item">
<img src="images/product5.jpg" alt="jwellery" >
</div>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
<br>
</div>
</div>
<div class="col-md-4 showcase1 ">
<div class="showcase-rt-top">
<div class = "row">
<div class ="col-md-12">
<br>
<div class="pull-left Product-name">
<h3><?php echo $data['product_name']; ?></h3>
<br>
</div>
</div>
<br><br><br>
<div class ="col-xs-4 price">
<h4>Rs.<?php echo $data['new_price']; ?></h4>
</div>
<div class ="col-xs-4 oldprice text-left">
<s><h4 class="text-left">Rs.<?php echo $data['old_price']; ?></h4></s>
</div>
<div class ="col-xs-4">
<div class="pull-right rating-stars pull-left">
<h4><span class="label label-warning">3.5 <span class="glyphicon glyphicon-star star-stn" aria-hidden="true"></span></span></h5>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
<hr class="featurette-divider">
<div class="shocase-rt-bot">
<div class="col-xs-6 discount">
<?php
$discount = (($data['old_price'] - $data['new_price'])/$data['old_price'])*100;
echo round($discount) . '% off ';
?>
</div>
<form action = "" method="post" class="form-inline">
<div class="col-xs-6">
<div class="float-qty-chart">
<!-- <label class=" option">quantity:</label>-->
<select id= "select" class="form-control qnty-chrt" name="qty" >
<option value="1">quantity</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
</select>
<div class="clearfix"></div>
</div>
</div>
<ul class="ul_list">
<li class="ad-2-crt simpleCart_shelfItem">
<button type="submit" class="btn item_add" name="add" >Add To Cart</button>
<button type="submit" class="btn item_add" name="buy-now">Buy Button</button>
</li>
</ul>
<br>
</div>
</form>
<div class="features" >
<h3>product details</h3>
<ul>
<li><?php echo nl2br($data['brief_description']); ?></li>
</ul>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php
}
else{
header('location: index.php');
exit();
}
Include ('footer.php');
?>
This is the product.php
<?php
class Product {
public function fetch_all() {
global $pdo;
$query = $pdo->prepare("SELECT * FROM ear_rings_list");
$query -> execute();
return $query->fetchAll();
}
public function fetch_data($pid) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM ear_rings_list WHERE listing_id = ?");
$query->bindValue(1, $pid);
$query->execute();
return $query->fetch();
}
}
Actually you are working with an foreach loop
Just check here
<?php foreach($cartitems as $cartitem) { ?>
if(isset($_POST['delete'])){
$query = $pdo->prepare('DELETE FROM cart_items WHERE product_id=?');
$query->bindValue(1 , $cartitem['product_id']);
$query->execute();
}
if(isset($_POST['qty'])){
$quantity=$_POST['qty'];
$query = $pdo->prepare('UPDATE cart_items SET quantity= ? WHERE product_id=?');
$query->bindValue(1 , $quantity);
$query->bindValue(2 , $cartitem['product_id']);
$query->execute();
}
<?php }?>
Here you are just checking the isset($_POST['delete']) which will be true for each $cartitem['product_id'] that's why your each row is deleting and updating.
Solution:-
Use this code
<?php
include "header.php";
require "includes/connect.php";
require "includes/product.php";
class Cartitem
{
public function fetch_cart()
{
global $pdo;
$query = $pdo->prepare("SELECT * FROM cart_items WHERE user_id = ?");
$query->bindvalue(1, $_SESSION['user_id']);
$query->execute();
return $query->fetchAll();
}
}
$cartitem = new Cartitem;
$cartitems = $cartitem->fetch_cart();
?>
<div class="cart">
<div class="container">
<div class="col-md-9 cart-items">
<h1 class="cart-items-h1">Cart</h1>
<hr>
<?php
if (isset($_POST['qty']) && isset($_POST['product_id'])) {
$quantity = $_POST['qty'];
$product_id = $_POST['product_id'];
$query = $pdo->prepare('UPDATE cart_items SET quantity= ? WHERE product_id=?');
$query->bindValue(1, $quantity);
$query->bindValue(2, $product_id);
$query->execute();
}
?>
<?php
if (isset($_POST['delete']) && isset($_POST['product_id'])) {
$product_id = $_POST['product_id'];
$query = $pdo->prepare('DELETE FROM cart_items WHERE product_id=?');
$query->bindValue(1, $product_id);
$query->execute();
}
?>
<?php foreach($cartitems as $cartitem) { ?>
<div class="cart-header">
<h3>
<?php echo $cartitem['product_name']; ?></h3>
<br>
<form method="POST" action="">
<input type="hidden" value="<?php $cartitem['product_id'] ?>" name="product_id">
<button type="submit" class="close btn btn-default" name="delete"><span class="glyphicon glyphicon-remove " aria-hidden="true"></span></button>
</form>
<div class="cart-sec simpleCart_shelfItem">
<div class="cart-item cyc">
<img src="Tiger.jpg" class="img-responsive" alt=" No image"/>
</div>
<div class="cart-item-info">
<ul class="item-properties">
<li>
<form action="" method="post">
<input type="hidden" value="<?php $cartitem['product_id'] ?>" name="product_id">
<select class="quantity" name="qty">
<?php
echo '<option >' . $cartitem['quantity'] . '</option>';
for ($q = 1; $q < 10; $q++) {
echo '<option >' . $q . '</option>';
}
?>
</select>
<button type="submit" class="update" name="update">Update</button>
</form>
</li>
<li><p>Rs.<?php echo $cartitem['price']; ?></p></li>
</ul>
<div class="delivery">
<p>Service Charges : Rs.190.00</p>
<span>Delivered in 2-3 bussiness days</span>
<div class="clearfix"></div>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
<?php }?>
</div>
Changes :-
I just make an hidden input field for $cartitem['product_id'] here <input type="hidden" value="<?php $cartitem['product_id'] ?>" name="product_id">
and placed the delete and update query out side the foreach loop.

Categories