[go: up one dir, main page]

0% found this document useful (0 votes)
15 views2 pages

We 7

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

We 7

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

G.

Mathan Kumar
311522205024

PROGRAM:
SQL:

CREATE DATABASE we;


USE we;
CREATE TABLE users ( id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL, address VARCHAR(255) NOT NULL, phone
VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL );

PHP:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add and Display Users</title>
</head>
<body>
<form action="" method="post">
<label>Name:</label>
<input type="text" name="name" required>
<label>Address:</label>
<input type="text" name="address" required>
<label>Phone:</label>
<input type="text" name="phone" required>
<label>Email:</label>
<input type="email" name="email" required>
<button type="submit">Submit</button>
</form>
<?php
// connect to the database
$servername = "localhost";
$username = "root";
$password = "mathan@2004";
$dbname = "we";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// sanitize input data
$name = $conn->real_escape_string($_POST['name']);
$address = $conn->real_escape_string($_POST['address']);
$phone = $conn->real_escape_string($_POST['phone']);
$email = $conn->real_escape_string($_POST['email']);
G.Mathan Kumar
311522205024

$stmt = $conn->prepare("INSERT INTO users (name, address, phone, email) VALUES


(?, ?, ?, ?)");
$stmt->bind_param("ssss", $name, $address, $phone, $email);
if ($stmt->execute()) {
echo "User added successfully";
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<h2>User Information</h2>";
echo "Name: " . htmlspecialchars($row["name"]) . "<br>";
echo "Address: " . htmlspecialchars($row["address"]) . "<br>";
echo "Phone: " . htmlspecialchars($row["phone"]) . "<br>";
echo "Email: " . htmlspecialchars($row["email"]) . "<br><br>";
}
} else {
echo "No users found.";
}

// close the connection


$conn->close();
?>
</body>
</html>

OUTPUT:

You might also like