在mysql中重复插入值

时间:2016-12-09 20:16:05

标签: php mysql



<?php
include("config.php");
$cart=$_GET['cart'];
$numberofcart=$_GET['numberofcart'];
if ($cart) {
mysql_query("INSERT INTO trade (cart,price) values('$cart','150LE')");

}
?>


<form action="index.php" method="GET">
  <input type="text" name="cart" />
  <input type="text" name="numberofcart" />
  <input type="submit" />
</form>
&#13;
&#13;
&#13; 购物车数量=重复次数

在mysql中就像这样     id购物车价格
    1笔150LE
    2笔150LE
    3笔150LE

1 个答案:

答案 0 :(得分:0)

<?php
include("config.php");
$cart=$_POST['cart'];
$numberofcart=$_POST['numberofcart'];
$values ='';

if ($cart !=='' && $numberofcart !=='') {
    for($i=0;$i<$numberofcart;$i++){
        $values .= "('$cart','150LE'),";
    }
    $values = trim($values,',');
    mysql_query("INSERT INTO trade (cart,price) values $values");// good to change connection code in file according to mysqli_* standards
}else{
    echo "form field are required";
}
?>


<form action="index.php" method="POST">
  <input type="text" name="cart" />
  <input type="text" name="numberofcart" />
  <input type="submit" />
</form>

实施例: - https://eval.in/694166