簡體   English   中英

php將數據INSERT到for循環中的sql

[英]php INSERT data to sql in for loop

我想在for循環中創建一個mysql插入語句。 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

我找不到錯誤,請幫我找到它..

mysqli_query()只提供一個語句,但是你發送了多個語句。 可以使用mysqli_multi_query() ,但....

更好地使用准備好的語句+參數
像是這樣的東西

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 );
            }
        }
    }
}

您只需要使用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);

對於此操作,您不能將多個INSERT用於單個mysqli_query ,您可以使用mysqli_multi_query()來執行多個查詢。

對於多個查詢執行,您可以使用mysqli_multi_query()

對於具有單個查詢的表的多插入就像這樣的多插入 : -

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);
}

試試這個代碼

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]')";
}
}
    $sql .= "INSERT INTO label (item_id, label)
    VALUES ('".$last_id."', '".$splitLabel[$i]."');";

你錯過了 ”;”

試試這個吧。

   $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 .= ",";
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM