I can't Insert in mysql database with php i want to add table

Viewed 26

No matter what i did i can't insert a new table to my daTabase in mysql

what i did is:- 1-i build an database
2-i build an html form i want the data from the form to insert directly into the database
3-i made an table called "department" to hold an id of the department and make refrence to it in the employee table.

4- i connected successfully to the database

--it's A school Task

THE HTML CODE:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Registration Form</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <h1>Employee Form</h1>
    <form method="POST">
      <fieldset>
        <label for="first-name">Enter Your Name: <input id="first-name" name="name" type="text" required /></label>
        <label for="last-name">Enter Your Phone: <input id="last-name" name="phone" type="text" required /></label>
        <label for="email">Enter Your Salary: <input id="email" name="salary"  required /></label>
      </fieldset>
      <fieldset>
        <label for="referrer"> Enter The City
          <select id="referrer" name="city">
            <option value="">(select one)</option>
            <option value="Cairo">Cairo</option>
            <option value="Alexandria">Alexandria</option>
            <option value="Aswan">Aswan</option>
          </select>
        </label>
      </fieldset>

      <fieldset>
        <label for="referrer">Enter The Department
          <select id="referrer" name="department">
            <option value="">(select one)</option>
            <option value="IT">IT</option>
            <option value="MANAGEMENT">MANAGEMENT</option>
            <option value="Security">Security</option>
          </select>
        </label>
      </fieldset>
      <input type="submit" name="submit" value="Submit" />
    </form>
  </body>
</html>

php code:

<?php
function testMessage($connication, $message)
{
    if ($connication) {
        echo "True $message";
    } else {
        echo "false $message";
    }
}
$servername = "localhost";
$username = "root";
$password = "";
$db = "employees";

$conn = mysqli_connect($servername, $username, $password , $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else{
    echo "Connected successfully";
}


if(isset($_POST['submit']))
{    
     $first_name = $_POST['name'];
     $salary = $_POST['salary'];
     $phone = $_POST['phone'];
     $city = $_POST['city'];
     $department= $_POST['department'];
     $sql = "INSERT INTO 'department'VALUES (null,'$department')";
     $sql = "INSERT INTO 'emp'VALUES (null,'$first_name','$salary','$phone',null,'$city')";
     if (mysqli_query($conn, $sql)) {
        echo "New record created successfully !";
     } else {
        echo "Error: " . $sql . "
" . mysqli_error($conn);
     }
     mysqli_close($conn);
}
?>

my database: enter image description here

0 Answers
Related