【问题标题】:Is there a way to insert data to multiple tables? php mysql有没有办法将数据插入多个表? php mysql
【发布时间】:2020-06-22 14:51:15
【问题描述】:

我一直在尝试将数据从我的网页插入到数据库中的多个表中,但我真的想不通!

我有这个页面,用户可以在其中下订单,首先他需要获取客户详细信息:

name, age, email etc.

还有他的汽车详细信息:

the car model and the year of fabrication

在我的数据库中,我有 2 个链接的表 1 - Clients(Client_id (PK), name, email, id_Car(fK),第二个是 Cars (id_Car, model, year_of_fabrication)。

我的主键是 Auto_Incremented,所以我不必传递值。

我认为问题出在我的外键上,因为从网站上我没有将任何值传递给 Clients 表,但我真的不知道如何同时将数据插入到 clients 和 cars 表中。

这是我的订单表格。

<form action="include/comanda.inc.php">
    <div class="comanda"> 
        <h2 id="titlu">Detaliile clientului:</h2>

        <label for="client">Numele clientului: </label>
        <input type="text" name="client">

        <label for="client">CNP: </label>
        <input type="text" name="cnp">

        <label for="client">Sex: </label>
        <select name="sex" id="sex">
            <option value="M">M</option>
            <option value="F">F</option>
        </select>

        <label for="client">Email: </label>
        <input type="text" name="email">

        <label for="client">Sector: </label>
        <select name="sector" id="sectorCl">
            <option value="1">Sector 1</option>
            <option value="2">Sector 2</option>
            <option value="3">Sector 3</option>
            <option value="4">Sector 4</option>
            <option value="5">Sector 5</option>
            <option value="6">Sector 6</option>
        </select>

    
        <label for="client">Marca masina: </label>
        <input type="text" name="masina">

        <!-- DROPDOWN LIST CU ANII DE LA 2020 LA 2000-->
        <label for="client">Anul Fabricatiei: </label>
        <select name="an_fabricatie" id="an_masina">
        <option value="2020">2020</option>
        <option value="2019">2019</option>
        <option value="2018">2018</option>
        <option value="2017">2017</option>
        <option value="2016">2016</option>
        <option value="2015">2015</option>
        <option value="2014">2014</option>
        <option value="2013">2013</option>
        <option value="2012">2012</option>
        <option value="2011">2011</option>
        <option value="2010">2010</option>
        </select>

这是我的订单脚本:

<?php 
if(isset($_POST["creaza_comanda"])){
    require 'dbh.inc.php';

    $nume = $_POST["client"];
    $cnp = $_POST["cnp"];
    $sex = $_POST["sex"];
    $email = $_POST["email"];
    $sector = $_POST["sector"];
    $masina = $_POST["masina"];
    $an_fabricatie = $_POST["an_fabricatie"];
    $angajat = $_POST["nume-angajat"];
    $den_furnizor = $_POST["den_furnizor"];
    $oras_furnizor = $_POST["oras_furnizor"];
    $piesa = $_POST["productName"];
    $pret = $_POST["price"];

    if(!preg_match("/^[a-zA-Z ]*$/", $nume)){
        header("Location: ../comanda_noua.php?eroare");
        exit();
    } else if(!preg_match("/^[0-9]*$/",$cnp)){
        header("Location: ../comanda_noua.php?msg2");
        exit();
    } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
        header("Location: ../comanda_noua.php?msg2");
        exit();
    } else {
        
       $sql = "INSERT INTO masini (marca, an_fabricatie) VALUES ('$masina', $an_fabricatie);";
       
       if(mysqli_query($conn,$sql)){
           header("Location: ../comanda_noua.php?succes");
           exit();
       } else{
            echo "Error ". $sql . ":-" . mysqli_error($conn);
       }
       mysqli_close($conn);
    }
    header("Location: ../comanda_noua.php");
    exit;
}

【问题讨论】:

  • 您的脚本很容易受到SQL Injection 攻击。您应该使用准备好的语句。
  • 小点 &lt;label for="client" for 属性应该将标签链接到特定的输入元素。
  • 您的脚本对SQL Injection Attack 开放。即使是if you are escaping inputs, its not safe!,您也应该考虑在MYSQLI_PDO API 中使用prepared parameterized statements,而不是连接值
  • else if(带空格)是 javascript 样式。 elseif(无空格)是 php 风格。请参阅有关ctype_digit() 而不是preg_match("/^[0-9]*$/" 功能的php 文档。永远不要向您的用户显示mysqli_error()。 @蒂米

标签: php mysql datatables sql-insert


【解决方案1】:

您只需从主表中获取最后一个插入行 ID,并将其作为外键传递给派生表。 当您使用 DOM 对象将数据插入 Cars 表时,您需要在此之后写下这一行:

$sql = "INSERT INTO masini (marca, an_fabricatie) VALUES ('$masina', $an_fabricatie);"; 
if(mysqli_query($conn,$sql)){
     $last_id = $conn->insert_id;     // this will gives you last inserted id
    // just after this you can add data to another table
    $sql1 = "INSERT INTO Clients (<column_name>) VALUES ('<values>');";  // change column name and values here;
    if(mysqli_query($conn,$sql1)){
       // after all data insert add your next logic code here.or simply redirect it.
    }
}

或者您也可以通过以下方式获取 ID:

 $last_id = mysqli_insert_id($conn);

试试这个,我想这会对你有所帮助。

【讨论】:

  • 这个答案是不稳定/不安全的,因为它没有使用带有绑定参数的准备好的语句。 OP 或研究人员不应执行此建议。
猜你喜欢
  • 1970-01-01
  • 2019-11-24
  • 1970-01-01
  • 2012-06-13
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
相关资源
最近更新 更多