gpt4 book ai didi

php - 使用一个html表单插入两个sql表

转载 作者:行者123 更新时间:2023-11-29 10:01:16 26 4
gpt4 key购买 nike

我正在尝试使用一种表单将数据插入到两个单独的 mysql 表中。

在下面的 php 代码中,我有两个 INSERT 语句,一个用于桌面游戏,一个用于桌面结果。如果我单独使用“INSERT INTO Games ...”或“INSERT INTO Results ...”语句运行代码(修改表单以仅包含相应的一个或两个相应字段),则会插入数据正确存入数据库。当我尝试同时做这件事时,我无法弄清楚我做错了什么。只是要注意,当我在 phpmyadmin 中一起运行两个 INSERT 语句时,数据会正确插入到两个表中。

我已经阅读了有关 mysqli_multi_query 的内容,并尝试了各种选项来使用它,但没有任何结果,因为我不确定如何将它与我正在使用的以下准备语句一起使用。

if($stmt = mysqli_prepare($link, $sql))

这是 php 部分:

<?php

// Define variables and initialize with empty values
$tourn_id = "";
$game_id = "";
$player_id = "";
$tourn_name_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

// Check input errors before inserting in database
if(empty($tourn_name_err)){

// Prepare an insert statement
$sql = "INSERT INTO Games (idTournaments) VALUES (?);
INSERT INTO Results (idGames, idPlayers) VALUES (?, ?);";

if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "iii", $param_tourn_id, $param_game_id, $param_player_id);

// Set parameters
$param_tourn_id = trim($_POST["tourn_id"]);
$param_game_id = trim($_POST["game_id"]);
$param_player_id = trim($_POST["player_id"]);

// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Redirect to login page
header("location: welcome.php");
} else{
echo "Something went wrong. Please try again later.";
}
}

// Close statement
mysqli_stmt_close($stmt);
}

// Close connection
mysqli_close($link);
}
?>

这是 HTML 表单部分:

<div class="wrapper">
<form class="form-signin" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<h1 class="form-signin-heading">New Match</h1>

<div class="form-group <?php echo (!empty($tourn_name_err)) ? 'has-error' : ''; ?>">
<label>Enter Tournament ID here ...:</label>
<input type="number" name="tourn_id"class="form-control" value="<?php echo $tourn_id; ?>">
<span class="help-block"><?php echo $tourn_name_err; ?></span>
</div>

<div class="form-group <?php echo (!empty($tourn_name_err)) ? 'has-error' : ''; ?>">
<label>Enter Game ID here ...:</label>
<input type="number" name="game_id"class="form-control" value="<?php echo $game_id; ?>">
<span class="help-block"><?php echo $tourn_name_err; ?></span>
</div>

<div class="form-group <?php echo (!empty($tourn_name_err)) ? 'has-error' : ''; ?>">
<label>Enter Player ID here ...:</label>
<input type="number" name="player_id"class="form-control" value="<?php echo $player_id; ?>">
<span class="help-block"><?php echo $tourn_name_err; ?></span>
</div>

<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit">
<input type="reset" class="btn btn-default" value="Reset">
</div>
</form>
</div>

最佳答案

好的,经过多次尝试错误并删除了原始 php 代码中的“Prepare statements”,让它开始工作。我还尽可能地遵循手册中的 mysqli_multi_query 示例。

我不确定设置如下参数是否必要。

$param_tourn_id = trim($_POST["tourn_id"]);

这里是完整的 php 代码:

<?php

// Define variables and initialize with empty values
$tourn_id = "";
$game_id = "";
$player1_id = "";
$tourn_name_err = "";


// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){


// Set parameters
$param_tourn_id = trim($_POST["tourn_id"]);
$param_game_id = trim($_POST["game_id"]);
$param_player1_id = trim($_POST["player1_id"]);


// Prepare an insert statement
$sql = "INSERT INTO Games (idTournaments) VALUES ($param_tourn_id);";
$sql .= "INSERT INTO Results (idGames, idPlayers) VALUES ($param_game_id, $param_player1_id)";


/* execute multi query */
if (mysqli_multi_query($link, $sql)) {
do {
/* store first result set */
if ($result = mysqli_store_result($link)) {
while ($row = mysqli_fetch_row($result));
mysqli_free_result($result);
}
/* print divider */
if (mysqli_more_results($link));
} while (mysqli_next_result($link));
}

/* close connection */
mysqli_close($link);

}

?>

关于php - 使用一个html表单插入两个sql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52904478/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com