【发布时间】:2016-06-12 02:53:13
【问题描述】:
我想在 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
我找不到错误,请帮我找到它..
【问题讨论】:
-
标签表没有'PRIMARY KEY'
-
mysqli_query() 执行 one 语句。但是例如
INSERT ... ; INSERT ...;是两个语句。见docs.php.net/manual/en/mysqli.quickstart.multiple-statement.php |但究竟为什么你需要多个语句,尤其是。当他们都打同一张桌子时? -
使用mysqli_multi_query执行多个查询
-
在你的情况下,只有条件才会执行。为什么要写 if 和 else??
-
我想在 for 循环中将多条记录插入 MySQL....