Php 使用子查询将多行插入MySQL
我在尝试根据子查询中的WHERE条件插入新表时遇到一些问题 我有一个名为productoptions的表,它有四列LineID、ProductSize、productcolor和ProductID 我有第二个名为productprice的表,它有两列LineID,Price 我的应用程序使用PHP post方法从一个简单表单接受用户输入“productID”。我需要它做的是根据用户在表单中给出的productID,为productoptions表中存在的每个LineID在productprice表中插入一个新行 我的PHP代码如下:Php 使用子查询将多行插入MySQL,php,mysql,Php,Mysql,我在尝试根据子查询中的WHERE条件插入新表时遇到一些问题 我有一个名为productoptions的表,它有四列LineID、ProductSize、productcolor和ProductID 我有第二个名为productprice的表,它有两列LineID,Price 我的应用程序使用PHP post方法从一个简单表单接受用户输入“productID”。我需要它做的是根据用户在表单中给出的productID,为productoptions表中存在的每个LineID在productprice
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> </label>
</div>
<div class="col span-2-of-3">
<input type="submit" value="Add" name="btn-add-price">
</div>
</div>
</form>
这仍然抛出$errormsg-我已经在SQL查询上方添加了$successsg,它确实出现在浏览器中,因此它似乎是SQL代码的问题,而不是表单输入错误
正确代码:
您应该在select中执行all列,而不是在单独的代码中执行
if(mysqli_query($con,
"INSERT INTO productprice (LineID,Price)
SELECT LineID, " . $price .
" FROM productoptions WHERE LineID = '" . $productIDTwo . "'")) {
首先,您需要使用SQL查询
INSERT INTO productprice (LineID,Price)
SELECT LineID , YourPriceHere FROM productoptions
WHERE ProductID = YourProductHere;
在将裸SQL写入PHP之前,应确保其正常工作。要做到这一点,您可以使用命令行mysql客户端或任何其他
此外,concatations会使查询看起来非常难看且难以阅读,因此,如果不使用PDO与数据库交互,您至少可以像这样准备sql查询:
$sql =
strtr(
'SELECT * FROM productoptions WHERE ProductId = {ProductId}',
['ProductId' => $productId]
);
最后,在php代码中重新考虑要加下划线的表和字段名称,即product_选项、product_价格、product_id和用例,这对您是有好处的。祝你好运:我想你把SELECT和LineID之间的圆括号放错位置了?可能尝试插入productprice LineID,Price VALUES从productoptions中选择LineID,其中LineID='$productID。“,”$价格。”我认为您不需要子查询。使用“插入到选择中”。老例子。也是在产品已经存在之前!您应该检查错误是否重复,而不是其他错误。谢谢您的帮助。。。我想我现在明白了,但这似乎仍然不起作用。我已经修改了我在表格上的ID标签,因为我已经使用了两次,我认为这可能会引起问题。我还在SQL查询上方发布了$SUCCESMSG,以查看它是否出现,并且它确实出现了。这似乎是SQL的一个问题,这是仍然抛出$errormsg的修订代码-请参阅修订代码above@basil3我已经更新了答案。。。。。。select中的错误删除了..太棒了..我不得不更改一件小事,因为我实际上在WHERE子句中查询了错误的列,但现在一切都有意义了。我已经用完整的代码添加了我的问题!谢谢你的提示。。。我已经开始重命名我的数据库表和字段名。我将有一个通过我的PHP阅读,并开始camelCase!
INSERT INTO productprice (LineID,Price)
SELECT LineID , YourPriceHere FROM productoptions
WHERE ProductID = YourProductHere;
$sql =
strtr(
'SELECT * FROM productoptions WHERE ProductId = {ProductId}',
['ProductId' => $productId]
);