外键的数据库插入问题

时间:2019-01-14 17:17:13

标签: php database

我想学习创建自己的表单,并通过示例制作了一个表单。在数据库(公司)中,我有3个表,雇员(员工编号,姓名,地址,生日),bill_rates(帐单编号,bill)和最后一个项目(project_name,start_date)。

一个项目有一个或多个员工。而且帐单也已连接到项目。

我是否必须在项目中将bill_id和employee_id用作外键?

这是我的数据库。

employee table

bill_rates

project

我做了什么。我将bill_id和employee_id外键设置为项目表。

在我的表单中,我尝试从数据库中插入。该日志在url中成功显示,但在数据库中仅插入了employee表和braterates,而没有插入project表中。

这是我的表单和process.php的屏幕截图

Company

这是我插入数据过程的代码

<?php
if(isset($_POST['submit'])){
    include_once 'dbConnect.php';
    //employees
    $employee_id =  mysqli_real_escape_string($conn, $_POST['employee_id']);
    $project_id =   $_POST['project_id'];
    $bill_id = $_POST['bill_id'];
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $address = mysqli_real_escape_string($conn, $_POST['address']);
    $birthdate = mysqli_real_escape_string($conn, $_POST['birthdate']);
    //project employee_id, bill_id
    $project_name = mysqli_real_escape_string($conn, $_POST['project_name']);
    $start_date = mysqli_real_escape_string($conn, $_POST['start_date']);
    //bill
    $bill = mysqli_real_escape_string($conn, $_POST['bill']);

    if(empty($name) || empty($address) || empty($birthdate) || empty($project_name) || empty($start_date)  || empty($bill) ){
        header("Location: index.php?register=empty");
        exit();
    }else{
        //to the DB we gow
        $sql = "INSERT INTO employee (name, address, birthdate) VALUES ('$name','$address','$birthdate');";
        mysqli_query($conn,$sql);
        $sql2 = "INSERT INTO project (`project_name`, `start_date`) VALUES ('$project_name','$start_date');";
        mysqli_query($conn,$sql2);
        $sql3 = "INSERT INTO billing_rates (bill) VALUES ('$bill');";
        mysqli_query($conn,$sql3);
        echo '<p class="text-danger">'.'Success!'.'</p>';
        header("Location: index.php?register=success");
    }

}


?>

我想确定要使用的查询,因为我认为我的方法是错误的。

0 个答案:

没有答案