I've this problem in which I can't update the cart for some reason. I've looked for many solutions to see if they can solve my problem but no luck. I've 2 files one called cart.php which contains the form and a updatebasket.php file which contains query.
Cart file
<?php
$count = 0;
while ($count < $numrow)
{
$row = $results -> fetch_assoc();
extract($row);
echo"<div>";
echo"<div class='recommended_games'>";
echo "<img src='images/".$gameIMG."' />";
echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag">£'.$gamePrice. '</div>';
echo'</div>';
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform">';
echo '<input type="text" value="1" name="quantity" id="quantity" />';
echo '<input type="hidden" value="'.$gameID.'" name='.$gameID.' id="gameid" />';
echo '<input type="submit" value="update" />';
echo '</form>';
echo '</div>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
?>
updatebasket file
<?php
session_start();
require "dbconnect.php";
$memberID = $_SESSION['id'];
$quantity = $_POST['quantity'];
$gameID = $_POST['gameid'];
mysqli_autocommit($con,FALSE);
$connect->query($query);
$query = "UPDATE basket SET quantity = ".$quantity." WHERE gameid = ".$gameID." AND id = ".$memberID."";
$results = $connect->query($query);
mysqli_commit($con);
header('Location: cart.php');
?>
Change cart.php to this:
<?php
$count = 0;
while ($count < $numrow)
{
$row = $results -> fetch_assoc();
extract($row);
echo"<div>";
echo"<div class='recommended_games'>";
echo "<img src='images/".$gameIMG."' />";
echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag">£'.$gamePrice. '</div>';
echo'</div>';
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform">';
echo '<input type="text" value="1" name="quantity" id="quantity" />';
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
echo '<input type="submit" value="update" />';
echo '</form>';
echo '</div>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
?>
The big change is this:
BEFORE:
echo '<input type="hidden" value="'.$gameID.'" name='.$gameID.' id="gameid" />';
AFTER:
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
php uses the name attribute as the key for post and get, not id.
id is used by javascript, jquery (also javascript), css, and probably a few other things.
But in forms, name is the one you want for the post and get key.
Related
I'm making a search query for my webs, and i get an error for my query after i add the if ($results==0) ......
Object of class mysqli_result could not be converted to int in line 65 which refer to if ($results==0)
$results = $mysqli->query("SELECT * FROM produk2 WHERE $construct" );
if ($results==0)
{
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1.
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
meaning</br>3. Please check your spelling";
}
else{
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<form method="post" action="cart_update.php">';
echo '<div class="product-thumb"><img src="images/'.$obj->product_img_name.'"></div>';
echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
echo '<div class="product-desc">'.$obj->product_desc.'</div>';
echo '<div class="product-info">';
echo 'Price '.$currency.$obj->price.' | ';
echo 'Qty <input type="text" name="product_qty" value="1" size="3" />';
echo '<button class="add_to_cart">Add To Cart</button>';
echo '</div></div>';
echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
}
$mysqli->query don't have the data count until you ask for it
Try like this
if($results ->num_rows == 0){
// no result found
}
There are 4 sub menus in "menu" button which are drinks, main, appetizers and desserts. When users click drinks, they will be directed to drinks and the same when they go other sub menu buttons. But I want to display all menu when users click "menu" button. I know I have to use "else" but the menu does not display at all.
menu.php
<div class="products">
<?php
if (isset($_GET['id'])) {
//current URL of the Page. basket_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$results = $mysqli->query("SELECT * FROM menu WHERE MenuTypeID = ".$_GET['id']);
$currency = '$';
if ($results) {
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<form method="post" action="basket_update.php">';
echo '<div class="product-thumb"><img src="'.$obj->MenuPicture.'" /></div>';
echo '<div class="product-content"><h3>'.$obj->MenuName.'</h3>';
echo '<div class="product-desc">'.$obj->MenuDescription.'</div>';
echo '<div class="product-info">';
echo 'Price: '.$currency.$obj->MenuPrice.' | ';
echo 'Qty <input type="text" name="menu_qty" value="1" size="1" />';
echo '<button class="add_to_basket">Add To Basket</button>';
echo '</div></div>';
echo '<input type="hidden" name="MenuID" value="'.$obj->MenuID.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
}
?>
<?php
}else{
$query2 = "SELECT * FROM Menu";
$result2 = mysql_query($query2);
}
?>
</div>
Try this code.
<div class="products">
<?php
if (isset($_GET['id'])) {
$results = $mysqli->query("SELECT * FROM menu WHERE MenuTypeID = ".$_GET['id']);
}
else{
$results = $mysqli->query("SELECT * FROM menu");
}
//current URL of the Page. basket_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$currency = '$';
if ($results) {
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<form method="post" action="basket_update.php">';
echo '<div class="product-thumb"><img src="'.$obj->MenuPicture.'" /></div>';
echo '<div class="product-content"><h3>'.$obj->MenuName.'</h3>';
echo '<div class="product-desc">'.$obj->MenuDescription.'</div>';
echo '<div class="product-info">';
echo 'Price: '.$currency.$obj->MenuPrice.' | ';
echo 'Qty <input type="text" name="menu_qty" value="1" size="1" />';
echo '<button class="add_to_basket">Add To Basket</button>';
echo '</div></div>';
echo '<input type="hidden" name="MenuID" value="'.$obj->MenuID.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
}
?>
I can't find where is the problem at my code. It only send single orders to the database. When I order in my cart 2 or more than items, it only sends the last order. I don't have any idea how I can change or add some syntax in my code.
Here is my code in checkout.php
<?php
session_start();
echo '<h3>Your Order</h3>';
$current_url = base64_encode($url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(isset($_SESSION['products'])){
echo '<ol>';
echo '<form action="checkout_with_us.php" method="POST">';
$total = 0;
$cart_items = 0;
foreach($_SESSION['products'] as $cart_itm){
$product_code = $cart_itm['code'];
$results = $mysqli->query("SELECT product_name,product_desc,price FROM products WHERE product_code='$product_code' LIMIT 1");
$obj = $results->fetch_object();
echo '<li>';
echo 'Price: '.$currency.$obj->price;
echo '<h4>'.$obj->product_name.'(Code: '.$product_code.')</h4>';
echo 'Qty: '.$cart_itm['qty'];
echo '</li>';
$subtotal = ($cart_itm['price'] * $cart_itm['qty']);
$total = ($total + $subtotal);
$cart_items++;
echo '<input type="hidden" name="item_name" value="'.$obj->product_name.'">';
echo '<input type="hidden" name="item_desc" value="'.$obj->product_desc.'">';
echo '<input type="hidden" name="item_qty" value="'.$cart_itm["qty"].'">';
echo '<input type="hidden" name="item_code" value="'.$product_code.'">';
}
echo '<strong>Sub Total: '.$currency.$total.'</strong>';
echo '<input type="hidden" name="price" value="'.$total.'">';
echo '</ol>';
}
//Here is the information of the customer
echo 'Firstname: <input type="text" name="firstname"><br />';
echo 'Lastname: <input type="text" name="lastname"><br />';
echo 'Email: <input type="text" name="email"><br />';
echo '<input type="submit" value="Send Step">';
echo '</form>';
?>
And here is my checkout.with_us.php codes. This code is the bridge to send the information to the database.
<?php
session_start();
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$order_name = $_POST['item_name'];
$order_code = $_POST['item_code'];
$order_qty = $_POST['item_qty'];
$sub_total = $_POST['price'];
$conn = mysqli_connect('localhost','root','','sampsix')or die('Could not connect');
$query = "INSERT INTO `sampsix`.`orders`(`firstname`,`lastname`,`email`,`OrderName`,`OrderCode`,`OrderQty`,`SubTotal`) VALUES('$firstname','$lastname','$email','$order_name','$order_code','$order_qty','$sub_total')";
mysqli_query($conn,$query);
mysqli_close($conn);
header('Location: checkout.php');
?>
Delete your other question, ok?
The problem is you loop through $_SESSION and use the same name value each time. You need to create an array of your inputs. Here is an example:
<?php
echo '<h3>Your Order</h3>';
$current_url = base64_encode($url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(isset($_SESSION['products'])){
echo '<ol>';
echo '<form action="checkout_with_us.php" method="POST">';
$total = 0;
$cart_items = 0;
foreach($_SESSION['products'] as $cart_itm){
$product_code = $cart_itm['code'];
$results = $mysqli->query("SELECT product_name,product_desc,price FROM products WHERE product_code='$product_code' LIMIT 1");
$obj = $results->fetch_object();
echo '<li>';
echo 'Price: '.$currency.$obj->price;
echo '<h4>'.$obj->product_name.'(Code: '.$product_code.')</h4>';
echo 'Qty: '.$cart_itm['qty'];
echo '</li>';
$subtotal = ($cart_itm['price'] * $cart_itm['qty']);
$total = ($total + $subtotal);
$cart_items++;
echo '<input type="hidden" name="product['.$product_code.'][item_name]" value="'.$obj->product_name.'">';
echo '<input type="hidden" name="product['.$product_code.'][item_desc]" value="'.$obj->product_desc.'">';
echo '<input type="hidden" name="product['.$product_code.'][item_qty]" value="'.$cart_itm["qty"].'">';
echo '<input type="hidden" name="product['.$product_code.'][item_code]" value="'.$product_code.'">';
}
echo '<strong>Sub Total: '.$currency.$total.'</strong>';
echo '<input type="hidden" name="product['.$product_code.'][price]" value="'.$total.'">';
echo '</ol>';
}
//Here is the information of the customer
echo 'Firstname: <input type="text" name="firstname"><br />';
echo 'Lastname: <input type="text" name="lastname"><br />';
echo 'Email: <input type="text" name="email"><br />';
echo '<input type="submit" value="Send Step">';
echo '</form>';
?>
You can catch this by looping in your product array:
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$conn = mysqli_connect('localhost','root','','sampsix')or die('Could not connect');
foreach($_POST['product'] as $product)
{
$order_name = $product['item_name'];
$order_code = $product['item_code'];
$order_qty = $product['item_qty'];
$sub_total = $product['price'];
$query = "INSERT INTO `sampsix`.`orders`(`firstname`,`lastname`,`email`,`OrderName`,`OrderCode`,`OrderQty`,`SubTotal`) VALUES('$firstname','$lastname','$email','$order_name','$order_code','$order_qty','$sub_total')";
mysqli_query($conn,$query);
}
mysqli_close($conn);
header('Location: checkout.php');
?>
I don't know what the purpose is of the table orders but with my example the products will be added to this table with the same firstname, lastname, etc.
Hi im currently doing making a website that sells games as a project but im having problems calculating the sum for the price of the games
I have this loop which displays the games added into the basket
cart.php
<?php
$count = 0;
while ($count < $numrow)
{
$row = $results -> fetch_assoc();
extract($row);
echo"<div>";
echo"<div class='recommended_games'>";
echo "<img src='images/".$gameIMG."' />";
echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag" name="price" method="POST">£'.$gamePrice. '</div>';
echo'</div>';
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform">';
echo '<select name="quantity" id="quantity" />';
echo '<option value="1">1</option>';
echo '<option value="2">2</option>';
echo '<option value="3">3</option>';
echo '<option value="4">4</option>';
echo '<option value="5">5</option>';
echo '</select>';
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
echo '<input type="submit" value="update" />';
echo '</form>';
echo '<div class="quantity_update">';
echo '<form action="remove_item.php" method="POST">';
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
echo '<input type="submit" value="Remove Item" />';
echo '</form>';
echo '</div>';
echo '</div>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
echo '<div id="delete_all">';
echo '<form action="delete_cart.php" method="POST">';
echo '<input id="hide_button" type="submit" value="Clear All" />';
$a=array($gamePrice);
echo array_sum($a);
echo '</form>';
echo '</div>';
?>
this is where im trying to calculate the total price
$a=array($gamePrice);
echo array_sum($a);
The reason this doesnt work, is because $gamePrice never is an array (unless you didnt provide all code). In the loop, it gets set to a new value, after the loop only the last one is stored.
Based on some hints in your code, I guessing this is a cart and you're looping through the cart. An easy way to get a total is like this:
$total = 0;
while( $itemsThatWeLoop){
// Some code here
$total+= $gamePrice*$quantity;
}
You add a variable which increments with the product's price
To explain the while-only-last-value-saved:
$i=0;
while( $i<=10){
$i= $i+1;
}
echo $i;
Will give 10. All other iterations $i gets set to a new value. The original value is not saved
Hello i am currently doing a project to make a website that sells game however one of my problems right now is updating the quantity with a single button. i have got it working with multiple update buttons next to the items that i would like to update but to make it more realistic, i would like to just have one button that will update all the item quantities in the basket table in my database.
This is my cart file, i have commented out the bit where it works with multiple update buttons.
<?php
$count = 0;
while ($count < $numrow)
{
$row = $results -> fetch_assoc();
extract($row);
echo"<div>";
echo"<div class='recommended_games'>";
echo "<img src='images/".$gameIMG."' />";
echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag">£'.$gamePrice. '</div>';
echo'</div>';
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform" id="update_all">';
echo '<select name="quantity" id="quantity" />';
echo '<option value="1">1</option>';
echo '<option value="2">2</option>';
echo '<option value="3">3</option>';
echo '<option value="4">4</option>';
echo '<option value="5">5</option>';
echo '</select>';
//echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
//echo '<input type="submit" value="update" />';
echo '</form>';
echo '</div>';
echo '</form>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" form="update_all"/>';
echo '<input type="submit" value="update" form="update_all"/>';
?>
This is my updatebasket file which updates the quantity in the database
<?php
session_start();
require "dbconnect.php";
$memberID = $_SESSION['id'];
$quantity = $_POST['quantity'];
$gameID = $_POST['gameid'];
$connect->query($query);
$query = "UPDATE basket SET quantity = ".$quantity." WHERE gameid = ".$gameID." AND id = ".$memberID."";
$results = $connect->query($query);
mysqli_commit($con);
header('Location: cart.php');
?>
If I understand you correctly, you need to work with an array for this to be achieved, by changing your form and you can use a foreach to assemble your query to update each entry in the cart. So one button will update each item in the cart with the respective quantities. I did not test this code, this is how I would approach it.
Eample of the HTML Form Changes:
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform" id="update_all">';
echo '<select name="quantity[]" id="quantity" />';
echo '<option value="1">1</option>';
echo '<option value="2">2</option>';
echo '<option value="3">3</option>';
echo '<option value="4">4</option>';
echo '<option value="5">5</option>';
echo '</select>';
//echo '<input type="hidden" value="'.$gameID.'" name="gameid[]" id="gameid" />';
//echo '<input type="submit" value="update" />';
echo '</form>';
echo '</div>';
echo '</form>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
echo '<input type="hidden" value="'.$gameID.'" name="gameid[]" id="gameid" form="update_all"/>';
echo '<input type="submit" value="update" form="update_all"/>';
?>
Query Eample:
foreach ($_POST['gameid'] as $row=>$id) {
$gameid = $id;
$newquantity = ($_POST['quantity'][$row]);
$query = "UPDATE basket SET quantity = ".$newquantity." WHERE gameid = ".$gameID." AND id = ".$memberID."";
$connect->query($query);
}
One solution would be to give your quantity <select> inputs unique names with the Game ID attached to the end (eg. "quantity_32"). This makes it easy to know how much of each game is in the cart.
// POST Example
$_POST['quantity_32'] = 1;
$_POST['quantity_31'] = 3;
$_POST['quantity_37'] = 2;
Front-End Form Change
echo '<select name="quantity_<?=$gameID?>" id="quantity" />';
Back-End Processing
Then on the processing page loop through the $_POST variables and find the quantity fields and grab their Game ID's.
foreach ($_POST as $key => $quantity) {
// Ignore non-quantity fields
if (preg_match('/quantity_([0-9]+)/i', $key, $result) !== 1) continue;
$quantity = (int)$quantity;
$game_id = (int)$result[1];
// Update Cart Quantity in DB
// ...
}
Important!
Please ensure you SQL-Escape all values you save into the DB. Hackers could do some nasty stuff if you don't.
This example shows how you can keep things safe using MySQLi Prepared Statements.
http://mattbango.com/notebook/code/prepared-statements-in-php-and-mysqli/