多次插入

时间:2017-06-06 12:47:06

标签: php mysql

获得以下错误

  

插入多条记录时出错:SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在'INSERT INTO enquiry_master(enq_no,enq_dt,name,contact_no,email_id,referen')第6行附近使用正确的语法

部分代码。我想插入多条记录。如果不使用.=

,则不会错误输入第一条记录
$row = $result->fetch_assoc();

$col_1 = $row["enq_no"];
$col_2 = $row["enq_dt"];
$col_3 = $row["name"];
$col_4 = $row["contact_no"];
$col_5 = $row["email_id"];
$col_6 = $row["reference_from"];
$col_7 = $row["subject"];
$col_8 = $row["msg"];

$sql_insert_2 .= "INSERT INTO enquiry_master(enq_no,enq_dt,name,
                        contact_no,email_id,reference_from,
                        subject,msg)
                VALUES('$col_1','$col_2','$col_3',
                        '$col_4','$col_5','$col_6',
                        '$col_7','$col_8')";


if($master_ctr ==2 )
{       
    $conn_insert =mysqli_connect("host", "user", "pass", "db");

    if (!mysqli_query($conn_insert, $sql_insert_2)) 
    {
         echo "Error inserting multiple record: " . mysqli_error($conn_insert);
    } 
    mysqli_close($conn_insert); 
}

1 个答案:

答案 0 :(得分:1)

您可以使用这样的批量插入查询:

INSERT INTO enquiry_master(enq_no,enq_dt,name,contact_no,email_id,reference_from,subject,msg)
VALUES('$col_1','$col_2','$col_3','$col_4','$col_5','$col_6','$col_7','$col_8'),
   ('$col_1','$col_2','$col_3','$col_4','$col_5','$col_6','$col_7','$col_8'),
   ('$col_1','$col_2','$col_3','$col_4','$col_5','$col_6','$col_7','$col_8')
";