我在首页中有一个添加到购物车按钮。将产品收集到购物车后,用户将以表格形式提交(处理)产品列表。由于他们将提交不止一种产品,因此该表单将自动添加(我使用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"].'/>';
}
现在,我的堆栈是在考虑如何在一个提交按钮中以表单形式提交此多个值。所以我想要实现的是这样的:
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');
由于我不知道他们将提交多少产品,这也意味着sql需要多少个值,因此我现在的问题是如何根据所需输入的数量自动添加值。
我希望任何人都能理解我在这里要实现的目标。
[更新问题1]
我已经按照Arif_suhail_123的建议进行了这样的尝试:
<?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
}
?>
我的表格:
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'; }
[更新问题2]
我已经按照Umair的建议进行了这样的尝试:
<?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);
?>
但是,Total products are 0
即使我在所有相关输入中都获取了一些产品,它也会始终打印出来。
请帮忙
我想你在找这个
请阅读答案中的评论
<?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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句