-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit_sp.php
60 lines (49 loc) · 1.58 KB
/
submit_sp.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
<?php
// Assuming you have already established a connection to your MySQL database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "learningkids";
// Create a new MySQLi object
$conn = new mysqli($servername, $username, $password, $dbname);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
// Validate input fields
if (empty($name) || empty($email) || empty($message)) {
echo "Please fill in all the fields.";
exit;
}
// Validate email format
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format.";
exit;
}
// Prepare and execute the SQL statement
$stmt = $conn->prepare("INSERT INTO sp_form (id, name, email, message, created_at) VALUES (NULL, ?, ?, ?, NOW())");
$stmt->bind_param("sss", $name, $email, $message);
$stmt->execute();
// Check if the insertion was successful
if ($stmt->affected_rows > 0) {
// Show a message after the form is submitted
echo "<script>alert('Form submitted successfully!');</script>";
// Redirect back to support.php after a delay
header("refresh:0.5;url=support.php");
exit;
} else {
echo "Error inserting data: " . $stmt->error;
}
// Close the statement
$stmt->close();
}
// Close the connection
$conn->close();
?>
>