Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用一个html表单插入到两个sql表中_Php_Mysql - Fatal编程技术网

Php 使用一个html表单插入到两个sql表中

Php 使用一个html表单插入到两个sql表中,php,mysql,Php,Mysql,我试图使用一种表单将数据插入两个单独的mysql表中 在下面的php代码中,我有两条INSERT语句,一条用于表格游戏,另一条用于表格结果。如果我单独使用“INSERT INTO Games…”或“INSERT INTO Results…”语句运行代码(修改表格以仅包含相应的一个或两个相应字段),数据将正确插入数据库。当我试图同时做这件事时,我不知道我做错了什么。请注意,当我在phpmyadmin中同时运行两个INSERT语句时,数据会正确地插入到两个表中 我已经阅读了有关mysqli_mult

我试图使用一种表单将数据插入两个单独的mysql表中

在下面的php代码中,我有两条INSERT语句,一条用于表格游戏,另一条用于表格结果。如果我单独使用“INSERT INTO Games…”或“INSERT INTO Results…”语句运行代码(修改表格以仅包含相应的一个或两个相应字段),数据将正确插入数据库。当我试图同时做这件事时,我不知道我做错了什么。请注意,当我在phpmyadmin中同时运行两个INSERT语句时,数据会正确地插入到两个表中

我已经阅读了有关mysqli_multi_query的信息,并尝试了各种选项来使用它,但没有任何结果,因为我不知道如何将它与我正在使用的以下prepare station一起使用

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语句”后,它开始工作了。我还尽可能多地遵循手册中的mysqli_multi_查询示例

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

$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);

    }

?>


我将构建这两个
插入
单独的。先执行第一个,然后紧接着执行第二个。你为什么要把它们结合起来呢。。。?插入后,数据库中仍然会有相同的数据,并且仍然只从1个表单提交执行。Hm!好的,我要重复代码的哪一部分?我尝试让每个INSERT独立运行,并重复了在“//prepareaninsert语句”和“//Close语句”之间找到的代码,但它仍然不起作用。