-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.php
91 lines (77 loc) · 2.19 KB
/
user.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Ecommerce";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
function registerUser($conn, $name, $email, $password) {
$passwordHash = password_hash($password, PASSWORD_BCRYPT);
$stmt = $conn->prepare("INSERT INTO customers (name, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $passwordHash);
if ($stmt->execute()) {
echo "User registered successfully.";
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
}
function listUsers($conn) {
$sql = "SELECT id, name, email, created_at FROM customers";
$result = $conn->query($sql);
if ($result === false) {
echo "Error: " . $conn->error;
return;
}
if ($result->num_rows > 0) {
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Created At</th>
</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['email']}</td>
<td>{$row['created_at']}</td>
</tr>";
}
echo "</table>";
} else {
echo "No users found.";
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
registerUser($conn, $name, $email, $password);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>User Registration</title>
</head>
<body>
<h2>Register User</h2>
<form method="POST" action="">
Name: <input type="text" name="name" required><br>
Email: <input type="email" name="email" required><br>
Password: <input type="password" name="password" required><br>
<input type="submit" value="Register">
</form>
<h2>List of Users</h2>
<?php listUsers($conn); ?>
</body>
</html>
<?php
$conn->close();
?>