向数据库提交多个值

时间:2014-10-27 07:31:31

标签: php forms

我在主页上有一个添加到购物车按钮。在将产品收集到购物车后,用户将在表单中提交(处理)产品列表。由于他们将提交多个产品,因此表单将自动添加(我使用foreach函数执行此操作)。

        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

请帮助

4 个答案:

答案 0 :(得分:1)

您可以通过声明静态变量来计算项目,只要任何人将项目添加到购物车,并在从购物车中删除项目时减少它。

然后你可以使用这个变量来查找购物车中的商品数量,并相应地运行循环。

我希望,这有帮助。

答案 1 :(得分:1)

我可以看到包含Cart产品的SESSION。你可以尝试类似的东西。

$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);

答案 2 :(得分:1)

我认为你正在寻找这个

请阅读答案中的评论

<?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

    }

答案 3 :(得分:0)

像这样:

$query = "INSERT INTO  `product` (  `productname` ,  `price` ,  `image`) Values";

while(Statement){ //(Or a for statement)
       $query .= "(".$somevar1.",".$somevar2.",".$somevar3."),";
}

$query .= ";";
//execute your query here.

将其调整为您的代码,以便它能够正常运行您的代码,您必须看到您可以使用最好的,一个for或一个while循环。这取决于你。