用户输入时向数据库添加多行

时间:2018-02-05 14:28:35

标签: php html database phpmyadmin

我正在尝试解决在提交表单时如何在“订单详细信息”表中添加多行。用户能够选择多个尺寸,这些尺寸在提交时应该生成由用户选择的多达N行。 以下是我目前查看文件的代码:

        <form action="../Controller/NewProduct.php" method="POST">
            <h4 align="center"> PRODUCT <b>DETAILS</b> </h4>
            <br/>

            <div class="form-group">
                <label for="categoryid">Category</label>
                <select class="form-control" name="categoryid" id="categoryid" required>
                    <option  value="1">Tshirt</option>
                    <option  value="2">Swim Shorts</option>
                </select>
            </div>
            <br/>
            <div class="form-group">
                <label for="productid">Product ID</label>
                <input name="productid" class="form-control" id="productid" required>
            </div>
            <br/>
            <div class="form-group">
                <label for="name">Product Name</label>
                <input name="name" class="form-control" id="name" required>
            </div>
            <br/>
             <div class="form-group">
                <label for="description">Product Description</label><br/>
                <textarea name="description" rows="4" class="description" placeholder="Enter product description here." required></textarea>
            </div>
            <br/>
            <div class="form-group">
                <label for="size">Size</label>
                <select class="form-control" id="size" name="size" multiple required>
                    <option value="S">S</option>
                    <option value="M">M</option>
                    <option value="L">L</option>
                    <option value="XL">XL</option>
                    <option value="XXL">XXL</option>
                </select>
            </div>
            <br/>
            <div class="form-group">
                <label for="colour">Colour</label>
                <select class="form-control" id="colour" name="colour" required>
                    <option value="Burgundy">Burgundy</option>
                    <option value="Navy Blue">Navy Blue</option>
                    <option value="Red">Red</option>
                    <option value="White">White</option>
                    <option value="Grey">Grey</option>
                    <option value="Black">Black</option>
                    <option value="Yellow">Yellow</option>
                    <option value="Green">Green</option>
                    <option value="Khaki">Khaki</option>
                    <option value="Blue">Blue</option>
                </select>
            </div>
            <br/>
            <div class="form-group">
                <label for="unitprice">Unit Price</label>
                <input name="unitprice" class="form-control" id="unitprice" placeholder="Do not include the £ sign." required>
            </div>
            <br/>
            <div class="form-group">
                <label for="unitsinstock">Units In Stock</label>
                <input name="unitsinstock" class="form-control" id="unitsinstock" required>
            </div>
            <br/>
            <div class="form-group">
                <label for="pimage">Product Image</label>
                <input name="pimage" class="form-control" id="pimage" placeholder="Enter Image URL" required>
            </div>
            <br/>
            <div class="form-group">
                <label for="discount">Discount</label>
                <input name="discount" class="form-control" id="discount">
            </div>
            <br/>
            <button type="submit" name="submit" class="btn"><i class="fa fa-check"></i> Done</button>
        </form>

这就是提交表单时运行的文件:     

if (isset($_POST['submit'])) {

require_once 'DatabaseConnection.php';


$statement = $pdo->prepare("INSERT INTO Product (categoryid, name, description, colour, unitprice, pimage, discount, productid)
        VALUES (:categoryid, :name, :description, :colour, :unitprice, :pimage, :discount, :productid)");


$statement->bindParam(':categoryid', $categoryid);
$statement->bindParam(':name', $name);
$statement->bindParam(':description', $description);
$statement->bindParam(':colour', $colour);
$statement->bindParam(':unitprice', $unitprice);
$statement->bindParam(':pimage', $pimage);
$statement->bindParam(':discount', $discount);
$statement->bindParam(':productid', $productid);

$categoryid = $_POST['categoryid'];
$name = $_POST['name'];
$description = $_POST['description'];
$colour = $_POST['colour'];
$unitprice = $_POST['unitprice'];
$pimage = $_POST['pimage'];
$discount = $_POST['discount'];


$statement2 = $pdo->prepare("INSERT INTO ProductDetails (unitsinstock, size, productid) VALUES (:unitsinstock, :size, :productid)");
$statement2->bindParam(':size', $size);
$statement2->bindParam(':unitsinstock', $unitsinstock);
$statement2->bindParam(':productid', $productid);

$size = $_POST['size'];
$unitsinstock = $_POST['unitsinstock'];
$productid = $_POST['productid'];

$statement->execute();
$statement2->execute();

header('location:../View/AdminAllProducts.php');
}
?>

我想要实现的一个例子如下:https://imgur.com/a/SPr14

2 个答案:

答案 0 :(得分:0)

您应该将可以多次选择的选项更改为数组(例如:size([])在PHP中,您应该将请求值作为数组使用。

您还可以在要插入的值中连接字符串,以便只对数据库执行一次请求。

答案 1 :(得分:0)

拿起尺寸数组并循环处理:

$sizes = $_POST['size[]'];

foreach ( $sizes as $size )
{

   //  ... your code of statement2 :
   statement2 = $pdo->prepare("INSERT INTO ProductDetails (unitsinstock, size, productid) VALUES (:unitsinstock, :size, :productid)");
   $statement2->bindParam(':size', $size);
   $statement2->bindParam(':unitsinstock', $unitsinstock);
   $statement2->bindParam(':productid', $productid);


   $unitsinstock = $_POST['unitsinstock'];
   $productid = $_POST['productid'];

   $statement->execute();
   $statement2->execute();

}