mysql同时向两张表中添加数据 将数据插入2个独立的MySQL表

来源:佚名

如何解决将数据插入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 = @R_301_5162@i_connect("localhost","root","","test-sql"); //connection variables
  
  $timezone = date_default_timezone_set("Europe/London");
  
   if(@R_301_5162@i_connect_errno()){
        echo "Failed to connect:" . @R_301_5162@i_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 = @R_301_5162@i_query($con,"INSERT INTO inf VALUES('','$inf_fname','$inf_lname')");
    $query = @R_301_5162@i_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 (?,$cust_fname,$cust_lname);
     mysqli_stmt_execute($stmt);
 }
 ?>
您的足迹: