I already search more about this topic solution "How to insert multiple item into database", but i can't understand. Hope somebody can help me !
shopping cart
{
while($row=mysqli_fetch_array($res)){
$total=($row['product_price']*$row['quantity']);
$tot = $tot + ($row['product_price']*$row['quantity']);
?>
<tr>
<td align="center"><img src="<?php echo $item['product_image'];?>" width="150px" height="150px" alt=""/></td>
<td><?php echo $row['product_name'];?></td>
<td><?php echo $row['quantity'];?></td>
<td>RM <?php echo $total;?></td>
<td style="text-align:center"><a href="delete_shopping_cart_item.php?id=<?php echo $row["id"];?>" onclick="return confirm('Are you sure you want to delete this item?');">
Remove
</a></td>
</tr>
<?php
}
if(isset($_POST['submit1']))
{
$customer = $_SESSION['login_user'];
$res = mysqli_query($mysqli,"SELECT * FROM shopping_cart where customer ='$customer'");
$row=mysqli_fetch_array($res);
$qty = $row['quantity'];
$product_name = $row['product_name'];
$rname = $_POST['rname'];
$raddress = $_POST['raddress'];
$rcontactno = $_POST['rcontactno'];
$total = $_SESSION['total'] ;
mysqli_query($mysqli,"insert into confirm_order (product_name,quantity,total_price,receiver_name,receiver_address,receiver_contact,customer)
values('$product_name','$qty','$total','$rname','$raddress','$rcontactno','$customer')");
$_SESSION['receiver'] = $rname;
}
}
Try this:
while ($row=mysqli_fetch_array($res)) {
$qty = $row['quantity'];
$product_name = $row['product_name'];
$rname = $_POST['rname'];
$raddress = $_POST['raddress'];
$rcontactno = $_POST['rcontactno'];
$total = $_SESSION['total'] ;
mysqli_query($mysqli,"insert into confirm_order (product_name,quantity,total_price,receiver_name,receiver_address,receiver_contact,customer)
values('$product_name','$qty','$total','$rname','$raddress','$rcontactno','$customer')");
}
Related
I am trying to create a shopping cart and take the quantities in the cart to subtract from my inventory in my table in mysql. But everytime i try and update it keeps on subtracting the wrong amount. So far i have this.
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<center><B>Invoice</B></center>
<?php
require 'connect.php';
require 'item.php';
if(isset($_GET['id'])){
$result = mysqli_query($con, 'select * from products where
id='.$_GET['id']);
$products = mysqli_fetch_object($result);
$item = new Item();
$item->id = $products->id;
$item->name = $products->name;
$item->price = $products->price;
$item->quantity = 1;
// Check if the products exists in the cart
$index = -1;
$cart = unserialize(serialize($_SESSION['cart']));
for($i=0; $i<count($cart); $i++)
if($cart[$i]->id==$_GET['id'])
{
$index = $i;
break;
}
if($index==-1)
$_SESSION['cart'][] = $item;
else{
$cart[$index]->quantity++;
$_SESSION['cart'] = $cart;
}
}
// Delete products in cart
if(isset($_GET['index'])){
$cart = unserialize(serialize($_SESSION['cart']));
unset($cart[$_GET['index']]);
$cart = array_values($cart);
$_SESSION['cart'] = $cart;
}
?>
<table cellpadding="3" cellspacing="3" border="0">
<tr>
<th>Id</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub-Total</th>
</tr>
<?php
$cart = unserialize(serialize($_SESSION['cart']));
$s = 0;
$index = 0;
for($i=0; $i<count($cart); $i++){
?>
<tr>
<td><?php echo $cart[$i]->id; ?></td>
<td><?php echo $cart[$i]->name; ?></td>
<td><?php echo $cart[$i]->price; ?></td>
<td><?php echo $cart[$i]->quantity; ?></td>
<td><?php echo $cart[$i]->price * $cart[$i]->quantity; ?></td>
</tr>
<?php
$index++;
$quantitycart=$cart[$i]->quantity;
$idcart=$cart[$i]->id;
}
?>
<td><input type="submit" name="submit" value="submit"></td></tr>
<tr><td colspan="5"</tr>
<tr>
</tr>
</table><br>
<?php
if(array_key_exists('submit', $_POST))
{ $results = mysqli_query($con, 'select * from products');
for($i=0; $i<count($cart); $i++)
{
while($products= mysqli_fetch_object($results)){
$quantity= $products->quantity;
$newquantity= $quantity-$quantitycart;
$sql = "UPDATE products SET quantity='$newquantity' WHERE id='$idcart'";
}}
if ($con->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $con->error;
}
$con->close();
}
$con is my connnection. Any help would be appreciated. The main problem is located in the last part of the code.
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 wanted to retrieve the proid thats in the session.
when i add to cart rdy to check out .
The proid seems cannot to be retrieve which show empty.
but it did count the item that i entered.
cause when i add 2 item in cart , it display 2 row.
please help me.
this is cart.php
if (isset($_SESSION['cart']))
{
$total = 0;
foreach($_SESSION['cart'] as $proid =>$x)
{
$results = mysqli_query($con,"Select * from product where Product_ID = $proid");
$myrow = mysqli_fetch_array($results);
$proid = $myrow['Product_ID'];
$productname = $myrow['Product_Name'];
$productprice= $myrow['Product_Price'];
$line_cost = $productprice * $x;
$total=$total+$line_cost;
?>
<li class = "cartlist">
<?php echo '<img src="data:image/jpeg;base64,' . base64_encode($myrow['Product_Pic']) . '" width="196" height="120">';?>
<p><span style = "font-weight: bold; font-size: 1.2em;">
<?php echo $productname ?>
</span></br />RM <?php echo $productprice;?>
</br />Quantity : <?php echo $x ?></br><a href='cart.php?proid=<?php echo $proid;?>&action=add'>Add </a> <a href ='cart.php?proid=<?php echo $proid;?>&action=remove'> Reduce </a></br>RM<?php echo $line_cost; ?> <br/>
</p> </li>
<?php
}
This is payment.php
i wanted to retrieve the proid and display the product name and total to the user.
print_r($_SESSION);
if (isset($_SESSION['cart']))
{
$total = 0;
foreach($_SESSION['cart'] as $proid =>$x)
{
$results = mysqli_query($con,"Select * from product where Product_ID = $proid");
$myrow = mysqli_fetch_array($results);
$productname = $myrow['Product_Name'];
$productprice= $myrow['Product_Price'];
$line_cost = $productprice * $x;
$total=$total+$line_cost;
?>
<tr>
<td><? echo $productname ?></td>
<td><? echo $x ?></td>
<td><? echo $line_cost ?></td>
</tr>
<?php
}
}
After adding a print_r($_SESSION['cart']); to the top of payment.php I get this output
Array ( [36] => 3 )
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;
}