-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.php
43 lines (35 loc) · 1.44 KB
/
connect.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
<?php
// Replace with your own database credentials
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "PortFolio";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Check if form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST")
// Validate user input
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$message = test_input($_POST["message"]);
// Insert user input into database
$sql = "INSERT INTO contact_form (name, email, message) VALUES ('$name', '$email', '$message')";
if (mysqli_query($conn, $sql)) {
echo "Thank you for contacting us! We will get back to you shortly.";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
// Function to sanitize user input
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// Close connection
mysqli_close($conn);
?>