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!
Related
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>
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 have a form where I'm submitting some values (from checkboxes) to another page and on that page I'm using an explode function to break the string inside the array. But I'm getting an additional (+1) value when I put the count() function on the explode.
HTML
<form name = "view" method = "POST" action ="cart.php">
<table align = 'center' width = '100%' border = '4'>
<tr>
<td colspan = '20' align = 'center'><h2> Viewing all the Products </h2></td>
</tr>
<tr align = 'center'>
<th>Item ID</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
</tr>
<tr align = 'center' class = "odd">
<?php
while($row = mysql_fetch_array($run))
{
$i_id = $row['item_id'];
$i_name = $row['item_name'];
$i_price = $row['item_price'];
?>
<td><?php echo $i_id; ?></td>
<td><?php echo $i_name; ?></td>
<td><?php echo $i_price; ?></td>
<?php
$item = $i_name ." ". $i_price;
?>
<td><input type="checkbox" name="addcart[]" value="<?php echo $item; ?>" onClick="return KeepCount()" />Tick</td>
</tr>
<?php }?><input type = "hidden" name = "check">
<button type= "submit" onclick = "location.href = 'cart.php';" id = "cart">Add to Cart</button> <?php } ?>
</table>
</form>
PHP (on page 2)
$prd = implode(",",$_POST['addcart']);
$final = explode(",", $prd);
for($i=0; $i<=count($final); $i++)
{
echo count($final); //this is where I'm getting the +1 to original count and hence everything falls apart.
echo $final[$i];
}
Note: I have include all the essential files like config.php and everything in the PHP file already.
Why that much extra code, directly do like below:-
foreach($_POST['addcart'] as $val){
echo $val;
}
Since count() starts counting from 1 and for loop starting from 0.
Replace this line:
for($i=0; $i<=count($final); $i++)
with
for($i=0; $i<=count($final)-1; $i++)
I have products on a loop in a shopping cart, but the values of qty is not appearing it have this error on every textbox. Notice: Array to string conversion in C:\xampp\htdocs\ecommerce\cart.php on line 172Array
<?php
$total = 0;
global $con;
$ip = getIp();
$sel_price = "select * from cart where ip_add='$ip'";
$run_price = mysqli_query($con, $sel_price);
while($p_price=mysqli_fetch_array($run_price)){
$pro_id = $p_price['p_id'];
$pro_price = "select * from products where product_id='$pro_id'";
$run_pro_price = mysqli_query($con,$pro_price);
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'];
$single_price = number_format($single_price);
$values = array_sum($product_price);
$total += $values;
$pro_qty = "select * from cart where p_id='$pro_id'";
$run_pro_qty = mysqli_query($con,$pro_qty);
while ($pp_qty = mysqli_fetch_array($run_pro_qty)){
$product_qty = array($pp_qty['qty']);
?>
<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_images/<?php echo $product_image;?>" width="60" height="60"/>
</td>
<td><input type="text" size="4" name="qty" value="<?php echo $product_qty;?>"/></td>
<td><?php echo "Php " . $single_price; ?></td>
</tr>
<?php } }?>
I'm trying to display my products from the database. I'm trying to split it using the if 4%=0, but I can't get it to display 4 items in a row.
Here is my codes:
<table>
<tr>
<?php
include "connect_to_mysql.php";
$split = 0;
$display_all = mysql_query("SELECT * FROM products
ORDER BY FIELD(category, 'ipad','iphone','others')");
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
$split++;
if ($split%4==0){
echo '</tr><tr>';
}
?>
<td>
<table class="normal_text" align="center">
<tr>
<td><a href="product.php?product_id=<?php echo $id?>">
<img width="200px" height="133px" src="products/<?php echo $product_code?>.jpg" />
</a></td>
</tr>
<tr>
<td align="center" style="font-weight:bold"><?php echo $title;?></td>
</tr>
<tr>
<td align="center">$<?php echo $price;?></td>
</tr>
</table>
</td>
<?php
}
?>
</tr>
</table>
You've got the PHP logic before, rather than inside your HTML table output.
Try reorganizing like this:
<?php
include "connect_to_mysql.php";
$split = 0;
$display_all = mysql_query("SELECT * FROM products
ORDER BY FIELD(category, 'ipad','iphone','others')");
?>
<table class="normal_text" align="center">
<tr>
<?php
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
// output product details -- note use of quotes to include PHP vars
$rowHTML = "<td><a href='product.php?product_id=$id'>";
$rowHTML .= "<img width='200px' height='133px' src='products/$product_code.jpg' />";
$rowHTML .= "</a><br/>";
$rowHTML .= "<strong>$title</strong>";
$rowHTML .= "$price</td>";
echo $rowHTML;
$split++;
if ($split%4==0){
echo '</tr><tr>';
}
}
?>
</tr>
</table>
while($row=mysql_fetch_assoc($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
if ($split % 4 === 0) echo "\n<tr>";
?>
// ONLY <td> CODE </td> here. NO <tr> and NO </table>
<?php
if ($split % 4 === 3) echo "</tr>";
$split++;
}
?>
You are starting and ending at same time and then putting table for each.
You want to start <tr> when %4==0 and end </tr> when %4==3.
Rather than use modulus (%), just add a second variable called $cols, assign it to the amount of columns you want, and compare it. For example:
$rows = 1;
$cols = 4;
$display_all = mysql_query("SELECT * FROM products ORDER BY FIELD(category, 'ipad','iphone','others')");
while($row=mysql_fetch_array($display_all)) {
$id = $row['id'];
$product_code = $row['product_code'];
$title = $row['title'];
$price = $row['price'];
$split++;
if ($rows==$cols){
echo '</tr><tr>';
$rows = 1;
}