请帮忙。我在论坛中找到了一些工作代码,但我认为它有问题。我的数据库中没有插入任何数据(描述和数量) 这是我的 php 代码:
<?php
$host = "localhost";
$username = "root";
$password = "";
$dbname = "posharrison";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");
$branch=$_POST['branch'];
$mydate=$_POST['mydate'];
$product=$_POST['product'];
$description = implode(',',$_POST['description']);
$quantity = implode(',',$_POST['quantity']);
$sql = "INSERT INTO orderlist (`description`,
`quantity`) VALUES ('".$description."', '".$quantity."')";
$sql = "INSERT INTO orderlist (branch, date, pname)
VALUES ('$branch', '$mydate', '$product')";
$result=mysql_query($sql);
if($result){
echo "<script>
alert('Success');
window.location ='./order.php';
</script>";
}
else {
echo "ERROR!!".die(mysql_error());
}
?>
<?php
// close connection
mysql_close();
?>
这是我的 html 代码:
<div class="desktop box" style="display:none">
Select Item:
<select name="description[]">
<option>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</option>
<option>Dell corei7 2500 1gRAM 80gHDD COMBO</option>
<option>Green</option>
<option>Blue</option>
</select>
Quantity: <input type="text" name="quantity[]" size="8">
</div>
<div class="monitor box" style="display:none">
Select Item:
<select name="description[]">
<option>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</option>
<option>monitor</option>
<option>Green</option>
<option>Blue</option>
</select>
Quantity: <input type="text" name="quantity[]" size="8">
</div>
<div class="laptop box" style="display:none">
Select Item:
<select name="description[]">
<option>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</option>
<option>laptop</option>
<option>Green</option>
<option>Blue</option>
</select>
Quantity: <input type="text" name="quantity[]" size="8">
</div>
<div class="parts box" style="display:none">
Select Item:
<select name="description[]">
<option>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</option>
<option>parts</option>
<option>Green</option>
<option>Blue</option>
</select>
Quantity: <input type="text" name="quantity[]" size="8">
</div>
最佳答案
您将覆盖此行之前的查询:
$result=mysql_query($sql);
您应该运行这两个查询:
$sql = "INSERT INTO orderlist (`description`,
`quantity`) VALUES ('".$description."', '".$quantity."')";
$result=mysql_query($sql);
$sql = "INSERT INTO orderlist (branch, date, pname)
VALUES ('$branch', '$mydate', '$product')";
$result=mysql_query($sql);
关于php - 在mysql中插入多个相同名称的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28757006/