纠正我的php和mysql代码中的小错误

Problem: i have problem when inserting values of dept_no into emp table. Values cannot be inserted while in dept table dept_no is successfully inserted

  • Emp (emp_no,emp_name,address,phone,salary,dept_no)
  • Dept (dept_no,dept_name,location)
  • Dept-Emp are related with one-many relationship. Create a relational database for the above and perform the following:

Using above database write PHP scripts which will:

  1. Insert employee records & department records into the respective tables.
  2. Print a salary statement in the formatlocation given below, for a given department. (Accept department name from the user).

My code..

<?php

    if(isset($_POST['submit'])){
    //first name of employee
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
    //to collect errors in an array
    $errors = array();
    //employee's id
    if(empty($_POST['emp_no'])){
    $errors[] = "Please enter employee number!";
    }else{
    $emp_no = trim($_POST['emp_no']);
    }
    //department number
    if(empty($_POST['dep_no'])){
    $errors[] = "Please enter employee number!";
    }else{
    $dep_no = trim($_POST['dep_no']);
    }
    //department name
    if(empty($_POST['dep_name'])){
    $errors[] = "Please enter department name!";
    }else{
    $dep_name = trim($_POST['dep_name']);
    }
    //department location
    if(empty($_POST['dep_location'])){
    $errors[] = "Please enter department location!";
    }else{
    $dep_location = trim($_POST['dep_location']);
    }
    //employee's first name
    if(empty($_POST['first_name'])){
    $errors[] = "Please enter first name!";
    }else{
    $first_name = trim($_POST['first_name']);
    }
    //employees last name
    if(empty($_POST['last_name'])){
    $errors[] = "Please enter last name!";
    }else{
    $last_name = trim($_POST['last_name']);
    }
    //employees address
    if(empty($_POST['address'])){
    $errors[] = "Please enter address!";
    }else{
    $address = trim($_POST['address']);
    }
    //employees phone
    if(empty($_POST['phone'])){
    $errors[] = "Please enter phone number!";
    }else{
    $phone = trim($_POST['phone']);
    }
    //employees salary
    if(empty($_POST['salary'])){
    $errors[] = "Please enter phone number!";
    }else{
    $salary = trim($_POST['salary']);
    }
    //check if no errors
    if(empty($errors)){
    require('dep_emp.php');
    }

    }
?>

<form action="emp_print.php" method="POST">
<p>Employee Form</p>
<p>Employee Number <input type="number" name="emp_no" value="<?php  if(isset($_POST['submit']))echo $_POST['emp_no']; ?>"/></p>
<p>Department Number <input type="number" name="dep_no" value="<?php if(isset($_POST['submit']))echo $_POST['dep_no']; ?>"/></p>
<p>Department Name <input type="text" name="dep_name" value="<?php if(isset($_POST['submit']))echo $_POST['dep_name']; ?>"/></p>
<p>Department Location <input type="text" name="dep_location" value="<?php if(isset($_POST['submit']))echo $_POST['dep_location']; ?>"/></p>
<p>First Name <input type="text" name="first_name" value="<?php if(isset($_POST['submit']))echo $_POST['first_name']; ?>"/></p>
<p>Last Name <input type="text" name="last_name" value="<?php if(isset($_POST['submit']))echo $_POST['last_name']; ?>"/></p>
<p>Address <input type="text" name="address" value="<?php if(isset($_POST['submit']))echo $_POST['address']; ?>"/></p>
<p>Phone Number <input type="text" name="phone" value="<?php if(isset($_POST['submit']))echo $_POST['phone']; ?>"/></p>
<p>Salary <input type="number" name="salary" value="<?php    if(isset($_POST['submit']))echo $_POST['salary']; ?>"/></p>    
<p><input type="submit" name="submit" value="Insert"/></p>

</form>

dep_emp.php

<?php

//connect to the database
$dbc = @mysqli_connect('localhost','root','black98765','activity_7a')
       OR die("Could not connect to MySQL: ".mysqli_connect_error());

//insert values into department table
$q2 = "INSERT INTO dept (dept_no,dept_name,location)
VALUES ($dep_no,'$dep_name','$dep_location')";
//insert values to employee table
$q = "INSERT INTO emp      (emp_no,dept_no,first_name,last_name,address,phone,salary)
VALUES ($emp_no,$dep_no,
'$first_name','$last_name','$address','$phone',$salary)";       
$r = mysqli_query($dbc,$q);
$r2 = mysqli_query($dbc,$q2);
if($r && $r2){
echo "<p>Successfully Inserted!</p>";
}else{
echo "<p>System Error!</p>
<p>Data are not been inserted!</p>";
echo "<p>".mysqli_error($dbc)."</p>";
}
mysqli_close($dbc); 
?>

why use the select statement here?

'SELECT dept_no FROM dept where dept_no = $dep_no'

Aren't you just selecting the exact same value you just inserted into the department table? Why not just

VALUES ($emp_no,$dep_no,'$first_name'...

I'm new to php, so I could easily be wrong here...

Try this...

$q2 = "INSERT INTO dept (dept_no,dept_name,location)
VALUES ('$dep_no','$dep_name','$dep_location')";

$q= "INSERT INTO emp SET emp_no='$emp_no',dept_no='$dep_no',first_name='$first_name',last_name='$last_name',address='$address',phone='$phone',salary='$salary'"