是否可以僅使用一個插入查詢來簡化以下代碼?將多個插入查詢合併爲一個
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());
}
}
您的代碼非常難以閱讀。順便說一下,你的代碼可能會打開盲SQL注入。確保你的變量不能包含錯誤的SQL代碼。 – rekire
我想訂購尺寸爲M的顏色爲''的; DROP TABLE store_product_attributes --'。 –
你是一個邪惡的傢伙Linus;) – rekire