-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddemployee.php
63 lines (48 loc) · 1.85 KB
/
addemployee.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Add Employee</title>
</head>
<body>
<h2>Add an Employee Record</h2>
<br><br>
<?php
echo "<h3>PHP Code Generates This:</h3>";
//some variables
$servername = "localhost"; //mysql is on the same host as apache (not realistic but whatevs)
$username = "foxsarh"; //username for database
$password = "123"; //password for the user
$dbname = "employees"; //which db you're going to use
//this is the php object oriented style of creating a mysql connection
$conn = new mysqli($servername, $username, $password, $dbname);
//check for connection success
if ($conn->connect_error) {
die("MySQL Connection Failed: " . $conn->connect_error);
}
echo "MySQL Connection Succeeded<br><br>";
//pull the attribute that was passed with the html form GET request and put into a local variable.
$lastname = $_GET["lastname"];
$firstname = $_GET["firstname"];
$birthdate = $_GET["birth_date"];
$hiredate = $_GET["hire_date"];
$gender = $_GET["gender"];
$emp_no = $_GET["emp_no"];
echo "Adding record for: " . $firstname . " " . $lastname;
echo "<br><br>";
//create the SQL insert statement, notice the funky string concat at the end to variablize the query
//based on using the GET attribute
//this statement needs to be variablized to put in the data passed from the form
//right now it is hardcoded.
$sql = "INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES
('".$emp_no."', '".$birthdate."', '".$firstname."', '".$lastname."', '".$gender."', '".$hiredate."')";
if ($conn->query($sql) === TRUE){
echo "New Employee Created Successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
//always close the DB connections, don't leave 'em hanging
$conn->close();
?>
</body>
</html>