猿问

将数据插入到 2 个独立的 mySQL 表中

下面是我正在尝试做的事情的简化版本。本质上我有 2 个注册表,每个表单的内容都将写入不同的表。当我加载表单时,我在第 xx 行上收到许多错误未定义索引,并且在本示例的第 75 行和第 84 行中仍然收到错误,我认为错误归结于某些“必需”字段,但该页面尚未加载为它使用一些 JS 就飘走了。... 请帮忙!


<?php

ob_start(); //Turns on output buffering

session_start(); //This starts a session variable where it store the input so the user doesnt have to start over if they get an error

$con = mysqli_connect("localhost","root","","test-sql"); //connection variables

  

  $timezone = date_default_timezone_set("Europe/London");

  

   if(mysqli_connect_errno()){

        echo "failed to connect:" . mysqli_connect_errno();

    }

?>


<html>

    <head>

            <! here we add the links and the jquery>

    <title>Register Project</title>

    </head>

    <body>

        

        <div id="first">

                    <form action="test-sql.php" method="POST">

                        <input type="text" name="Inf_fname" placeholder="First Name" required>

                        <br>

                        <input type="text" name="Inf_lname" placeholder="Last Name" required>

                        </form>

                    </div>

        

        <div id="second">

                

                    <form action="test-sql.php" method="POST">

                        

                       <br>

                    <input type="text" name="Cust_fname" placeholder="First Name" required>

                    <br>

                                               

                    <input type="text" name="Cust_lname" placeholder="Last Name" required>

                    <br>

    

                    <input type="submit" name="register_button" value="Register">

                        

                       </form>

                    </div>

    </body>

    

</html>


<?php


$inf_fname = "";

$cust_fname = "";

$inf_lname = "";

$cust_lname = "";


if(isset($_POST['register_button'])) {

    

    $inf_fname = strip_tags($_POST['Inf_fname']);

    $inf_fname = str_replace(' ', '', $inf_fname);

    $_SESSION['Inf_fname'] = $inf_fname;

    

}



翻阅古今
浏览 82回答 1
1回答

慕码人8056858

由于您一次仅提交一种格式,因此无法插入到两个表中。您需要检查提交的是哪个表单,并插入到相应的表中。每个表单都需要一个具有不同名称或值的提交按钮,这样您就可以知道在 PHP 中使用了哪一个。您还应该使用准备好的语句,而不是将变量替换到 SQL 中,以防止 SQL 注入。我已经展示了如何做到这一点。<?phpob_start(); //Turns on output bufferingsession_start(); //This starts a session variable where it store the input so the user doesnt have to start over if they get an error$con = mysqli_connect("localhost","root","","test-sql"); //connection variables&nbsp;&nbsp;$timezone = date_default_timezone_set("Europe/London");&nbsp;&nbsp;if(mysqli_connect_errno()){&nbsp; &nbsp; echo "failed to connect:" . mysqli_connect_errno();}?><html><head><! here we add the links and the jquery><title>Register Project</title></head><body>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<div id="first">&nbsp; &nbsp; <form action="test-sql.php" method="POST">&nbsp; &nbsp; <input type="text" name="Inf_fname" placeholder="First Name" required>&nbsp; &nbsp; <br>&nbsp; &nbsp; <input type="text" name="Inf_lname" placeholder="Last Name" required>&nbsp; &nbsp; <input type="submit" name="inf_submit" value="Register">&nbsp; &nbsp; </form></div><div id="second">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <form action="test-sql.php" method="POST">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <br>&nbsp; &nbsp; <input type="text" name="Cust_fname" placeholder="First Name" required>&nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <input type="text" name="Cust_lname" placeholder="Last Name" required>&nbsp; &nbsp; <br>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <input type="submit" name="cust_submit" value="Register">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </form></div></body></html>&nbsp;<?php&nbsp;$inf_fname = "";&nbsp;$cust_fname = "";&nbsp;$inf_lname = "";&nbsp;$cust_lname = "";&nbsp;if(isset($_POST['inf_submit'])) {&nbsp; &nbsp; &nbsp;$inf_fname = strip_tags($_POST['Inf_fname']);&nbsp; &nbsp; &nbsp;$inf_fname = str_replace(' ', '', $inf_fname);&nbsp; &nbsp; &nbsp;$_SESSION['Inf_fname'] = $inf_fname;&nbsp; &nbsp; &nbsp;$inf_lname = strip_tags($_POST['Inf_lname']);&nbsp; &nbsp; &nbsp;$inf_lname = str_replace(' ', '', $inf_lname);&nbsp; &nbsp; &nbsp;$_SESSION['Inf_lname'] = $inf_lname;&nbsp; &nbsp; &nbsp;$stmt = mysqli_prepare($con, "INSERT INTO inf VALUES (?, ?)");&nbsp; &nbsp; &nbsp;mysqli_stmt_bind_param($stmt, "ss", $inf_fname, $inf_lname);&nbsp; &nbsp; &nbsp;mysqli_stmt_execute($stmt);&nbsp;} elseif (isset($_POST['cust_submit'])) {&nbsp; &nbsp; &nbsp;$cust_fname = strip_tags($_POST['Cust_fname']);&nbsp; &nbsp; &nbsp;$cust_fname = str_replace(' ', '', $cust_fname);&nbsp; &nbsp; &nbsp;$_SESSION['Cust_fname'] = $cust_fname;&nbsp; &nbsp; &nbsp;$cust_lname = strip_tags($_POST['Cust_lname']);&nbsp; &nbsp; &nbsp;$cust_lname = str_replace(' ', '', $cust_lname);&nbsp; &nbsp; &nbsp;$_SESSION['Cust_lname'] = $cust_lname;&nbsp; &nbsp; &nbsp;$stmt = mysqli_prepare($con, "INSERT INTO customer VALUES (?, ?)");&nbsp; &nbsp; &nbsp;mysqli_stmt_bind_param($stmt, "ss", $cust_fname, $cust_lname);&nbsp; &nbsp; &nbsp;mysqli_stmt_execute($stmt);&nbsp;}&nbsp;?>
随时随地看视频慕课网APP
我要回答