是否可以在 php for 循环中使用 LAST_INSERT_ID()?
Is it possible to use LAST_INSERT_ID() in php for-loop?
可以在 php for 循环中使用 LAST_INSERT_ID()
。我需要在 guardian
table 中获取最后一个 playground
PK 作为 FK。两者需要同时插入。请原谅我没有使用 PDO
,我只想让这个东西先工作。
$query = "INSERT INTO playground (parent, children, amount) VALUES ('John','susy','2000');";
$levelarray = array ("One", "Two", "Three");
for ($i = 0; $i < count($levelarray); $i++) {
$level = $levelarray[$i];
$query .= "INSERT INTO guardian (playgroundid, level) VALUES (LAST_INSERT_ID(),'$level');";
}
mysqli_multi_query($con, $query);
这个我也试过了。但是下面这个输出的是最后一个id,而不是新插入的id。
$query = "INSERT INTO playground (parent, children, amount) VALUES ('John','Susy','2000');";
$sql = "SELECT playground_id AS playgroundid FROM playground ORDER BY playground_id DESC LIMIT 1";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result);
$playId = $row['playgroundid'];
$levelarray = array ("One", "Two", "Three");
for ($i = 0; $i < count($levelarray); $i++) {
$level = $levelarray[$i];
$query .= "INSERT INTO guardian (playgroundid, level) VALUES ('$playId','$level');";
}
mysqli_multi_query($con, $query);
不,这行不通。第一个守护者会得到playground的id,后面两个会得到前一个守护者的id。
改为使用 mysqli_insert_id()
获取 ID 并将其保存为 for 循环之外的变量。
您需要使用 mysqli_insert_id()
获取最后插入的 ID
然后将其用于下一个查询,如下所示:-
$query = "INSERT INTO playground (parent, children, amount) VALUES ('John','susy','2000');";
if(mysqli_query($con,$query)){
$id = mysqli_insert_id($con); //get last inserted id
$levelarray = array ("One", "Two", "Three");
for ($i = 0; $i < count($levelarray); $i++) {
$level = $levelarray[$i];
$query = "INSERT INTO guardian (playgroundid, level) VALUES ($id,'$level');";
mysqli_query($con,$query) ;
}
}
或者您仍然可以像下面这样使用 mysqli_multi_query()
:-
$query = "INSERT INTO playground (parent, children, amount) VALUES ('John','susy','2000');";
if(mysqli_query($con,$query)){
$id = mysqli_insert_id($con); //get last inserted id
$query1 = '';
$levelarray = array ("One", "Two", "Three");
for ($i = 0; $i < count($levelarray); $i++) {
$level = $levelarray[$i];
$query1 .= "INSERT INTO guardian (playgroundid, level) VALUES ($id,'$level');";
}
mysqli_multi_query($con, $query1);
}
注意:- 您的查询对 SQL INJECTION
开放。所以尝试使用 prepared statement
来防止它。
可以在 php for 循环中使用 LAST_INSERT_ID()
。我需要在 guardian
table 中获取最后一个 playground
PK 作为 FK。两者需要同时插入。请原谅我没有使用 PDO
,我只想让这个东西先工作。
$query = "INSERT INTO playground (parent, children, amount) VALUES ('John','susy','2000');";
$levelarray = array ("One", "Two", "Three");
for ($i = 0; $i < count($levelarray); $i++) {
$level = $levelarray[$i];
$query .= "INSERT INTO guardian (playgroundid, level) VALUES (LAST_INSERT_ID(),'$level');";
}
mysqli_multi_query($con, $query);
这个我也试过了。但是下面这个输出的是最后一个id,而不是新插入的id。
$query = "INSERT INTO playground (parent, children, amount) VALUES ('John','Susy','2000');";
$sql = "SELECT playground_id AS playgroundid FROM playground ORDER BY playground_id DESC LIMIT 1";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result);
$playId = $row['playgroundid'];
$levelarray = array ("One", "Two", "Three");
for ($i = 0; $i < count($levelarray); $i++) {
$level = $levelarray[$i];
$query .= "INSERT INTO guardian (playgroundid, level) VALUES ('$playId','$level');";
}
mysqli_multi_query($con, $query);
不,这行不通。第一个守护者会得到playground的id,后面两个会得到前一个守护者的id。
改为使用 mysqli_insert_id()
获取 ID 并将其保存为 for 循环之外的变量。
您需要使用 mysqli_insert_id()
获取最后插入的 ID然后将其用于下一个查询,如下所示:-
$query = "INSERT INTO playground (parent, children, amount) VALUES ('John','susy','2000');";
if(mysqli_query($con,$query)){
$id = mysqli_insert_id($con); //get last inserted id
$levelarray = array ("One", "Two", "Three");
for ($i = 0; $i < count($levelarray); $i++) {
$level = $levelarray[$i];
$query = "INSERT INTO guardian (playgroundid, level) VALUES ($id,'$level');";
mysqli_query($con,$query) ;
}
}
或者您仍然可以像下面这样使用 mysqli_multi_query()
:-
$query = "INSERT INTO playground (parent, children, amount) VALUES ('John','susy','2000');";
if(mysqli_query($con,$query)){
$id = mysqli_insert_id($con); //get last inserted id
$query1 = '';
$levelarray = array ("One", "Two", "Three");
for ($i = 0; $i < count($levelarray); $i++) {
$level = $levelarray[$i];
$query1 .= "INSERT INTO guardian (playgroundid, level) VALUES ($id,'$level');";
}
mysqli_multi_query($con, $query1);
}
注意:- 您的查询对 SQL INJECTION
开放。所以尝试使用 prepared statement
来防止它。