Mysql 将多个insert查询合并为一个查询

Mysql 将多个insert查询合并为一个查询,mysql,Mysql,是否可以仅使用一个insert查询来简化以下代码 if(isset($_POST['colore'])) { $range = array_keys($_POST['colore']); foreach ($range as $key) { $colore = $_POST['colore'][$key]; $s = $_POST['S'][$key]; $m = $_POST['M'][$key]; $l = $_POST['L'][$key]

是否可以仅使用一个insert查询来简化以下代码

if(isset($_POST['colore'])) {
    $range = array_keys($_POST['colore']); 
    foreach ($range as $key) {
    $colore = $_POST['colore'][$key];
    $s = $_POST['S'][$key];
    $m = $_POST['M'][$key];
    $l = $_POST['L'][$key];
    $xl = $_POST['XL'][$key];
    $xxl = $_POST['XXL'][$key];

    $sql = "INSERT INTO store_product_attributes (`prod_id`, `color`, `size`, `qty`)
    VALUES ('$last_id', '$colore', 's', '$s')";
    $query = mysql_query($sql) or die(mysql_error());
    $sql = "INSERT INTO store_product_attributes (`prod_id`, `color`, `size`, `qty`)
    VALUES ('$last_id', '$colore', 'm', '$m')";
    $query = mysql_query($sql) or die(mysql_error());
    $sql = "INSERT INTO store_product_attributes (`prod_id`, `color`, `size`, `qty`)
    VALUES ('$last_id', '$colore', 'l', '$l')";
    $query = mysql_query($sql) or die(mysql_error());
    $sql = "INSERT INTO store_product_attributes (`prod_id`, `color`, `size`, `qty`)
    VALUES ('$last_id', '$colore', 'xl', '$xl')";
    $query = mysql_query($sql) or die(mysql_error());
    $sql = "INSERT INTO store_product_attributes (`prod_id`, `color`, `size`, `qty`)
    VALUES ('$last_id', '$colore', 'xxl', '$xxl')";
    $query = mysql_query($sql) or die(mysql_error());
}
}您可以尝试:

INSERT INTO store_product_attributes (`prod_id`, `color`, `size`, `qty`)
VALUES ('$last_id', '$colore', 's', '$s'), ('$last_id', '$colore', 'm', '$m'), 
('$last_id', '$colore', 'l', '$l'), ('$last_id', '$colore', 'xl', '$xl'), 
('$last_id', '$colore', 'xxl', '$xxl');
但是你的密码很危险。SQL注入使用该代码非常简单,您应该在google上了解更多关于安全类型的信息:owasp或SQL注入

您可以尝试:

INSERT INTO store_product_attributes (`prod_id`, `color`, `size`, `qty`)
VALUES ('$last_id', '$colore', 's', '$s'), ('$last_id', '$colore', 'm', '$m'), 
('$last_id', '$colore', 'l', '$l'), ('$last_id', '$colore', 'xl', '$xl'), 
('$last_id', '$colore', 'xxl', '$xxl');

但是你的密码很危险。SQL注入使用该代码非常简单,您应该在google上了解更多关于安全类型的信息:owasp或SQL注入

如果我理解正确,应该可以使用此模式插入多个值:

INSERT INTO table (field1, field2, ...) VALUES (...), (...)

如果我理解正确,应该可以使用此模式插入多个值:

INSERT INTO table (field1, field2, ...) VALUES (...), (...)

您可以这样重写它:

$colore = mysql_real_escape_string($_POST['colore'][$key]);
$s = mysql_real_escape_string($_POST['S'][$key]);
$m = mysql_real_escape_string($_POST['M'][$key]);
$l = mysql_real_escape_string($_POST['L'][$key]);
$xl = mysql_real_escape_string($_POST['XL'][$key]);
$xxl = mysql_real_escape_string($_POST['XXL'][$key]);

$sql = "
    INSERT INTO store_product_attributes 
        (`prod_id`, `color`, `size`, `qty`)
     VALUES ('$last_id', '$colore', 's', '$s'),
        ('$last_id', '$colore', 'm', '$m'),
        ('$last_id', '$colore', 'l', '$l'),
        ('$last_id', '$colore', 'xl', '$xl'),
        ('$last_id', '$colore', 'xxl', '$xxl')";
$query = mysql_query($sql) or die(mysql_error());
重要:

确保查询中使用了所有值


在进行此操作时,最好切换到或,因为不鼓励使用mysql扩展。

您可以这样重写它:

$colore = mysql_real_escape_string($_POST['colore'][$key]);
$s = mysql_real_escape_string($_POST['S'][$key]);
$m = mysql_real_escape_string($_POST['M'][$key]);
$l = mysql_real_escape_string($_POST['L'][$key]);
$xl = mysql_real_escape_string($_POST['XL'][$key]);
$xxl = mysql_real_escape_string($_POST['XXL'][$key]);

$sql = "
    INSERT INTO store_product_attributes 
        (`prod_id`, `color`, `size`, `qty`)
     VALUES ('$last_id', '$colore', 's', '$s'),
        ('$last_id', '$colore', 'm', '$m'),
        ('$last_id', '$colore', 'l', '$l'),
        ('$last_id', '$colore', 'xl', '$xl'),
        ('$last_id', '$colore', 'xxl', '$xxl')";
$query = mysql_query($sql) or die(mysql_error());
重要:

确保查询中使用了所有值


当你在做这件事时,最好切换到或,因为不鼓励使用mysql扩展。

你的代码很难阅读。顺便说一句,您的代码可以为盲SQL注入打开。请确保您的变量不能包含错误的SQL代码。我想订购一个大小为M,颜色为“;删除表存储\产品\属性-。您的代码很难阅读。顺便说一句,您的代码可以为盲SQL注入打开。请确保您的变量不能包含错误的SQL代码。我想订购一个大小为M,颜色为“;DROP TABLE store_product_attributes-。谢谢,但我已重新编译了代码,它太长了,是的,我在插入它之前保护了代码。谢谢,但我已重新编译了代码,它太长了,是的,我在插入它之前保护了代码