在mysql中插入多个相同的命名字段

时间:2015-02-27 03:24:49

标签: php mysql

请帮忙。我做了一些我在论坛上发现的工作代码,但我觉得它有问题。我的数据库中没有插入数据(描述和数量) 这是我的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">
                                    &nbsp;&nbsp;Select Item:
                                    <select name="description[]">
                                            <option>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</option>
                                            <option>Dell corei7 2500 1gRAM 80gHDD COMBO</option>
                                            <option>Green</option>
                                            <option>Blue</option>
                                    </select>
                                    &nbsp;Quantity:&nbsp;<input type="text" name="quantity[]" size="8">
                                    </div>

                                    <div class="monitor box" style="display:none">
                                    &nbsp;&nbsp;Select Item:
                                    <select name="description[]">
                                            <option>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</option>
                                            <option>monitor</option>
                                            <option>Green</option>
                                            <option>Blue</option>
                                    </select>
                                    &nbsp;Quantity:&nbsp;<input type="text" name="quantity[]" size="8">
                                    </div>

                                    <div class="laptop box" style="display:none">
                                    &nbsp;&nbsp;Select Item:
                                    <select name="description[]">
                                            <option>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</option>
                                            <option>laptop</option>
                                            <option>Green</option>
                                            <option>Blue</option>
                                    </select>
                                    &nbsp;Quantity:&nbsp;<input type="text" name="quantity[]" size="8">
                                    </div>  
                                    <div class="parts box" style="display:none">
                                    &nbsp;&nbsp;Select Item:
                                    <select name="description[]">
                                            <option>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</option>
                                            <option>parts</option>
                                            <option>Green</option>
                                            <option>Blue</option>
                                    </select>
                                    &nbsp;Quantity:&nbsp;<input type="text" name="quantity[]" size="8">
                                    </div>

2 个答案:

答案 0 :(得分:2)

您在此行之前覆盖了您的查询:

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

答案 1 :(得分:0)

请在发布到StackOverflow之前尝试检查您的代码。

$sql = "INSERT INTO orderlist (`description`,
`quantity`) VALUES ('".$description."', '".$quantity."')"; 

$sql = "INSERT INTO orderlist (branch, date, pname)
VALUES ('$branch', '$mydate', '$product')";
$result=mysql_query($sql);

您的问题是您的SQL变量。