php mysql shopping cart updating quantity error - php

I am new to coding and trying to update quantities in my cart, but it only accepts for one product.
If I add more than one, I can only update the last product, which will also set all the other products to the same quantity.
I need help separating the quantities.
Here is my code:
<form action="" method="post" enctype="multipart/form-data">
<table align="center" width="700" >
<tr align="center">
<td colspan="5"><h2>SHOPPING CART</h2></td>
</tr>
<tr align="center">
<th>Remove</th>
<th>Product (s)</th>
<th>Quantity</th>
<th> Price</th>
<th> Total Price</th>
</tr>
<?php
global $con;
$total = 0;
$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_id = $pp_price['product_id'];
$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 global $con; echo $pro_id; ?>"/></td>
<td><span style="color: white;"><?php echo $product_title; ?></span>
<br>
<img src="admin_area/product_images/<?php echo $product_image; ?>" width="100px" height="100px"></td>
<td><input type="number" size="4" name="qty" /></td>
<?php
global $con;
global $Stotal;
$Stotal = $single_price;
if(isset($_POST['update_quantity'])){
$qty = $_POST['qty'];
$update_qty = "update cart set qty='$qty' ";
$run_qty = mysqli_query($con, $update_qty);
$Stotal = $single_price * $qty;
}
?>
<td><?php echo "KSh. " . $single_price; ?></td>
<td><?php echo "KSh. " . $Stotal; ?></td>
</tr>
<?php }} ?>
<tr align="right">
<td colspan="5"><b>Total:</b> <?php echo "KSh. " . $total; ?></td>
</tr>
<tr align="center">
<td><input type="submit" name="update_cart" value="Update Cart" /></td>
<td><input type="submit" name="continue" value="Continue Shopping" /></td>
<td><input type="submit" name="update_quantity" value="Update Quantity" /></td>
<td><button> Checkout </button></td>
</tr>
</table>
</form>
<?php
global $con;
$ip = getIp();
if(isset($_POST['update_cart'])){
foreach($_POST['remove'] as $remove_id){
$delete_pro = "delete from cart where p_id = '$remove_id' and ip_add='$ip'";
$run_delete = mysqli_query($con, $delete_pro);
if($run_delete){
echo "<script>window.open('cart.php','_self')</script>";
}
}
}
if(isset($_POST['continue'])){
echo "<script>window.open('index.php','_self')</script>";
}
?>

Looks like you are naming HTML form controls as 'PHP arrays'. <input name="remove[]">
These form element names can only be alphanumeric, and on the server you can differentiate the elements by for instance postfixing the id of the product to the form control name and checking it there.
By the way, I recommend using $_SESSION for cart handling

Related

why am I getting a MySQl error

