为了账号安全,请及时绑定邮箱和手机立即绑定

将数据插入 2 个单独的 mySQL 表

将数据插入 2 个单独的 mySQL 表

明月笑刀无情 2022-12-29 16:20:43
以下是我正在尝试做的简化版本。基本上我有 2 个注册表,每个表单的内容将写入不同的表。当我加载表单时,我在第 xx 行收到许多未定义索引错误,并且我仍然在第 75 行和第 84 行的这个示例中收到错误,我认为错误归因于某些字段是“必需的”,但该页面尚未加载为它使用一些 JS 飘走了。... 请帮忙!<?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    $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>
查看完整描述

1 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

由于您一次只提交一种格式,因此您不能插入到两个表中。您需要检查提交的是哪个表单,并插入到相应的表中。每个表单都需要一个具有不同名称或值的提交按钮,因此您可以分辨出在 PHP 中使用了哪一个。


您还应该使用准备好的语句而不是将变量替换到 SQL 中,以防止 SQL 注入。我已经展示了如何做到这一点。


<?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>

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

    </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="cust_submit" value="Register">

                        

    </form>

</div>

</body>


</html>


 <?php


 $inf_fname = "";

 $cust_fname = "";

 $inf_lname = "";

 $cust_lname = "";


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


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

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

     $_SESSION['Inf_fname'] = $inf_fname;

     $inf_lname = strip_tags($_POST['Inf_lname']);

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

     $_SESSION['Inf_lname'] = $inf_lname;


     $stmt = mysqli_prepare($con, "INSERT INTO inf VALUES (?, ?)");

     mysqli_stmt_bind_param($stmt, "ss", $inf_fname, $inf_lname);

     mysqli_stmt_execute($stmt);

 } elseif (isset($_POST['cust_submit'])) {


     $cust_fname = strip_tags($_POST['Cust_fname']);

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

     $_SESSION['Cust_fname'] = $cust_fname;

     $cust_lname = strip_tags($_POST['Cust_lname']);

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

     $_SESSION['Cust_lname'] = $cust_lname;


     $stmt = mysqli_prepare($con, "INSERT INTO customer VALUES (?, ?)");

     mysqli_stmt_bind_param($stmt, "ss", $cust_fname, $cust_lname);

     mysqli_stmt_execute($stmt);

 }

 ?>



查看完整回答
反对 回复 2022-12-29
  • 1 回答
  • 0 关注
  • 132 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号