PHP is giving me error of
Notice: Undefined index: product_id in C:\xampp\htdocs\odieinventory\admin\add_sales.php on line 6
This is my query
<?php
if (isset($_POST['addCart'])) {
$product_id = $_POST['product_id'];
$qtyBuy = $_POST['qtyBuy'];
$addQuery = "INSERT INTO sales (product_id, quantity) VALUES ($product_id, $qtyBuy)";
$execQuery = mysqli_query($connection, $addQuery);
}
?>
This is my table
<form action="add_sales.php" method="POST">
<?php
$query = "SELECT * FROM products";
$exec = mysqli_query($connection, $query);
while ($row = mysqli_fetch_array($exec)) {
$product_id = $row['product_id'];
$product_name = $row['product_name'];
$description = $row['description'];
$product_quantity = $row['quantity'];
$product_price = $row['sell_price'];
?>
<tr>
<td class="text-center"><?php echo $product_id; ?>
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
</td>
<td><?php echo $product_name; ?></td>
<td><?php echo $description; ?></td>
<td><?php echo $product_price; ?></td>
<td><?php echo $product_quantity; ?></td>
<td><input type="number" min="1" max="999" name="qtyBuy"></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<div class="form-group">
<input type="submit" name="addCart" value="Add Items to Cart" class="btn btn-info pull-right">
</div>
</div>
</form>
I would also like to ask for advice on how to insert records on my cart. By clicking 'Add Items to Cart', I like to pass ONLY the items with values on the quantitiy input on the cart. How to achieve this with PHP?
In your table file table.php , you have to:
1- open the table and tbody HTML tags
2- make your html attributes form name as array (you have more than one input with the same name), in your case you are only submiting the last product_id and the last qtyBuy input values.
table.php
<form action="add_sales.php" method="POST">
<div><table><tbody>
<?php
$query = "SELECT * FROM products";
$exec = mysqli_query($connection, $query);
while ($row = mysqli_fetch_array($exec)) {
$product_id = $row['product_id'];
$product_name = $row['product_name'];
$description = $row['description'];
$product_quantity = $row['quantity'];
$product_price = $row['sell_price'];
?>
<tr>
<td class="text-center"><?php echo $product_id; ?>
<input type="hidden" name="product_id[]" value="<?php echo $product_id; ?>">
</td>
<td><?php echo $product_name; ?></td>
<td><?php echo $description; ?></td>
<td><?php echo $product_price; ?></td>
<td><?php echo $product_quantity; ?></td>
<td><input type="number" min="1" max="999" name="qtyBuy[]"></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<div class="form-group">
<input type="submit" name="addCart" value="Add Items to Cart" class="btn btn-info pull-right">
</div>
</form>
In your add_sales.php file, you have to loop the $_POST['qtyBuy'] variable and check if the given quantity of each product is > 0
add_sales.php
<?php
if (isset($_POST['addCart']) && $_POST['addCart']=="Add Items to Cart") {
foreach($_POST['qtyBuy'] as $index=>$value){
if($value > 0){
$addQuery = "INSERT INTO sales (product_id, quantity) VALUES (".$_POST['product_id'][$index].", ".$value.")";
$execQuery = mysqli_query($connection, $addQuery);
}
}
}
?>
First of all, you should never put users input ($_POST) directly into the SQL to avoid SQL injection - use prepared statements with mysqli.
It would be easiest for you to add a separate form with product ID and quantity field and an "Add to cart" button for each product in the table.
Related
Hi all,
I am trying to populate a form from MySQL using a dropdown menu to select the row I want displayed.
The dropdown is displaying the items I need. But what I want to do is select one of the items in the dropdown, which will fill in the form with the items I need for editing.
Here is the code I have so far, including the form. Any help you guys can give would be really appreciated.
The Select:
<div class="panel panel-primary">
<div class="panel-heading"> Manage Tours</div>
<div class="panel-body">
<form role="form" method="post" action="">
<label for="singleSelect">Choose Tour to Edit</label>
<select class="form-control" name="tour_select">
<?php
$result = mysqli_query($mysqli,"SELECT tour_name FROM tours order by id");
while($row = mysqli_fetch_array($result))
echo "<option value='" . $row['tour_name'] . "'>" . $row['tour_name'] . "</option>";
?>
</select>
This works fine.
The form, which also works fine with a simple select query:
<div class="form-group">
<label>Tour Name</label>
<input class="form-control" name="tour_name" value="<?php echo $row['tour_name']; ?>" />
</div>
<div class="form-group">
<label>Destination</label>
<textarea class="form-control" name="tour_to" rows="4"><?php echo $row['tour_to']; ?></textarea>
</div>
<div class="form-group">
<label>Collection</label>
<textarea class="form-control" name="tour_from" rows="4"><?php echo $row['tour_from']; ?></textarea>
</div>
<div class="form-group">
<label>Date</label>
<input class="form-control" name="tour_date" value="<?php echo $row['tour_date']; ?>" />
</div>
<div class="form-group">
<label>Pickup Time</label>
<input class="form-control" name="tour_time" value="<?php echo $row['tour_time']; ?>" />
</div>
<div class="form-group">
<label>Itinerary</label>
<textarea class="tinymce" id="tinymce" name="tour_details" rows="12" ><?php echo $row['tour_details']; ?></textarea>
</div>
<button type="submit" name="Submit" class="btn btn-primary">Save Changes</button>
</form>
But, when I select an option from the dropdown, I need the form to be populated with the data from that database table row. I hope I am making sense.
I have searched for days on Google but found nothing.
I have changed the form from a dropdown to an html table with a Delete button.
Here is the code with table:
<?php
if (isset($_GET['id'])) {
if (isset($_POST['Delete'])) {
$remove = $mysqli->prepare("DELETE FROM `tours` WHERE `id` = $id");
$id = $_POST['id'];
$remove->bind_param('ssssss', $id);
if(!$remove->execute() === true) {
echo $mysqli->error;
}
}
}
$sql = "SELECT * FROM tours";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['tour_name']; ?></td>
<td><?php echo $row['tour_to']; ?></td>
<td><?php echo $row['tour_from']; ?></td>
<td><?php echo $row['tour_date']; ?></td>
<td><?php echo $row['tour_time']; ?></td>
<td><input type="submit" name="Delete" value="Delete" onclick="" /></td>
</tr>
</tbody>
<?php
}
?>
</table>
</form>
I appreciate any help.
Say you want to preselect a particular tour with $tour_id = 5. You can then modify your code to:
echo "<option value='" . $row['tour_name'] . "'" . ($row['id'] == $tour_id ? " selected" : "") . ">" . $row['tour_name'] . "</option>";
I have found the solution should anyone need it in future.
<?php
if (isset($_POST['Delete'])){
$checkbox = $_POST['checkbox'];
$count = count($checkbox);
for($i=0;$i<$count;$i++){
if(!empty($checkbox[$i])){ /* CHECK IF CHECKBOX IS CLICKED OR NOT */
$id = mysqli_real_escape_string($mysqli,$checkbox[$i]); /* ESCAPE STRINGS */
mysqli_query($mysqli,"DELETE FROM tours WHERE id = '$id'"); /* EXECUTE QUERY AND USE ' ' (apostrophe) IN YOUR VARIABLE */
} /* END OF IF NOT EMPTY CHECKBOX */
} /* END OF FOR LOOP */
} /* END OF ISSET DELETE */
$sql = "SELECT * FROM tours";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id = mysqli_real_escape_string($mysqli, $row['id']);
?>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['tour_name']; ?></td>
<td><?php echo $row['tour_to']; ?></td>
<td><?php echo $row['tour_from']; ?></td>
<td><?php echo $row['tour_date']; ?></td>
<td><?php echo $row['tour_time']; ?></td>
<?php echo "<td><input type='checkbox' name='checkbox[]' value='$id'></td>"; ?>
</tr>
</tbody>
<?php
}
?>
Thanks all for the help. Really appreciate it.
Im new in php. this problem stuck me for two days. ergh. I want to display a table that contain input field so that user can insert the data and update it into database. User can change all the data in the table. I want to update multiple rows at a time, but it ends up updating only 1 row (the last row). Anyone please help me for this. Thnks.
This are the form.
This are the following code.
[<table>
<thead>
<tr>
<td><b>Item Code</b></td>
<td><b>Item Barcode</b></td>
<td><b>Item</b></td>
<td><b>QOH</b></td>
<td><b>Quantity</br>Checked</b></td>
<td><b>Quantity</br>Order</b></td>
</tr>
</thead>
<?php
$Barcode=$_SESSION\["Barcode"\];
$code=$_SESSION\["Code"\];
$itemcode=$_SESSION\["Itemcode"\];
$guid=$_SESSION\["guid"\];
$sql = "SELECT itemmastersupcode.*, itembarcode.*, stock_count_item.*, stock_count.*, po_ex_c.*, d.itemlink_total_qty
FROM itemmastersupcode
WHERE stock_count.SupCode = '$code' and stock_count_item.TRANS_GUID = '$guid'
GROUP BY itemmastersupcode.Itemcode";
$result=mysqli_query($conn2,$sql);
$rowcount = mysqli_num_rows($result);
while($row = mysqli_fetch_assoc($result))
{
?>
<tbody>
<tr>
<td><?php echo $row\["Itemcode"\]; ?></td>
<td><?php echo $row\["Barcode"\]; ?></td>
<td><?php echo $row\["Description"\]; ?></td>
<td><?php echo $row\["itemlink_total_qty"\]; ?></td>
<td><?php echo $row\["qty"\]; ?></td>
<form class="form-inline" role="form" method="POST" id="myForm">
<td><input type="number" name="qty" value=""/></td>
</tr>
</tbody>
<input type="hidden" name="itemcode" value="<?php echo $row\["itemcode"\]; ?>"/>
<input type="hidden" name="supcode" value="<?php echo $_SESSION\["Code"\] ?>"/>
<input type="hidden" name="guid" value="<?php echo $_SESSION\["guid"\] ?>"/>
<?php
}
?>
</table>
<button name="save" type="submit" ><b>SUBMIT</b></button>
</form>
<?php
if (isset($_POST\["save"\]))
{
$itemcode=$_POST\['itemcode'\];
$qty=$_POST\['qty'\];
$supcode=$_POST\['supcode'\];
$guid=$_POST\['guid'\];
$sql = "UPDATE stock_count, stock_count_item SET stock_count.posted = '1', stock_count_item.qty_order = '$qty'
WHERE stock_count.TRANS_GUID = '$guid' AND stock_count_item.Itemcode ='$itemcode'
and stock_count_item.TRANS_GUID = '$guid' ";][1]
An UPDATE query will update all records that are filtered by the WHERE clause. If no WHERE is given, all records are updated:
UPDATE table1 SET field1='value1';
will set the field1 column of all records to value1.
UPDATE table1 SET field1='value1' WHERE field2='value2';
will set the field1 column to value1 or all records where field2 is equal to value2.
So, in your case, the records that are found through the WHERE clause are updated. This is all basic SQL stuff by the way, so I advise you to read up on SQL.
Finally, also use prepared statements in your code to prevent SQL injection.
<table>
<thead>
<tr>
<td><b>Item Code</b></td>
<td><b>Item Barcode</b></td>
<td><b>Item</b></td>
<td><b>QOH</b></td>
<td><b>Quantity</br>Checked</b></td>
<td><b>Quantity</br>Order</b></td>
</tr>
</thead>
<?php
$Barcode=$_SESSION["Barcode"];
$code=$_SESSION["Code"];
$itemcode=$_SESSION["Itemcode"];
$guid=$_SESSION["guid"];
$sql = "SELECT itemmastersupcode.*, itembarcode.*, stock_count_item.*, stock_count.*, po_ex_c.*, d.itemlink_total_qty
FROM itemmastersupcode
WHERE stock_count.SupCode = '$code' and stock_count_item.TRANS_GUID = '$guid'
GROUP BY itemmastersupcode.Itemcode";
$result=mysqli_query($conn2,$sql);
$rowcount = mysqli_num_rows($result);
while($row = mysqli_fetch_assoc($result))
{
?>
<tbody>
<tr>
<td><?php echo $row["Itemcode"]; ?></td>
<td><?php echo $row["Barcode"]; ?></td>
<td><?php echo $row["Description"]; ?></td>
<td><?php echo $row["itemlink_total_qty"]; ?></td>
<td><?php echo $row["qty"]; ?></td>
<form class="form-inline" role="form" method="POST" id="myForm">
<td><input type="number" name="itemcode[<?php echo $row["itemcode"]; ?>][qty]" value=""/></td> //update here
</tr>
</tbody>
<?php
}
?>
</table>
<input type="hidden" name="supcode" value="<?php echo $_SESSION["Code"] ?>"/> //update here
<input type="hidden" name="guid" value="<?php echo $_SESSION["guid"] ?>"/>//update here
<button name="save" type="submit" ><b>SUBMIT</b></button>
</form>
Php content
<?php
if (isset($_POST["save"]))
{
$itemcodes=$_POST['itemcode'];
$qty=$_POST['qty'];
$supcode=$_POST['supcode'];
$guid=$_POST['guid'];
$sql = "UPDATE stock_count, stock_count_item SET
stock_count.posted = '1',
stock_count_item.qty_order = CASE stock_count_item.Itemcode";
foreach( $itemcodes as $itmcode=>$qty)
{
$sql.=" WHEN ".$itmcode." THEN ".$qty
}
$sql.=" END WHERE stock_count.TRANS_GUID =$guid AND stock_count_item.TRANS_GUID=$guid AND stock_count_item.Itemcode IN (".implode(",",array_keys( $itemcodes)).") ";
?>
Hope it will help
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
Im actually trying to add only a specific row from the table product to the table product_add but the last row is being inserted in the table product_add. What i want is that when I click on the button ADD that specific row is being inserted in the table product_add. I think that the code is not considering the if (isset($_REQUEST['submit'])) part.
<?php
include'connect.php';
$image = isset($_GET['image']) ? $_GET['image'] : "";
$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$price= isset($_GET['price']) ? $_GET['price'] : "";
$sql="SELECT * FROM product";
$result = mysql_query($sql);
if($result>0){
?>
<form method="POST" id="form" name="form">
<table border='1'>
<tr>
<th>Id</th>
<th>Image</th>
<th>Name</th>
<th>Price MUR</th>
</tr>
<?php
while ($row = mysql_fetch_array($result)){
extract($row);
?>
<tr>
<td><?php echo ($row['id']); ?></td>
<td><img src=<?php echo $row['image'] ?> width='120' height='100'/></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
<td>
<input id="submit" type="submit" name="submit" value='ADD'/>
</td>
</tr>
<?php
}
?>
</table>
</form>
<?php
}
if (isset($_REQUEST['submit']))
{
$insert = "INSERT INTO product_add(id, name, price) VALUES ('$id', '$name','$price')";
$insertQuery=mysql_query($insert);
}
?>
basically it a general coding issue
type submit and also name submit is conflicting
first add an attribute in form tag enctype="multipart/form-data" for image or video upload
then
creat a hidden field named 'something'
like
<input type="hidden" name="something" />
and after that use this
isset($_REQUEST['something']
add from in each while
like this updated code
<?php
include'connect.php';
$image = isset($_REQUEST['image']) ? $_REQUEST['image'] : "";
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : "";
$name = isset($_REQUEST['name']) ? $_REQUEST['name'] : "";
$price= isset($_REQUEST['price']) ? $_REQUEST['price'] : "";
$sql="SELECT * FROM product";
$result = mysql_query($sql);
if($result>0){
?>
<table border='1'>
<tr>
<th>Id</th>
<th>Image</th>
<th>Name</th>
<th>Price MUR</th>
</tr>
<?php
while ($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo ($row['id']); ?></td>
<td><img src=<?php echo $row['image'] ?> width='120' height='100'/></td>
<td><?php echo htmlspecialchars($row['name']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
<td>
<form method="POST" action="" >
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="name" value="<?php echo $row['name']; ?>" />
<input type="hidden" name="image" value="<?php echo $row['image']; ?>" />
<input type="hidden" name="price" value="<?php echo $row['price']; ?>" />
<input id="submit" type="submit" name="submit" value='ADD'/>
</form>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
if (isset($_REQUEST['submit']))
{
$insert = "INSERT INTO product_add(id, name, price) VALUES ('$id', '$name','$price')";
$insertQuery=mysql_query($insert);
}
?>
NOTE: mysql_* is deprecated use mysqli_* or PDO
new guy here!
I am building a custom shopping cart driven by mysql and i am trying to update my cart item by item in terms of quantity. It seems that i am doing something wrong because when i try to update the quantity it only update the last item. I am posting the code below. Any help would be appreciated. Thanks in advance.
1.cart.php:
$sql = "select * from orders";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
echo "Στοιχεία: ".$num;
?>
<form name="cart" method="post" action="updatecart.php">
<table border="2">
<tr>
<td>Α/Α</td>
<td>img_name</td>
<td>Reprints</td>
<td>Color</td>
</tr>
<?php
if($num){
while ($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['item_id']; ?></td>
<td><?php echo $row['img_name']; ?></td>
<td><input type="number" name="quantity" value="<?php echo $row['quantity']; ?>"></td>
<input type="hidden" name="item_id" value="<? echo $row['item_id']; ?>">
<td><?php echo $row['color']; ?></td>
</tr>
<?php
}
}
?>
</table>
<input type="submit" name="update" value="Update Cart" />
<input type="button" name="2checkout" value="Proceed to Checkout" />
</form>
2.updatecart.php
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<?php
$database_name = "vog";
$conn = mysql_connect("localhost","root","toor");
mysql_select_db($database_name);
$num = 2; //na to ferw me session meta edw!
if(isset($_POST['update'])){
$item_id = $_POST['item_id'];
$i=1;
while($i<=$num){
$item_id = $_POST['item_id'][$i];
$quantity = $_POST['quantity'];
$sql2 = "update orders SET quantity = '$quantity' where item_id = '$item_id' ";
$result2 = mysql_query($sql2) or die ("Error in query: $result2");
$i++;
}
}
if(isset($result2)){
header("Location:cart.php");
}
?>
So far it updates just the last record.
Your problem is with the names of the fields in your HTML form:
<input type="number" name="quantity" value="<?php echo $row['quantity']; ?>">
<input type="hidden" name="item_id" value="<? echo $row['item_id']; ?>">
I think you meant to call them quantity[] and item_id[] instead, so they will and up as arrays in your $_POST variable later on, now they overwrite eachother, making $_POST['item_id'] only contain the last id in the database.
in #1.cart.php use the inputs as array:
<input type="number" name="quantity[<?php echo $row['item_id']; ?>]" value="<?php echo $row['quantity']; ?>">
<input type="hidden" name="item_id[<?php echo $row['item_id']; ?>]" value="<? echo $row['item_id']; ?>">
and in #2.updatecart.php: process it like
foreach($_POST['item_id'] as $key => $id){
$item_id = $id;
$quantity = $_POST['quantity'][$key];
$sql2 = "update orders SET quantity = '$quantity' where item_id = '$item_id' ";
$result2 = mysql_query($sql2) or die ("Error in query: $result2");
$i++;
}
You need to tell PHP that you're using an array for your submitted form items. The way to do this is to make the name of each input quantity[]. You can also place the item ID directly in the array as a key. In cart.php you can do this in your loop:
<input type="number" name="quantity[<?php echo $row['item_id']; ?>]"
value="<?php echo $row['quantity']; ?>"/>
In effect, this will putput something like:
<input type="number" name="quantity[2]" value="1" />
<input type="number" name="quantity[4]" value="1" />
<input type="number" name="quantity[8]" value="2" />
i.e. 1 of item 2, 1 of item 4 and 2 of item 8.
Then, in updatecart.php you can read in the array and process it in a loop.
if(isset($_POST['update'])){
foreach ($_POST['quantity'] as $item_id => $item_qty) {
$item_id = (int)$item_id;
$item_qty = (int)$item_qty;
$sql2 = "update orders SET quantity = '$item_qty' where item_id = '$item_id' ";
mysql_query($sql2);
}
}