-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.php
53 lines (49 loc) · 1.62 KB
/
insert.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
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "MBATECH";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$firstname = $email = $address1 = $city = $state_name = $zip = $reg_date = "";
$firstname = $_POST["firstname"];
$email = $_POST["email"];
$address1 = $_POST["address1"];
$city = $_POST["city"];
$state_name = $_POST["state_name"];
$zip = $_POST["zip"];
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$firstNameError = $emailErr = "";
if (empty($firstname)) $firstNameError = "First Name field is empty";
else {
$name = test_input($firstname);
if (!preg_match("/^[a-zA-Z]*$/", $name))
$firstNameError = "Only letters and white spaces allowed";
}
if (empty($email)) $emailErr = "Email field empty.";
else {
$email = test_input($email);
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
$emailErr = "Enter valid mail id!";
}
}
if ($firstNameError === "" && $emailErr === "") {
$sql = "INSERT INTO appointments (firstname,email,address1,city,state_name,zip)
VALUES ('$firstname', '$email', '$address1', '$city', '$state_name', '$zip')";
} else echo $firstNameError . "<br>" . $emailErr . "<br>Record not stored";
if ($conn->query($sql) === TRUE) {
echo "Booked appointment successfully.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}