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

Javascript 将数据插入2个单独的mySQL表,javascript,php,html,css,Javascript,Php,Html,Css,下面是我试图做的一个简化版本。基本上我有两个登记表,每个表的内容都要写在不同的表中。当我加载表单时,我在xx行得到了许多未定义索引的错误,我仍然在这个例子的第75行和第84行得到了错误,我认为这些错误是由于一些字段是“必需的”,但该页面没有加载,因为它使用一些JS浮动了。。。请帮忙 <?php ob_start(); //Turns on output buffering session_start(); //This starts a session variable where it

下面是我试图做的一个简化版本。基本上我有两个登记表,每个表的内容都要写在不同的表中。当我加载表单时,我在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;
    
    $cust_fname = strip_tags($_POST['Cust_fname']);
    $cust_fname = str_replace(' ', '', $cust_fname);
    $_SESSION['Cust_fname'] = $cust_fname;
    
    $inf_lname = strip_tags($_POST['Inf_lname']);
    $inf_lname = str_replace(' ', '', $inf_lname);
    $_SESSION['Inf_lname'] = $inf_lname;
    
    $cust_lname = strip_tags($_POST['Cust_lname']);
    $cust_lname = str_replace(' ', '', $cust_lname);
    $_SESSION['Cust_lname'] = $cust_lname;
    
    $query = mysqli_query($con, "INSERT INTO inf VALUES('', '$inf_fname', '$inf_lname')");
    $query = mysqli_query($con, "INSERT INTO customer VALUES('', '$cust_fname', '$cust_lname')");
    
}
?>


注册项目





因为一次只提交一种格式,所以不能同时插入两个表。您需要检查提交的表单,并将其插入相应的表中。每个表单都需要一个具有不同名称或值的提交按钮,这样您就可以知道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);
 }
 ?>

注册项目





为什么要使用两个单独的表单?一次只能提交其中一个。因此,如果他们提交第一个表单,您无法访问第二个表单的输入,反之亦然。这是两个表单,因为有两种类型的成员,因此注册表单需要不同的成员信息。有一些JS需要根据他们选择的表单来显示正确的表单。警告:您完全可以使用参数化的预处理语句,而不是手动生成查询。它们由或提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行。感谢您的快速回复,巴尔马。。这似乎没有在数据库中输入任何内容(即使inf_submit上的输入错误已修复),这可能很简单,但没有错误,因此我将进一步介绍它