한 번에 여러 행을 삽입 할 수 있습니다 prodcut에 대한
// Check for a form submiss
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$product=$_POST['product'];
foreach($product as $productcount){
$q = 'INSERT INTO product(id,code,name,description,category_id,price,icon) VALUES (NULL,'.$productcount['code'].',"'.$productcount['name'].'",'.$productcount['description'].',"'.$productcount['category'].',"'.$productcount['price'].',"'.$productcount['thumbnail'].')';
mysqli_query($dbc, $q);//insertion of general information of current product
//insertion of many attribute of current product
$sql = 'INSERT INTO product_attribute (product_id,code,c_value,s_value,stock) VALUES (LAST_INSERT_ID(), ?, ?, ?, ?)';
// Prepare the statement:
$stmt = mysqli_prepare($dbc, $sql);
// For debugging purposes:
// if (!$stmt) echo mysqli_stmt_error($stmt);
mysqli_stmt_bind_param($stmt,'sssi',$attribute_code,$color_value,$size_value,$stock_unit);
foreach($productcount['code'] as $attcode){
$attribute_code=$attcode;
}
foreach($productcount['color'] as $attcolor){
$color_value=$attcolor;
}
foreach($productcount['size'] as $attsize){
$size_value=$attsize;
}
foreach($productcount['stock'] as $attstock){
$stock_unit=$attstock;
}
foreach($productcount['attcode'] as $attcode){
$attcode;
}
// Execute the query:
mysqli_stmt_execute($stmt);
$stmt->close();
}
테이블
INSERT INTO TableName(
foo_field,
bar_field
)
VALUES
(foo1, bar1),
(foo2, bar2),
(foo3, bar3),
(foo4, bar4)
이 방법의 단점은 준비된 문을 사용할 수 없어 주입에 대한 기본 보호 기능이 추가된다는 것입니다.
또는 준비된 문을 만든 다음 루프에서 매개 변수를 사용하여 실행할 수 있습니다. 이것은 더 느린 방법이지만 데이터를 삽입하기 전에 데이터를 수동으로 위생 처리 할 필요가 없습니다. 당신의 $product
배열은 다음과 같이하면
당신은 아무것도 시도? – Indranil
메신저 stmt 함께 삽입하려고하지만이 일을 더 나은 방법이 궁금하다면 –