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
Related
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();
?>
I have created a search page that will search columns in the posts table in the database. When I go to the search page I get the error Notice: Undefined index: search in /app/public/search.php, but search will work if I search for a word that exists in the database. I expected the error since $search won't be recognized until the form is submitted. Here's the code.
<form class="form-inline" action="search.php" method="post">
<input class="form-control" type="text" name="search" placeholder="Search"><br>
<button class="btn btn-primary" type ="submit">Submit</button>
</form>
<?php
$search = $_POST['search'];
$sql = "SELECT * FROM posts WHERE post LIKE '%$search%' OR title LIKE '%$search%' OR datetime LIKE '%$search%'";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$posts = $stmt->fetchAll();
$postCount = $stmt->rowCount();
if ($postCount < 1) {
flash('no_posts_found', '<h3>No Posts Found</h3>', 'lead');
}
?>
<?php foreach($posts as $post) : ?>
<div class="card my-4">
<img class="img-fluid card-img-top" src="uploads/<?php echo htmlentities($post->image); ?>" alt="post image">
<div class="card-body">
<h2 class="card-title"><?php echo htmlentities($post->title); ?></h2>
<small>Created on <?php echo htmlentities($post->datetime); ?> by <?php echo htmlentities($post->author); ?></small>
<span class="float-right badge badge-dark">20 Comments</span>
<hr>
<p><?php echo htmlentities(mb_strimwidth($post->post, 0, 150, '...')); ?></p>
<a class="btn btn-primary float-right" href="post.php?id=<?php echo htmlentities($post->id); ?>">Read More<i class="fas fa-angle-double-right ml-1"></i></a>
</div>
</div>
<?php endforeach; ?>
</div>
I tried using if(isset($_POST['search'])) which stops the error, but then I get the message Undefined variable: posts in /app/public/search.php. Here's the code:
<?php if(isset($_POST['search'])) {
$search = $_POST['search'];
$sql = "SELECT * FROM posts WHERE post LIKE '%$search%' OR title LIKE '%$search%' OR datetime LIKE '%$search%'";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$posts = $stmt->fetchAll();
$postCount = $stmt->rowCount();
if ($postCount < 1) {
flash('no_posts_found', '<h3>No Posts Found</h3>', 'lead');
}
}
?>
<?php foreach($posts as $post) : ?>
<div class="card my-4">
<img class="img-fluid card-img-top" src="uploads/<?php echo htmlentities($post->image); ?>" alt="post image">
<div class="card-body">
<h2 class="card-title"><?php echo htmlentities($post->title); ?></h2>
<small>Created on <?php echo htmlentities($post->datetime); ?> by <?php echo htmlentities($post->author); ?></small>
<span class="float-right badge badge-dark">20 Comments</span>
<hr>
<p><?php echo htmlentities(mb_strimwidth($post->post, 0, 150, '...')); ?></p>
<a class="btn btn-primary float-right" href="post.php?id=<?php echo htmlentities($post->id); ?>">Read More<i class="fas fa-angle-double-right ml-1"></i></a>
</div>
</div>
<?php endforeach; ?>
</div>
I'm trying to understand what I am doing wrong. Can anyone give me a pointer in the right direction please?
That error is came from foreach loop.Add isset and empty check for foreach loop to.
<?php if( isset($posts) && !empty($posts) ) { foreach($posts as $post) : ?>
<div class="card my-4">
<img class="img-fluid card-img-top" src="uploads/<?php echo htmlentities($post->image); ?>" alt="post image">
<div class="card-body">
<h2 class="card-title"><?php echo htmlentities($post->title); ?></h2>
<small>Created on <?php echo htmlentities($post->datetime); ?> by <?php echo htmlentities($post->author); ?></small>
<span class="float-right badge badge-dark">20 Comments</span>
<hr>
<p><?php echo htmlentities(mb_strimwidth($post->post, 0, 150, '...')); ?></p>
<a class="btn btn-primary float-right" href="post.php?id=<?php echo htmlentities($post->id); ?>">Read More<i class="fas fa-angle-double-right ml-1"></i></a>
</div>
</div>
<?php endforeach; } ?>
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
{
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.
I am working on a project where I came across where when I click plus sign the data will hide and the appear in the div next to it.
here is the code:
<fieldset class="col-md-4" >
<legend>Services</legend>
<div class="col-md-12" >
<?php
$id = 0;
foreach ($servicesname as $val) {
$id++;
?>
<div class="col-md-12" style="font-size: 16px;" id="itemservices<?php echo $id ?>">
<span style="float:left;" ><?php echo $val[0]['servicename']; ?></span>
<a style="float:right;" href onclick="return addSrvToCart('itemservices<?php echo $id ?>')" >
<strong>₹ <?php echo $val[0]['amount']; ?></strong>
<span class="glyphicon glyphicon-plus-sign" id="id_<?php echo $id; ?>"></span>
</a>
</div>
<?php } ?>
</div>
</fieldset>
and the code for next div:
<fieldset class="col-md-4" >
<legend>Cart</legend>
<div style="list-style:none;" class="no-left-padding" id="cart">
<div class="col-md-12" >
</div>
</div>
<div class="col-sm-12 no-right-padding" style="background-color:#f3f0f0; padding-top:6px; border:1px solid#ccc">
<label class="pull-right">/*what should i write here to show the sum */: ₨ </label>
</div>
<button style="margin-top:10px;" class="btn btn-primary btn-sm pull-right" onclick="bookNowAfterFilter()">Book Now</button>
</fieldset>
And This the Jquery code:
<script>
function addSrvToCart(elem){
alert($('#' + elem).html());
$('#' + elem).hide();
//return false;
//what should i write here the show that hidden div
document.getElementById('summery);
return false;
}
</script>
This is the picture and I want to display the data in the Cart div and show the amount
You need to change in both HTML and Script to achieve the result
Try like this
HTML
<fieldset class="col-md-4" >
<legend>Services</legend>
<div class="col-md-12" >
<?php
$id = 0;
foreach ($servicesname as $val) {
$id++;
?>
<div class="col-md-12" style="font-size: 16px;" id="itemservices<?php echo $id ?>">
<span style="float:left;" ><?php echo $val[0]['servicename']; ?></span>
<a style="float:right;" onclick=" addSrvToCart('itemservices<?php echo $id ?>')" >
₹<strong> <?php echo $val[0]['amount']; ?></strong>
<span class="glyphicon glyphicon-plus-sign" id="id_<?php echo $id; ?>"></span>
</a>
</div>
<?php } ?>
</div>
</fieldset>
and the code for next div:
<fieldset class="col-md-4" >
<legend>Cart</legend>
<div style="list-style:none;" class="no-left-padding">
<div class="col-md-12" id="cart" >
</div>
</div>
<div class="col-sm-12 no-right-padding pull-right" style="background-color:#f3f0f0; padding-top:6px; border:1px solid#ccc">
Total: ₨ <label class="" id="sumAmount">0</label>
</div>
<button style="margin-top:10px;" class="btn btn-primary btn-sm pull-right" onclick="bookNowAfterFilter()">Book Now</button>
</fieldset>
Script:
function addSrvToCart(elem){
var div=$('#' + elem);
div.hide();
$('#cart').append(div.html());
var total = parseInt(div.find('strong').html()) + parseInt($('#sumAmount').html());
$('#sumAmount').html(total);
}
It will produce output as
I think it will help you.