<!DOCTYPE html>
<html>
<head>
<title>Database connectivity in Form</title>
</head>
<body>
<?php
// Connect to database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "customers";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Handle form submissions
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$sql = "INSERT INTO customers (name, email, phone) VALUES ('$name', '$email', '$phone')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
// Close connection
mysqli_close($conn);
?>
<!-- Display form -->
<form method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br>
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
For Database
CREATE DATABASE customers
- localhost/phpmyadmin
- GO to SQL and type
CREATE DATABASE customers- SELECT created DATABASE and type
CREATE TABLE customers
(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL UNIQUE,
phone VARCHAR(20)
);