Я хочу создать инструкцию insert mysql в цикле for. I'm looking for to insert multiple records at a time.
php INSERT data to sql in for loop
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
этикетку Tabel:
Field Type null
item_id int(11) NO
label varchar(50) NO
я не могу найти ошибку, пожалуйста, помогите мне найти его ..
таблица меток не имеет «PRIMARY KEY» –
mysqli_query() выполняет оператор _one_. Но, например, 'INSERT ...; INSERT ...; '- два утверждения. см. http://docs.php.net/manual/en/mysqli.quickstart.multiple-statement.php | Но почему именно вам нужно несколько операторов, особенно. когда все они попали в один стол? – VolkerK
Используйте [mysqli_multi_query] (http://php.net/manual/en/mysqli.multi-query.php), чтобы выполнить несколько запросов. – Saty