I've tried a lot thing and a lot ideas, but i can't make this code run well.
I'm starting to build page for a shopping cart.
In these images i want update quantity each product separately in database using sql
When I update quantity product from website, I'd like to update just the product by p_id . not all products.
Lets say I have for example two product in my cart shop:
in this images i show u my database
And this is my work so far:
<form action="" method="post">
<table border="0" cellspacing="0" width="100%">
<tr colspan="5" class="title_incart">
<th colspan="2">product</th>
<th>qty</th>
<th>price</th>
<th>x</th>
</tr>
<?php
global $connect;
$ip = getIp();
$total = 0;
$t_price = "SELECT * FROM cart WHERE ip_add = '$ip'";
$run_price = mysqli_query($connect, $t_price);
while($row_t_price = mysqli_fetch_array($run_price)){
$pro_id_t = $row_t_price['p_id'];
$price_pro = "SELECT * FROM products WHERE p_id = '$pro_id_t'";
$run_price_pro = mysqli_query($connect, $price_pro);
while($row_price_pro = mysqli_fetch_array($run_price_pro)){
$pro_price = array($row_price_pro['p_price']);
$pro_title = $row_price_pro['p_title'];
$pro_img = $row_price_pro['p_img'];
$pro_price_single = $row_price_pro['p_price'];
$values = array_sum($pro_price);
$total +=$values;
?>
<tr colspan="5" class="info_incart">
<th><img class="res_img_incart" src="files/images/images_product/<?php echo $pro_img; ?>"/></th>
<th><?php echo $pro_title; ?></th>
<th><input type="text" name="abcd" size="1" value="<?php echo $_COOKIE['abcd']; ?>"/></th>
<?php
if(isset($_POST['update_cart'])){
$abcd = $_POST['abcd'];
$update_qty = "UPDATE cart SET abcd = '$abcd' WHERE p_id = '$pro_id_t'";
$run_u_qty = mysqli_query($connect, $update_qty);
$_COOKIE['abcd'] = $abcd;
$total = $total*$abcd;
}
?>
<th><?php echo $pro_price_single; ?> $</th>
<th><input type="checkbox" name="remove[]" value="<?php echo $pro_id_t; ?>"/></th>
</tr>
<?php } } ?>
<tr colspan="5" class="total_price_incart">
<th>Total price</th>
<th><?php echo $total; ?> $</th>
</tr>
<tr class="checkout_incart">
<th colspan="2">
<input type="submit" name="update_cart" value="update cart"/>
</th>
</tr>
</table>
</form>
i just want help on this part
<th><input type="text" name="abcd" size="1" value="<?php echo $_COOKIE['abcd']; ?>"/></th>
<?php
if(isset($_POST['update_cart'])){
$abcd = $_POST['abcd'];
$update_qty = "UPDATE cart SET abcd = '$abcd' WHERE p_id = '$pro_id_t'";
$run_u_qty = mysqli_query($connect, $update_qty);
$_COOKIE['abcd'] = $abcd;
$total = $total*$abcd;
}
?>
<input type="submit" name="update_cart" value="update cart"/>
Help me please, I want this for my school assignment, and I'm sorry about my English, in case there's anything wrong.
my friend i found the solution, after a lot trying
i used
$query = "UPDATE cart SET abcd= 'abcd + 1' WHERE p_id = '$pro_id'";
and its work fine, when i add product to my cart its add +1 on old value
You loop through your items, and check if $_POST['update_cart'] is set to update an item's quantity. But if $_POST['update_cart'] is set for one itteration, it will be set for all itterations. So you will update the quantity of each item in the loop.
What you could do, is give each input field a unique name. Not just name them all abcd. This way, you will be able to identiy the quantity for each item.
Something like this:
<tr colspan="5" class="info_incart">
<th><img class="res_img_incart" src="files/images/images_product/<?php echo $pro_img; ?>"/></th>
<th><?php echo $pro_title; ?></th>
<th><input type="text" name="abcd<?php echo $pro_id_t ?>" size="1" value="<?php echo $_COOKIE['abcd' + $pro_id_t]; ?>"/></th>
<?php
if(isset($_POST['update_cart'])){
$abcd = $_POST['abcd' + $pro_id_t];
$update_qty = "UPDATE cart SET abcd = '$abcd' WHERE p_id = '$pro_id_t'";
$run_u_qty = mysqli_query($connect, $update_qty);
$_COOKIE['abcd' + $pro_id_t] = $abcd;
$total = $total*$abcd;
}
?>
<th><?php echo $pro_price_single; ?> $</th>
<th><input type="checkbox" name="remove[]" value="<?php echo $pro_id_t; ?>"/></th>
</tr>
Related
i need to select from a DB only the items from table that are not yet added to cart, and if they have been added to unset the value from the $row array. this is so far what my problem looks like
$emptyCart=[];
$result = mysqli_query($con,"SELECT * FROM `products`");
while($row = mysqli_fetch_assoc($result))
{
echo "<div class='product_wrapper'>
<form method='post' action=''>
<input type='hidden' name='code' value=".$row['id']." />
<table class='product'>
<thead>
<tr>
<th class='image' rowspan='3'><img src='img1.jpg' /></th>
<th class='title'>".$row['title']."</th>
<th class='buy' rowspan='3'><button type='submit' class='buy' name='add'>Add</button></th>
</tr>
<tr>
<td class='description'>".$row['description']."</td>
</tr>
<tr>
<td class='price'>$".$row['price']."</td>
</tr>
</thead>
</table>
</form>";
};
You can exclude IDs from query.
Here an example assuming $cart is an array containing IDs. We build a comma separated string list of all those IDs with implode.
If cart is not empty we build a where clause for the query. Now the query won't contain the given IDs.
$cart = [55, 123, 5564, 3422];
$list = implode(', ', $cart);
$exclude = $list ? "WHERE `id` NOT IN($list)" : '';
$result = mysqli_query($con, "SELECT * FROM `products` $exclude");
SELECT * FROM `products` WHERE `id` NOT IN(55, 123, 5564, 3422)
I have to delete a record from database using a button but my delete query does not work. Records are entered in database successfully with insertion query. I followed exact tutorial for php code available on YouTube "How to delete records from database with PHP & MySQL" by "kanpurwebD". The code in tutorial works fine but my code still does not delete record. (I have 2 records entered in database).
My code is as follows:
<div class="container">
<div class="row">
<form action='add_record.php' method='get'><button type='submit' name='id' value='submit' class='btn btn-default'>ADD RECORD</button><br />
</form>
<table class="table table-hover table-responsive">
<thead>
<tr>
<th>Topic #</th>
<th>Name</th>
<th>Admin ID</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
echo '<br />';
$query = "SELECT * FROM tb_topic";
$result = $con->query($query);
if(isset($_POST['submitDeleteBtn'])){
$key = $_POST['keyToDelete'];
$check = "Select * from tb_topic where topic_id = '$key'";
if(mysqli_num_rows($con, $check)>0){
$query_delete = mysqli_query($con,"Delete from tb_topic where topic_id = '$key'");
echo 'record deleted';
}
}
while($query_row = mysqli_fetch_array($result)) {?>
<tr>
<td><?php echo $query_row['topic_id'];?></td>
<td><?php echo $query_row['topic_name'];?></td>
<td><?php echo $query_row['aid'];?></td>
<td><input type = 'checkbox' name = 'keyToDelete' value = "<?php echo $query_row['topic_id'];?>" required></td>
<td><input type="submit" name="submitDeleteBtn" class="btn btn-danger"></td>
</tr>
<?php }
?>
</html>
I got it resolved by using following statement:
if(isset($_GET['delete'])) {
$page = filter_input(INPUT_GET, 'delete', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$sql = "DELETE FROM tb_topic WHERE topic_id = $page";
}
$_GET() was not taking id as int so I tried typecasting it and it worked for me.
hey im trying to make an online shopping website but im stuck at the cart section
im trying to show the quantity of product in the Text Box and update it also which doesn't seem to work with my code and also im unable to remove product from the cart.
im using check box for delete and an update button of type submit and an text box to show the quntity
here's code the my cart.php page..
<form action="" method="post" enctype="multipart/form-data">
<table align="center" width="700px" bgcolor="skyblue">
<tr align="center">
<th>Remove</th>
<th>Products</th>
<th>Quantity</th>
<th>Total Price</th>
</tr>
<?php
$total = 0;
global $con;
$ip = getIp();
$select_price = "select * from cart where ip_addr = '$ip'";
$run_price = mysqli_query($con, $select_price);
while ($p_price = mysqli_fetch_array($run_price)) {
$pro_id = $p_price['p_id'];
$pro_qty = $p_price['qty'];
$pro_price = "select * from products where product_id = '$pro_id'";
$run_pro_price = mysqli_query($con, $pro_price);
$select_qty = "select * from cart where p_id = '$pro_id'";
$run_qty = mysqli_query($con, $select_qty);
$row_qty = mysqli_fetch_array($run_qty);
$qty = $row_qty['qty'];
while ($pp_price = mysqli_fetch_array($run_pro_price)) {
$product_price = array($pp_price['product_price']);
$product_title = $pp_price['product_title'];
$product_image = $pp_price['product_image'];
$single_price = $pp_price['product_price'];
$values = array_sum($product_price);
$total += $values;
?>
<tr align="center">
<td><input type="checkbox" name="remove[]" value="<?php echo
$pro_id; ?>"></td>
<td><?php echo $product_title; ?><br>
<img src="Admin_area/product_image/<?php echo $product_image; ?>"
width="60px" height="60px">
</td>
<td><input type="text" name="qty" size="4px" value="<?php echo $qty;
?>"></td> // Calling the $qty variable which store the product quantity
<?php
if (isset($_POST['update_cart'])) {
$qty = $_POST['qty'];
$update_qty = "update cart set qty='$qty' where p_id =
'$pro_id'";
$run_qty = mysqli_query($con, $update_qty);
$_SESSION['qty'] = $qty;
$total = $total*$qty;
echo "<script>window.open('cart.php','_self')</script>";
}
?>
<td>₨ <?php echo $single_price; ?></td>
</tr>
<?php } } ?>
<tr align="right">
<td colspan="2" style="padding: 10px;"><b>Sub Total:</b></td>
<td colspan="2" style="padding: 10px;">₨ <?php echo $total; ?>
</td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="update_cart"
value="Update Cart"></td>
<td><button><a href="index.php" style="text-decoration: none; color:
black;">Continue</a></button></td>
<td><button><a href="checkout.php" style="text-decoration: none;
color: black;">Checkout</a></button></td>
</tr>
</table>
</form>
<?php
function updatecart(){
$ip = getIp();
if (isset($_POST['update_cart'])) {
// when update cart button clicked delete all product which are checked
foreach ($_POST['remove'] as $remove_id) {
$delete_product = "delete from cart where p_id = '$remove_id'
AND ip_addr = '$ip'";
$run_delete = mysqli_query($con,$delete_product);
if ($run_delete) {
echo "<script>alert('Inside Function');</script>";
echo "<script>window.open('cart.php','_self')</script>";
}
}
}
elseif (isset($_POST['continue'])) {
echo "<script>window.open('index.php','_self')</script>";
}
}
echo #$update_cart = updatecart();
?>
</div>
I am trying to get the quantity in text field and multiply it with the total price,but i always got the following error,can anyone help me in that?
Warning: A non-numeric value encountered
<table class="table">
<tr align="center">
<th>Remove</th>
<th>Product(S)</th>
<th>Quantity</th>
<th>Total Price</th>
</tr>
<?php
$total = 0;
global $con;
$ip = getIp();
$sel_price = "SELECT * FROM Cart WHERE IP_Address='$ip'";
$pricerows = $con->query($sel_price)->fetchall(PDO::FETCH_ASSOC);
foreach ($con->query($sel_price) as $row_price) {
$pro_id = $row_price['Product_ID'];
$pro_price = "SELECT * FROM Products WHERE Product_ID='$pro_id'";
foreach ($con->query($pro_price) as $row_proprice) {
$product_price = array($row_proprice['Price']);
$product_title = $row_proprice['Product_Name'];
$product_imaage = $row_proprice['Photo'];
$single_price = $row_proprice['Price'];
$values = array_sum($product_price);
$total += $values;
?>
<tr align="center">
<td><input type="checkbox" name="remove[]" value="<?php echo $pro_id; ?>"></td>
<td><?php echo $product_title; ?><br><img src="images/<?php echo $product_imaage; ?>" width="60" height="60"/></td>
<td><input type="text" size="4" id="qty" name="qty" value="<?php echo $_SESSION['qty'];?>"/></td>
<?php
if (isset($_POST['update_cart'])) {
$qty = $_POST['qty'];
$update_qty = "UPDATE cart set Quantity='$qty'";
$updaterows = $con->prepare($update_qty);
$updaterows->execute();
$_SESSION['qty'] = $qty;
$total = $total*$qty; //ERROR APPEARS HERE
}
?>
<td><?php echo $single_price . " L.E";?></td>
</tr>
</table>
You need to convert the value to integer, you can use
int(your value)
$total = $total* int($qty);
I hope this will solve the problem.
Please don't insert unfiltered $_POST variables into sql queries, always use filters to prevent sql injections!
As far as I can see you're trying to multiply $total (which is not initialized) with $qty .
Please insert $total = 0 (or an equal initialization) somewhere before this calculation!
I have a page that contains an ordering form, on this form it lists the vendor information and then each of the products for the vendor underneath and in front of the product is an input field that allows the user to input the quantity of each product that they want.
Upon submitting the information goes to a confirmation page where I need to be able to show the order information. On the form on the order page, I have a hidden field that contains the vendor id. and the vendor id is put once for each vendor. What I need to be able to do is not only echo out the quantity but also echo out the vendor id specific for each order. My code is below. The first block is the order page and then the block below that will be the confirm page.
As it stands right now underneath every quantity it displays all the vendor ids as opposed to just the one I need.
<?php defined('C5_EXECUTE') or die("Access Denied.");?>
<div class="ccm-ui">
<?php
$db= Loader::db(); //This loads the database helper.
Loader::model('user'); //This loads the user Model.
$user = new User();
$userInfo = UserInfo::getByID($user->getUserID()); //This gets the user info for the current user.
$userCostCenter = $userInfo->getAttribute('cost_center'); //This sets a variable equal to the attribute Cost Center for the current user.
//The if statement below checks if the user is an admin and then displays the info accordingly.
if ($userCostCenter === "Admin") {
?>
<form name="SelectCostCenter" action="/adminorder" method="POST">
<select name="CostCenter">
<option value="unitedilluminating">United Illumination</option>
<option value="clp">CL&P</option>
</select>
<input type="submit" value="Continue">
<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
<?php
} elseif ($userCostCenter === "United Illuminating") {
?>
<form name="OrderForm" action="/confirm" method="POST">
<?php
$query = 'SELECT * FROM Vendors WHERE costCenterID = 1';
$productQuery = 'SELECT * FROM Products WHERE costCenterID = 1';
$results = $db->getAll($query);
$productResults = $db->getAll($productQuery);?>
<table class="table">
<thead>
<tr>
<th>Quantity/Product</th>
<th>Category</th>
<th>Vendor</th>
<th>Address</th>
</tr>
<?php
foreach ($results as $vendor) {
?>
<tr class="category">
<td></td>
<td><?php echo $vendor['Category']; ?></td>
<td><?php echo $vendor['Vendor']; ?></td>
<td><?php echo $vendor['Address']; ?></td>
</tr>
<?php foreach ($productResults as $product) { ?>
<tr class="product">
<td colspan="4"><span class="name"><input type="text" name="quantities[]" size="1" /><?php echo $product['Product'];?></span></td>
</tr>
<?php } ?>
<td><input type="hidden" name="vendor[]" value="<?php echo $vendor['vendorID']; ?>"/></td>
<?php
}?>
</table>
<input type="submit" value="Checkout"<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
</div><?php
}
else {
?>
<form name="OrderForm" action="/confirm" method="POST">
<?php $query = 'SELECT * FROM Vendors Where costCenterID = 2';
$productquery = 'SELECT * FROM Products WHERE costCenterID = 2';
$results = $db->getAll($query);
$productresults = $db->getAll($productquery);?>
<table class="table">
<thead>
<tr>
<th>Quantity/Product</th>
<th>Category</th>
<th>Vendor</th>
<th>Address</th>
</tr>
<?php
foreach ($results as $vendor) {
?>
<tr class="category">
<td></td>
<td><?php echo $vendor['Category'];?></td>
<td><?php echo $vendor['Vendor'];?> </td>
<td><?php echo $vendor['Address'];?></td>
</tr>
<?php
foreach ($productresults as $product){
?>
<tr class="product">
<td colspan="4"><span class="name"><input type="text" name="quantities[<?php echo $vendor['vendorID']; ?>]" size="1" /><?php echo $product['Product'];?></span></td>
<td><input type="hidden" name="vendor[]" value="<?php echo $vendor['vendorID']; ?>"/></td>
</tr>
<?php
}
?>
<?php
}?>
</table>
<input type="submit" value="Checkout"<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
</div><?php
}
?>
This is the confirm page below.
<?php defined('C5_EXECUTE') or die("Access Denied.");
$db= Loader::db();
$quantity = $_POST['quantities'];
$vendor = $_POST['vendor'];
$minimumorder = 25;
foreach($quantity as $num){
if ($num >= $minimumorder){
echo "$num";
echo "</br>";
foreach($vendor as $vendors){
echo "$vendors";
echo "</br>";
}
}
}
?>
I appreciate any help anyone can give. This has had me stumped for a few days actually.
you might want to rearrange your array, and do something like:
$i = 0;
foreach ($productresults as $product) {
echo '<input name="product['.$i.'][quantity]" />';
echo '<input name="product['.$i.'][vendor_id]" value="'.$vendor['vendorID'].'" type="hidden" />';
++$i;
}
The resulting array in $_POST would have the quantities & their vendor separated into their own arrays.
In your code $vendor['vendorID'] seems the key of your $_POST['quantities'] so in your confirm page you could use:
foreach($quantity as $vendorid=>$num){
if ($num >= $minimumorder){
echo "$num";
echo "</br>";
echo "$vendorid";
}
}