Reputation: 886
I have an add to cart button in the homepage. After collecting their product to their cart, the user will submit (process) the list of the product in the form. Since there would be more than one product that they will submit, the form is going to be added automatically (I do this with foreach
function).
foreach ($_SESSION["products"] as $cart_itm) {
echo '<input name="product_name">'.$cart_itm["product_name"].'/>';
echo '<input name="product_price">'.$cart_itm["prduct_price"].'/>';
}
My stack now is to think how to submit this multiple value in the form in one submit button. So what I want to achieve is like this:
INSERT INTO `product` ( `productname` , `price` , `image`)
VALUES ('Product 1', '$10', 'product-1.jpg'),
('Product 2', '$20', 'product-2.jpg'),
('Product 3', '$30', 'product-3.jpg');
Since I don't know how much the product they are going to submit which it also mean how many values that is needed to the sql, my question now is how to automatically add value based on the number of the input that is needed.
I hope anyone understand what I am going to achieve here.
[UPDATE QUESTION 1]
I have tried it like this as how Arif_suhail_123 suggested:
<?php
$mysql_hostname = "myhost.com";
$mysql_user = "myname";
$mysql_password = "mypassword";
$mysql_database = "database-one";
$connection=mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Can't connect to mysql");
mysql_select_db($mysql_database, $connection) or die("Can't Select Database");
for($i=0; $i<count($_POST['product_name']);$i++)
{
$product_name=$_POST['product_name'][$i];
$product_price=$_POST['product_price'][$i];
//Run your query here.
$mysql_query="INSERT INTO `member_cart` ( `product_cart_name` , `product_cart_price`)
VALUES ('$product_name', '$product_price')";
$result= mysql_query($connection, $mysql_query)
or die(mysql_error($connection));//here $connection is your
//database connection
}
?>
My form:
if(isset($_SESSION["products"])) {
$total = 0;
echo '<ol>';
echo '<form action="cart-post-config.php" method="POST">>';
foreach ($_SESSION["products"] as $cart_itm) {
echo '<tr>';
echo '<td></td>';
echo '<td><input type="text" name="product_name[]"value='.$cart_itm["name"].'/></td>';
echo '<td><input type="text" name="product_price[]"value='.$cart_itm["price"].'/></td>';
echo '<td>View Save Delete</td>';
echo '</tr>';
}
echo '<input type="submit" name="submit" />';
echo '</form>';//close the form
}
else {
echo 'Your Cart is empty'; }
[UPDATE QUESTION 2]
I have tried it like this as how Umair suggested:
<?php
$mysql_hostname = "....";
$mysql_user = ".....";
$mysql_password = ".....";
$mysql_database = ".......";
$connection=mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Can't connect to mysql");
mysql_select_db($mysql_database, $connection) or die("Can't Select Database");
$query = "INSERT INTO `member_cart` ( `product_cart_name` , `product_cart_price`) VALUES";
$values = array();
$total_products=0;
foreach ($_SESSION["products"] as $cart_itm) {
$values[] = "('{$cart_itm['product_name']}', '{$cart_itm['product_price']}')";
$total_products++;
}
print "Total products are ".$total_products;
//final query
$query = implode(",",$values);
?>
But it always prints that Total products are 0
even though I have fetch some products in the all related inputs.
Please Help
Upvotes: 2
Views: 511
Reputation: 2509
I think you are looking for this
Please read the comment in the answer
<?php
echo '<form action="Your_File_name" method="POST">';
foreach ($_SESSION["products"] as $cart_itm) {//store them in array
echo '<input type= "text" name="product_name[]" value='.$cart_itm["product_name"].'/>';
echo '<input type= "text" name="product_price[]" value='.$cart_itm["prduct_price"].'/>';
}
echo '<input type="submit" name="submit" />';
echo '</form>';//close the form
//use array filter to remove the empty value
$product_name=array_filter($_POST['product_name']);//remove the empty value
$product_price=array_filter($_POST['product_price']);//remove the empty value
for($i=0; $i<count($product_name);$i++)//run a for loop, on the new array.
{
$product=$product_name[$i];
$price=$product_price[$i];
$mysql_query="INSERT INTO `member_cart` ( `product_cart_name` , `product_cart_price`)
VALUES ('$product', '$price')"; //use the new variable to run the query
$result= mysql_query($connection, $mysql_query)
or die(mysql_error($connection));//here $connection is your
//database connection
}
Upvotes: 1
Reputation: 21231
As I can see a SESSION containing the Cart products. You can try something simlar to this.
$query = "INSERT INTO `product` ( `productname` , `price` , `image`) VALUES";
$values = array();
$total_products=0;
foreach ($_SESSION["products"] as $cart_itm) {
$values[] =
"('{$cart_itm['product_name']}', '{$cart_itm['product_price']}',
'{$cart_itm['product_image']}')";
$total_products++;
}
print "Total products are ".$total_products;
//final query
$query .= implode(",",$valuse);
Upvotes: 1
Reputation: 81
Make it like this:
$query = "INSERT INTO `product` ( `productname` , `price` , `image`) Values";
while(Statement){ //(Or a for statement)
$query .= "(".$somevar1.",".$somevar2.",".$somevar3."),";
}
$query .= ";";
//execute your query here.
Adjust it to your code, so that it works properly for your code, you have to see what you can use the best, a for or a while loop. That's up to you.
Upvotes: 0
Reputation: 2943
You can achieve this by declaring static variable to count the items whenever any one add item to cart and decrease it when they remove the item from cart.
Then you can use this variable to find the number of items in cart and can run loop accordingly.
I hope, it helps.
Upvotes: 1