我有一个函数可以将数据插入SQL表。
public function process_form(){
# Start function when submit button is clicked
if (isset($_POST['submit'])) {
# Assign form data
$host = $_SESSION['logged_user'];
$game = str_replace('g','',$_POST['game']);
$region = str_replace('r','',$_POST['region']);
$name = $_POST['match-name'];
$mode = str_replace('mode_0','',$_POST['mode']);
$bid = $_POST['bid'];
# SQL query
$q = '
INSERT INTO matches(
match_id,
host,
game,
region,
match_name,
match_mode,
started,
start_time,
finished,
finish_time,
validated,
team_a,
team_b,
bid
)
VALUES (
null,
?,
?,
?,
?,
?,
0,
null,
0,
null,
0,
null,
null,
?
);
';
# Process SQL
if ($stmt = $this->db->prepare($q)) {
$stmt->bind_param('iiisii',$host,$game,$region,$name,$mode,$bid);
$stmt->bind_result()
$stmt->execute(VARIABLE_GOES_HERE);
$stmt->close();
}
}
}#endfunc(process_form)
以及收集所需数据的表单,该表单具有操作属性,可将用户重定向到显示插入数据的下一页。
<form method="POST" action="match.php?id=VARIABLE_GOES_HERE">
我想在$q中扩展我的SQL查询,这将给我当前创建的行的Id,然后在$stmt->bind_result()中用Id填充变量,然后将其添加到form元素的action属性中。
需要的是添加这行代码:$last_id = $stmt->insert_id;
if ($stmt = $this->db->prepare($q)) {
$stmt->bind_param('iiisii',$host,$game,$region,$name,$mode,$bid);
$stmt->execute();
$last_id = $stmt->insert_id;
$stmt->close();
然后在这个表单中:
<form method="POST" action="match.php?id='.$last_id.'">