I am getting this error when I run the below code. I dont know why I am getting this error. I echoed the query to make sure that the syntax is correct I checked the query in a syntax checker to make sure that the query contains no syntax errors. and even tested in PHP my admin.
The query that is producing this error is the query that is in the for loop with numberofcolor and numberofsizes
Error description: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
<?php include('includes/db.php'); include('../functions/functions.php');?>
<?php require './p_struct/head.php';?>
<?php require './p_struct/header.php';?>
<?php require './p_struct/sidenav.php';?>
<div class="main" style="font-size: 20px">
<form action="insert_product.php" method="post" enctype="multipart/form-data" class="form-horizontal" id="chells">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="product_title" required="required"></td>
</tr>
<tr>
<td>category</td>
<td><select name="product_category" >
<option>Select a Category</option>
<?php
$get_cats = " select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while($row_cats = mysqli_fetch_array($run_cats))
{
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_data'];
echo "
<option vallue = '$cat_id' >$cat_title </option>
";
}
?>
</select></td>
</tr>
<tr>
<td>featured</td>
<td><select name="product_featured" >
<option>featured</option>
<option>not featured</option>
</select></td>
</tr>
<tr>
<td>price</td>
<td><input type="text" name="product_price" required="required"></td>
</tr>
<tr>
<td>image</td>
<td><input type="file" name="product_image" required="required"></td>
</tr>
<tr>
<td>big image</td>
<td><input type="file" name="product_big_image" required="required"></td>
</tr>
<tr>
<td>cart image</td>
<td><input type="file" name="product_cart_image" required="required"></td>
</tr>
<tr>
<td>keywords</td>
<td><input type="text" name="product_keywords" required="required"></td>
</tr>
<tr>
<td> Colors</td>
<td>
<div class="checkbox">
<?php
$get_colors = "SELECT * FROM `product_colors`";
$run_get_colors = mysqli_query($con, $get_colors);
while($row_get_color = mysqli_fetch_array($run_get_colors))
{
$colorS = $row_get_color['color'];
echo '<label class="checkbox-inline" ><input type="checkbox" name = "mycolor[]" value="'.$colorS.'" >'.$colorS.'</label>';
}
?>
</div>
</td>
</tr>
<tr>
<td> Sizes</td>
<td>
<div class="checkbox">
<?php
$get_sizes = "SELECT * FROM `product_sizes`";
$run_get_sizes = mysqli_query($con, $get_sizes);
while($row_get_sizes = mysqli_fetch_array($run_get_sizes))
{
$sizesS = $row_get_sizes['size'];
echo '<label class="checkbox-inline" ><input type="checkbox" name = "mysizes[]" value="'.$sizesS.'" >'.$sizesS.'</label>';
}
?>
</div>
</td>
</tr>
<tr>
<td>description</td>
<td><textarea name="product_description" cols="20" rows="5" ></textarea></td>
</tr>
<tr>
<td><input type="submit" value="Add Product" name="insert_post"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
global $con;
if( isset($_POST['insert_post']))
{
$a = rand(100, 999);
$b = rand(100, 999);
$product_id = $a.$b;
$product_title = $_POST['product_title'];
$product_category = $_POST['product_category'];
$product_featured = $_POST['product_featured'];
$product_price = $_POST['product_price'];
$product_keywords = $_POST['product_keywords'];
$product_description = $_POST['product_description'];
$color_array = $_POST['mycolor'];
$sizes_array = $_POST['mysizes'];
$product_image = $_FILES['product_image']['name'];
$product_big_image = $_FILES['product_big_image']['name'];
$product_cart_image = $_FILES['product_cart_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp_name'];
$product_big_image_tmp = $_FILES['product_big_image']['tmp_name'];
$product_cart_image_tmp = $_FILES['product_cart_image']['tmp_name'];
move_uploaded_file($product_image_tmp,"product_images/$product_image");
move_uploaded_file($product__big_image_tmp,"product_big_images/$product_big_image");
move_uploaded_file($product_cart__image_tmp,"product_cart_images/$product_cart_image");
$insert_product = " insert into products (product_id,product_cat, product_featured, product_title, product_price, product_desc,product_image,product_big_image,product_cart_image,product_keywords) values ($product_id,'$product_category','$product_featured','$product_title','$product_price','$product_description','$product_image','$product_big_image','$product_cart_image','$product_keywords')";
$run_insert_query = mysqli_query($con, $insert_product);
$number_of_colors = count($color_array);
for($i=0; $i <$number_of_colors; $i++)
{
$curr_color_value = $color_array[$i];
$add_color_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, 'color','$curr_color_value');";
$run_add_color_att = mysqli_query($con, $add_color_att);
/* if($run_add_color_att)
{
echo "COLOR ADDED!";
}
else {
echo "COLOR NOT ADDED!";
}*/
}
$number_of_sizes = count($sizes_array);
for($s=0; $s <$number_of_sizes; $s++)
{
$curr_size_value = $number_of_sizes[$s];
$add_size_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, `size`,`$curr_size_value`)";
$run_add_size_att = mysqli_query($con, $add_size_att);
/* if($run_add_size_att)
{
echo "SIZE ADDED!";
}
else {
echo "SIZE NOT ADDED!";
}*/
}
if($run_insert_query)
{
echo "PRODUCT ADDED!";
}
else {
echo "PRODUCT NOT ADDED!";
}
} ?>
</div>
$add_size_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, `size`,`$curr_size_value`)";
your using the incorrect quote type here the ` should be ' around the string values:
$add_size_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, 'size','$curr_size_value')";
also should size be $size?

PHP Fetches two rows but uploads file from last row only

Im trying to fetch records from database, then i have to upload a image which should be stored in folder and also link to be updated in table.. Everything working fine only with last row.. For first row its not updating.. Please help me where im goin on.. Below is my code
<?php
$email1 = $_SESSION['email'];
$Vendor_id = "SELECT Vendor_id FROM vendors where email = '$email1' ";
$result = mysqli_query($conn, $Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$cpid = $row['Vendor_wallet_id'];
$tid = $row['request'];
$type = $row['amount'];
$pays = $row['status'];
$amt = $row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td><input type="file" value="<?php echo $amt; ?>" name="fileToUpload" id="fileToUpload" ></td>
<td><input type="submit" value="Submit" name="submit" >
</button>
</td> </tr>
<?php } ?>
</tbody>
</table>
</form>
upload.php is taken from https://www.w3schools.com/php/php_file_upload.asp
on your form
<?php
$email1 = $_SESSION['email'];
$Vendor_id = "SELECT Vendor_id FROM vendors where email = '$email1' ";
$result = mysqli_query($conn, $Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$cpid = $row['Vendor_wallet_id'];
$tid = $row['request'];
$type = $row['amount'];
$pays = $row['status'];
$amt = $row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td><input type="file" name="fileToUpload []" class="fileToUpload" ></td>
<input type="hidden" value="<?php echo $amt; ?>" name="fileToUploadLink []" class="fileToUploadLink" >
<td><input type="submit" value="Submit" name="submit" >
</button>
</td> </tr>
<?php } ?>
</tbody>
</table>
</form>
on upload.php
<?php
if(isset($_FILES['fileToUpload']['tmp_name'])&&isset($_POST['fileToUploadLink'])){
for($i=0;$i<=(count($_FILES['fileToUpload']['tmp_name'])-1);$i++){
move_uploaded_file($_FILES['fileToUpload']['tmp_name'][$i],$_POST['fileToUploadLink'][$i]);
}
}
?>
I have a few suggestions:
In your "php.ini" file, search for the file_uploads directive, and set it to On: file_uploads = On
...</button><!-- <<-- How are you using this? -->
Could try to echo $amt; at the bottom of the loop to see if the path is correct.
May want to check if the $amt with IF Empty condition/statement and maybe check the condition of your other variables.
$email1 = $_SESSION['email'];
$Vendor_id="SELECT Vendor_id FROM vendors where email = '$email1' ";
$result=mysqli_query($conn,$Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$cpid=$row['Vendor_wallet_id'];
$tid=$row['request'];
$type=$row['amount'];
$pays=$row['status'];
$amt=$row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid;?></td>
<td><?php echo $tid;?></td>
<td><?php echo $type;?></td>
<td><?php echo $pays;?></td>
<td><input type="file" value="<?php echo $amt;?>" name="fileToUpload" id="fileToUpload" ></td>
<td><input type="submit" value="Submit" name="submit" >
</button> <!-- WHY IS THIS HERE? -->
</td></tr>
<?php
echo $amt; //See if it is correct path
}//END WHILE ?>
</tbody>
</table>
</form>
There are a few ways in which you could do the file upload, one of which would be to make the form a child of a single table cell and have the file field as a direct child of that form. The submit button, to keep the layout, would not be a child of the form and would need to be changed to a simple button input type then use javascript to submit the form
<?php
$email1 = $_SESSION['email'];
$Vendor_id = "SELECT Vendor_id FROM vendors where email = '$email1' ";
$result = mysqli_query($conn, $Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "select Vendor_wallet_id, total_amount, request, amount, status, amt_pdflink from vendor_wallet";
$query = mysqli_query($conn, $sql);
?>
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Transfer ID</th>
<th>Request</th>
<th>Amount</th>
<th>Status</th>
<th>Upload</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysqli_fetch_array( $query ) ) {
$cpid = $row['Vendor_wallet_id'];
$tid = $row['request'];
$type = $row['amount'];
$pays = $row['status'];
$amt = $row['amt_pdflink'];
?>
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" value="<?php echo $amt; ?>" name="fileToUpload" />
</form>
</td>
<td><input type="button" value="Submit" name="submit" /></td>
</tr>
<?php
}
?>
</tbody>
</table>
<script>
var bttns=Array.prototype.slice.call(document.querySelectorAll('input[type="button"][name="submit"]'));
bttns.forEach(function(bttn){
bttn.addEventListener('click',function(evt){
this.parentNode.parentNode.querySelector('form').submit();
}.bind(bttn),false );
});
</script>
Seems like You want to make a form per row.
<tr>
<td value="<?php echo $cpid; ?>" name="cpid"><?php echo $cpid; ?></td>
<td><?php echo $tid; ?></td>
<td><?php echo $type; ?></td>
<td><?php echo $pays; ?></td>
<td colspan="2">
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="Vendor_wallet_id" value="<?php echo $cpid;?>" />
<input type="file" value="<?php echo $amt; ?>" name="fileToUpload" />
<button>Submit</button>
</form>
</td>
</tr>
</form>
and remove <form> tag that wraps table

Html table only last row instead of all rows is inserting in mysql database

I am implementing clothing shopping website. I am having a problem when I place order only last row item from the shopping cart HTML table is inserted into the database and not all rows. I want that when I click on place order button all rows values should be inserted in the database. so that customers all selected items in the shopping cart should be placed for order.
Here is the code,
<table align="center" >
<thead>
<tr >
<th>Product</th>
<th>Price</th>
<th>QTY</th>
<th>SubTotal</th>
</tr>
</thead>
<?php
if (!empty($_SESSION["shopping_cart"]))
{
$total=0;
foreach ($_SESSION["shopping_cart"] as $keys => $values) {
?>
<tr>
<td>
<p>Product Code:<?php echo $values["item_id"]; ?> <br/>
<?php echo $values["item_gender"]; ?><br/>
<?php echo $values["item_description"]; ?> </p></td>
<td>PKR
<input type="number" name="price1" id="price1" value="<?php echo $values["item_price"];?>" readonly ></td>
<td><input type="number" name="qty[<?php echo $values["item_id"]; ?>]" id="qty" value="<?php echo $values["item_quantity"];?>"></td>
<input type="hidden" name="qty" value="<?php echo $values["item_quantity"];?>" >
<td >PKR<input type="number" name="total" id="total" value="<?php echo ($values["item_quantity"] * $values["item_price"]) ?>"readonly></td>
</tr>
<?php
$total=$total+($values['item_quantity']*$values["item_price"]);
/* $total=$total+$values["item_price"]; */
}
} ?>
</table> <br/><br/>
<button type="submit" name="submit" class="btn btn-primary btn-lg">Place Order</button>
here is php code,
if (isset($_POST['submit']))
{
$product_code = $_POST['ID'];
$price = $_POST['grandtotal'];
$quantity = $_POST['qty'];
$description = $_POST['description'];
$email=$_SESSION["email"];
$con=mysql_connect("localhost", "root", "");
mysql_select_db("login",$con);
$qry="INSERT INTO order ( order_description , product_code, order_quantity, order_price, customer_name, email, customer_id) VALUES ('$description', '$product_code', '$quantity', '$price', (SELECT name from users where email='$email'), '$email', (SELECT user_id from users where email='$email') ) ";
$result=mysql_query($qry,$con);
if($result)
{
echo '<script>alert("Your order has been placed")</script>';
echo '<script>window.location="portfolionew.php"</script>';
} else {
die("Error While Adding Stock ! Please Try Again .");
}
}
<table align="center">
<thead>
<tr >
<th>Product</th>
<th>Price</th>
<th>QTY</th>
<th>SubTotal</th>
</tr>
</thead>
<?php
if (!empty($_SESSION["shopping_cart"]))
{
$total=0;
$i = 1;
foreach ($_SESSION["shopping_cart"] as $keys => $values) {
?>
<tr>
<td style=" text-align: left;" >
<p style="margin-top: -120px; margin-left: 150px;">Product Code:<?php echo $values["item_id"]; ?> <br/>
<?php echo $values["item_gender"]; ?><br/>
<?php echo $values["item_description"]; ?> </p></td>
<td style="text-align: left; width: 80px;">PKR<br/>
<input style="width: 70px;" type="number" name="price<?= $i; ?>" id="price<?= $i; ?>" value="<?php echo $values["item_price"];?>" readonly ></td>
<td><input type="number" name="qty_<?= $i; ?>" id="qty_<?= $i; ?>" value="<?php echo $values["item_quantity"];?>"></td>
<td style="text-align: left; width: 80px;"> <b>PKR</b>
<br/> <input style="width: 70px; " type="number" name="total" id="total" value="<?php echo ($values["item_quantity"] * $values["item_price"]) ?>"readonly></td>
</tr>
<?php
$total=$total+($values['item_quantity']*$values["item_price"]);
$i++;
/* $total=$total+$values["item_price"]; */
}
} ?>
<input hidden="iTotalProducts" value="<?php echo $i; ?>" name="product_count">
</table> <br/><br/>
<button type="submit" name="submit" class="btn btn-primary btn-lg">Place Order</button>
//here is php code,
if (isset($_POST['submit']))
{
$product_code = $_POST['ID'];
$price = $_POST['grandtotal'];
$quantity = $_POST['qty'];
$description = $_POST['description'];
$email=$_SESSION["email"];
$con=mysql_connect("localhost", "root", "");
mysql_select_db("login",$con);
$cnt = $_POST["iTotalProducts"];
for($i = 1; $i <= $cnt; $i++) {
// here you can write your insert code
}
if($result)
{
echo '<script>alert("Your order has been placed")</script>';
echo '<script>window.location="portfolionew.php"</script>';
} else {
die("Error While Adding Stock ! Please Try Again .");
}
} `enter code here`

Not removing item from the cart

Below is my php code to show how I removing items from the cart. There is not error showing in webpage but also the item is not removing and cart is not updating. Edited with full code
<div class="product_box">
<form action="cart.php" method="post" enctype="multipart/data">
<table align="center" width="700" bgcolor="skyblue">
<tr>
<th>Remove</th>
<th>Product (s) </th>
<th>Quantity</th>
<th>Total Price</th>
</tr>
<?php
$total = 0;
global $db;
$ip = getIp();
$sel_price ="select * from cart where ip_add='$ip'";
$run_price = mysqli_query($db, $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($db, $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_img1'];
$single_price = $pp_price['product_price'];
$values = array_sum($product_price);
$total +=$values;
//echo "Rs ." . $total;
?>
<tr align="center">
<td><input type="checkbox" name="remove[]"></td>
<td><?php echo $product_title; ?><br>
<img src="admin_area/product_images/<?php echo $product_image;?>" width="50px" height="50px">
</td>
<td><input type="text" name="qty" size="3"></td>
<td><?php echo "Rs." . $single_price ?></td>
</tr>
<?php } } ?>
<tr align="right">
<td colspan="3">
<b> Sub Total: </b>
</td>
<td>
<?php echo "Rs." .$total; ?>
</td>
</tr>
<tr align="center">
<td colspan="1"><input type="submit" name="update_cart" value="Update Cart"></td>
<td><input type="submit" name="continue" value="Continue Shopping"></td>
<td><button>Checkout</button></td>
</tr>
</table>
</form>
<?php
$ip = getIp();
if (isset($_POST['update_cart'])) {
foreach ($_POST['remove'] as $remove_id) {
$delete_product = "delete from cart where p_id=".$remove_id." AND ip_add=".$ip;
$run_delete = mysqli_query($db, $delete_product);
if ($run_delete) {
echo "<script>window.open('cart.php','_self')</script>";
}
}
}
}
?>
</div>
</div>
I tried to check error by putting below code after if statement but not worked.
else { echo mysqli_error($db);}
Please help Thanks.
Try using writelog to check whether your mysql statements are getting values. Here is small function which will help you better in debugging.
<?php
function writelog($content) {
$file = 'log.txt';
file_put_contents($file, date("Y-m-d H:i:s")." : ".$content."\r\n", FILE_APPEND | LOCK_EX);
}
//Only display php errors to the developer...
if($_SERVER['REMOTE_ADDR'] == "put your localhost ip address")
{
error_reporting(0);
ini_set('display_errors', 'On');
$db_hostname = "localhost";
$db_database = "ecommerce";
$db_username = "root";
$db_password = "";
}
else
{
error_reporting(0);
ini_set('display_errors', 'On');
$db_hostname = "localhost";
$db_database = "ecommerce";
$db_username = "root";
$db_password = "";
}
//Establishing database connection
$db_connection = mysqli_connect($db_hostname, $db_username, $db_password, $db_database) or die(mysqli_error($db_connection));
Example how to use this function:
write_log($delete_product);
This will create a text file where you have your current php file.
Learn a complete CRUD php mysqli example from this website:
try this it will help you in understanding better
I think you should in the line with checkbox
<tr align="center">
<td><input type="checkbox" name="remove[]"></td>
<td><?php echo $product_title; ?><br>
<img src="admin_area/product_images/<?php echo $product_image;?>" width="50px" height="50px">
</td>
<td><input type="text" name="qty" size="3"></td>
<td><?php echo "Rs." . $single_price ?></td>
</tr>
You should set a value = product_id in checkbox like
<input type="checkbox" name="remove[]" id="<?=$pp_price['product_id']?>>
I got my answer i just added below value to input checkbox and it works.
<input type="checkbox" name="remove[]" value="<?php echo $pro_id; ?> ">
try this it will help you
$ip = getIp();
if (isset($_POST['update_cart'])) {
foreach ($_POST['remove'] as $remove_id) {
$delete_product = "delete from cart where p_id=".$remove_id." AND ip_add=".$ip;
$run_delete = mysqli_query($db, $delete_product);
if ($run_delete) {
echo "<script>window.open('cart.php','_self')</script>";
}
}
}

Form Select List to show Product Names from Mysql Database

I dont get what im doing wrong here..
i have about 125 product in the table but i get only the last product from the table so it shows only one item... this is a simple calculator to provide the sales person and the customer how much box they would need and how much it would cost a quick estimate.
Thank you for the help in advance..
<?php
include('admincik/config.php');
include ('birlikte/ac.thumbs.php');
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM Calculator ORDER BY isim") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
$resim = $info['resim'];
$isim = $info[isim];
$boyut = $info[boyut];
$pcs = $info[adet];
$sqft = $info[sqft];
$price = $info[price];
}
/////////Formdan gelen yada Giden//////////////
$length =htmlspecialchars(stripslashes($_POST['Length']));
$width =htmlspecialchars(stripslashes($_POST['Width']));
$TileNameList = "<option value=\"$sqft\">$isim $boyut</option>";
/////Matematiksel islemler/////////
$equals = $length * $width;
$box = round($equals / $sqft);
$sqftbox = $box * $sqft;
$TotalPrice = $sqftbox * $price
?>
<div class="ana">
<table width="900" height="199" border="1">
<tr>
<td width="150">Name</td>
<td width="150">Length</td>
<td width="150">Width</td>
<td width="150">Total Sqft Area</td>
<td width="200">Box Needed /Total Sqft</td>
<td width="100">Price</td>
</tr>
<tr>
<td><form id="form5" name="form5" method="post" action="">
<select name="TileName" id="TileName">
<?php echo ($TileNameList); ?>
</select>
</td>
<td><input name="Length" type="text" id="Length"/></td>
<td><input type="text" name="Width" id="Width"/></td>
<td><input type="text" name="Sqft" id="Sqft" value="<?php echo ($equals); ?>"/></td>
<td><?php echo "You will need <span style=\"color:red\">$box</span> Boxes<br> Which is <span style=\"color:red\">$sqftbox</span> "; ?></td>
<td><?php echo "$$TotalPrice"; ?></td>
</tr>
<tr >
<td colspan="6" align="center">
<input type="submit" name="Submit" id="Submit" value="Submit" />
</form></td>
</tr>
</table>
</div>
Your loop doesn't append to $TileNameList because it exists outside of it. It actually replaces it's value. Try:
<?php
include('admincik/config.php');
include ('birlikte/ac.thumbs.php');
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM Calculator ORDER BY isim") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
$resim = $info['resim'];
$isim = $info[isim];
$boyut = $info[boyut];
$pcs = $info[adet];
$sqft = $info[sqft];
$price = $info[price];
$TileNameList .= "<option value=\"$sqft\">$isim $boyut</option>"; // NOTE THE .=
}
/////////Formdan gelen yada Giden//////////////
$length =htmlspecialchars(stripslashes($_POST['Length']));
$width =htmlspecialchars(stripslashes($_POST['Width']));
/////Matematiksel islemler/////////
$equals = $length * $width;
$box = round($equals / $sqft);
$sqftbox = $box * $sqft;
$TotalPrice = $sqftbox * $price
?>
<div class="ana">
<table width="900" height="199" border="1">
<tr>
<td width="150">Name</td>
<td width="150">Length</td>
<td width="150">Width</td>
<td width="150">Total Sqft Area</td>
<td width="200">Box Needed /Total Sqft</td>
<td width="100">Price</td>
</tr>
<tr>
<td><form id="form5" name="form5" method="post" action="">
<select name="TileName" id="TileName">
<?php echo ($TileNameList); ?>
</select>
</td>
<td><input name="Length" type="text" id="Length"/></td>
<td><input type="text" name="Width" id="Width"/></td>
<td><input type="text" name="Sqft" id="Sqft" value="<?php echo ($equals); ?>"/></td>
<td><?php echo "You will need <span style=\"color:red\">$box</span> Boxes<br> Which is <span style=\"color:red\">$sqftbox</span> "; ?></td>
<td><?php echo "$$TotalPrice"; ?></td>
</tr>
<tr >
<td colspan="6" align="center">
<input type="submit" name="Submit" id="Submit" value="Submit" />
</form></td>
</tr>
</table>
</div>

Categories