使用子查询将多行插入MySQL

时间:2016-10-27 21:07:47

标签: php mysql

我在尝试根据子查询中的WHERE条件插入新表时遇到一些麻烦。

我有一个名为 productoptions 的表格,其中有四列 LineID,ProductSize,ProductColour,ProductID

我有一个名为 productprice 的第二个表,它有两列 LineID,价格

我的应用程序使用PHP post方法从一个简单的表单接受用户输入'productID'。我需要做的是根据用户在表单中提供的productID,为 productoptions 表中的每个LineID在 productprice 表中插入一个新行。

我的PHP代码如下:

if (isset($_POST['btn-add-price'])) {
        $productID = mysqli_real_escape_string($con, $_POST['productID']);
        $price = mysqli_real_escape_string($con, $_POST['price']);


        if(empty($productID)) {
            $error = true;
            $num_error_two = "Please enter a value";
        }
        if(!is_numeric($productID)) {
            $error = true;
            $value_error_two = "Data entered was not numeric";
        }

        if(empty($price)) {
            $error = true;
            $num_error_three = "Please enter a value";
        }
        if(!is_numeric($price)) {
            $error = true;
            $value_error_three = "Data entered was not numeric";
        }
        if (!$error) {
            if(mysqli_query($con, "INSERT INTO productprice (LineID,Price) VALUES(SELECT(LineID FROM productoptions WHERE LineID = '" . $productID . "'), '" . $price . "')")) {
                $successmsg = "Successfully Added!";
            } else {
                $errormsg = "Product already exists!";
            }
        }
    } 

表单代码:

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" class="col span-1-of-2 product-submit-form-form">

                    <div class="row">
                        <div class="col span-1-of-3">
                            <label for="name">Product ID</label>
                        </div>
                        <div class="col span-2-of-3">
                            <input type="text" name="productID" id="productID" placeholder="Product ID" required><br>
                            <span class="text-danger"><?php if (isset($value_error_two)) echo $value_error_two; ?></span> 
                            <span class="text-danger"><?php if (isset($num_error_two)) echo $num_error_two; ?></span> 
                        </div>
                    </div>

                    <div class="row">
                        <div class="col span-1-of-3">
                            <label for="name">Price</label>
                        </div>
                        <div class="col span-2-of-3">
                            <input type="text" name="price" id="price" placeholder="Price" required><br>
                            <span class="text-danger"><?php if (isset($value_error_three)) echo $value_error_three; ?></span> 
                            <span class="text-danger"><?php if (isset($num_error_three)) echo $num_error_three; ?></span>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col span-1-of-3">
                            <label>&nbsp;</label>
                        </div>
                        <div class="col span-2-of-3">
                           <input type="submit" value="Add" name="btn-add-price">
                        </div>
                    </div>

                </form>

目前这会抛出$ errormsg

我是否以正确的方式接近这个?请保持温和,我只是刚刚进入PHP,我可以感觉到我的理解在许多方面都缺乏!!!

修改后的代码及建议:

if (isset($_POST['btn-add-price'])) {
        $productIDTwo = mysqli_real_escape_string($con, $_POST['productIDTwo']);
        $price = mysqli_real_escape_string($con, $_POST['price']);

        //name can contain only alpha characters and space

        if(empty($productIDTwo)) {
            $error = true;
            $num_error_two = "Please enter a value";
        }
        if(!is_numeric($productIDTwo)) {
            $error = true;
            $value_error_two = "Data entered was not numeric";
        }

        if(empty($price)) {
            $error = true;
            $num_error_three = "Please enter a value";
        }
        if(!is_numeric($price)) {
            $error = true;
            $value_error_three = "Data entered was not numeric";
        }
        if (!$error) {
            if(mysqli_query($con, "INSERT INTO productprice (LineID,Price) SELECT(LineID, " . $price . " FROM productoptions WHERE LineID = '" . $productIDTwo . "')")) {
                $successmsg = "Successfully Added!";
            } else {
                $errormsg = "Product already exists!";
            }
        }
    }

这仍然会抛出$ errormsg - 我在SQL查询上面添加了$ successmsg,它确实出现在浏览器中,所以它似乎是SQL代码的问题,而不是表单输入错误?

正确代码:

if (isset($_POST['btn-add-price'])) {
        $productIDTwo = mysqli_real_escape_string($con, $_POST['productIDTwo']);
        $price = mysqli_real_escape_string($con, $_POST['price']);

        //name can contain only alpha characters and space

        if(empty($productIDTwo)) {
            $error = true;
            $num_error_two = "Please enter a value";
        }
        if(!is_numeric($productIDTwo)) {
            $error = true;
            $value_error_two = "Data entered was not numeric";
        }

        if(empty($price)) {
            $error = true;
            $num_error_three = "Please enter a value";
        }
        if(!is_numeric($price)) {
            $error = true;
            $value_error_three = "Data entered was not numeric";
        }
        if (!$error) {
            if(mysqli_query($con, "INSERT INTO productprice (LineID,Price) SELECT LineID, " . $price . " FROM productoptions WHERE ProductID = '" . $productIDTwo . "'")) {
                $successmsg = "Successfully Added!";
            } else {
                $errormsg = "Product already exists!";
            }
        }
    }

2 个答案:

答案 0 :(得分:3)

您应该执行insert select(select中的所有列,而不是分开的代码中)

if(mysqli_query($con, 
    "INSERT INTO productprice (LineID,Price) 
         SELECT LineID, " . $price . 
         " FROM productoptions WHERE LineID = '" . $productIDTwo . "'")) {

答案 1 :(得分:0)

首先,您需要工作SQL查询。

INSERT INTO productprice (LineID,Price)
    SELECT LineID , YourPriceHere FROM productoptions 
    WHERE ProductID = YourProductHere;

在将它写入PHP之前,您应确保您的裸SQL正常工作。为此,您可以使用命令行mysql客户端或任何其他。

此外,连接使您的查询看起来非常难看且难以阅读,因此,如果您不使用PDO与数据库进行交互,则至少可以准备这样的SQL查询:

$sql = 
   strtr(
     'SELECT * FROM productoptions WHERE ProductId = {ProductId}',
     ['ProductId' => $productId]
   );

最后,您可以重新考虑您的表格和字段名称以强调,即&#34; product_options&#34;,&#34; product_price&#34;,&#34; product_id&#34;并在您的PHP代码中使用camelCase。祝你好运:)