简体   繁体   English

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

[英]Insert data into 2 seperate mySQL tables

Below is a simplified version of what i am trying to do.下面是我正在尝试做的简化版本。 Essentially i have 2 registration forms and the content of each form is to be written to a different table.基本上我有 2 个注册 forms 并且每个表单的内容都将写入不同的表。 When i load the form i get many errors undefined index on line xx and i still get the errors on this example with line 75 and 84, i think the errors are down to some of the fields being "required" but that page hasnt loaded as its floated away using some JS.当我加载表单时,我在第 xx 行收到许多错误未定义索引,并且在第 75 行和第 84 行的此示例中仍然出现错误,我认为错误归结为某些“必需”字段,但该页面尚未加载为它使用一些 JS 飘走了。 ... Please help! ... 请帮忙!

<?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')");
    
}
?>

Since you're only submitting one format a time, you can't insert into both tables.由于您一次只提交一种格式,因此您不能同时插入两个表格。 You need to check which form was submitted, and insert into the corresponding table.您需要检查提交了哪个表单,并插入到相应的表中。 Each form needs a submit button with a different name or value, so you can tell which one was used in PHP.每个表单都需要一个具有不同名称或值的提交按钮,因此您可以知道 PHP 中使用了哪个。

You also should use prepared statements rather than substituting variables into SQL, to protect against SQL injection.您还应该使用准备好的语句而不是将变量替换为 SQL,以防止 SQL 注入。 I've shown how to do this.我已经展示了如何做到这一点。

<?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);
 }
 ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM