php将数据INSERT到for循环中的sql

时间:2016-02-29 06:31:39

标签: php mysql

我想在for循环中创建一个mysql insert语句。 I'm looking for to insert multiple records at a time.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$label =htmlspecialchars( $_POST["label"]);
$splitLabel = explode(" ", $label);//split the label to a array
}
//.....insert another data, getting the $last_id here

$sql = $result = "";
for ($i =0; $i< count($splitLabel); $i++){
    if ($i < count($splitLabel)){
        $sql .= "INSERT INTO label (item_id, label)
        VALUES ('".$last_id."', '".$splitLabel[$i]."');";
    }else{
        $sql .= "INSERT INTO label (item_id, label)
        VALUES ('".$last_id."', '".$splitLabel[$i]."')";
    }
}
$result = mysqli_query($conn, $sql);

我收到了关于

的错误
 check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO label (item_id, label)
    VALUES ('13', 'tin');INSERT INTO label (' at line 2

标签表:

Field      Type           null
item_id    int(11)         NO
label      varchar(50)     NO

我找不到错误,请帮我找到它..

6 个答案:

答案 0 :(得分:4)

mysqli_query()只能执行一个语句,但是您要发送多个语句。您可以使用mysqli_multi_query(),但....

最好使用prepared statement + parameters
像是这样的东西

if ( isset($_POST["label"]) ) {
    $stmt = $conn->prepare('INSERT INTO label (item_id, label) VALUES (?,?)')
    if ( !$stmt ) {
        someErrorHandler( $conn->error );
    }
    else if ( !$stmt->bind_param('ss', $last_id, $label) ) {
        someErrorHandler( $stmt->error );
    }
    else {
        // I have no idea where this $last_id comes from ....
        foreach( explode(' ', $_POST["label"]) as $label ) {
            if ( !$stmt->execute() ) {
              someErrorHandler( $stmt->error );
            }
        }
    }
}

答案 1 :(得分:2)

您只需使用mysqli_multi_query()进行多次查询。

$sql = "";
$result = "";

for ($i =0; $i< count($splitLabel); $i++){
    if ($i < count($splitLabel)){
        $sql .= " INSERT INTO label (item_id, label)
        VALUES ('".$last_id."', '".$splitLabel[$i]."');";
    }else{
        $sql .= " INSERT INTO label (item_id, label)
        VALUES ('".$last_id."', '".$splitLabel[$i]."');";
    }
}
$result = mysqli_multi_query($conn, $sql);

对于此操作,您不能对单个mysqli_query使用多个INSERT,您可以使用mysqli_multi_query()执行多个查询。

答案 2 :(得分:2)

对于多个查询执行,您可以使用mysqli_multi_query()

对于使用单个查询对表进行多次插入,就像这样multi insert: -

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

所以你可以试试

$sqlInsert   = '';
$countOfData = count($splitLabel);
for ($i = 0; $i < $countOfData; $i++){
    $sqlInsert .= "('{$last_id}', '{$splitLabel[$i]}'),";
}
$sqlInsert = rtrim($sqlInsert, ',');//remove the extra comma
if ($sqlInsert) {
    $sql = "INSERT INTO label (item_id, label) VALUES {$sqlInsert} ;";
    $result = mysqli_query($conn, $sql);
}

答案 3 :(得分:1)

试试此代码

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$label =htmlspecialchars( $_POST["label"]);
$splitLabel = explode(" ", $label);//split the label to a array
}
//.....insert another data, getting the $last_id here

$sql = $result = "";
for ($i =0; $i< count($splitLabel); $i++){
if ($i < count($splitLabel)){
    $sql .= "INSERT INTO label (item_id, label) VALUES ('$last_id', '$splitLabel[$i]')";
}else{
    $sql .= "INSERT INTO label (item_id, label)
    VALUES ('$last_id', '$splitLabel[$i]')";
}
}

答案 4 :(得分:0)

    $sql .= "INSERT INTO label (item_id, label)
    VALUES ('".$last_id."', '".$splitLabel[$i]."');";

你错过了#34 ;;&#34;

答案 5 :(得分:-1)

试试这个。

   $sql = $result = "INSERT INTO label (item_id, label) VALUES ";
   for ($i =0; $i< count($splitLabel); $i++){    
        $sql .= "('".$last_id."', '".$splitLabel[$i]."')";
        if ($i < count($splitLabel) - 1){
            sql .= ",";
        }
    }