[Back] <?php
// Database credentials
$servername = "localhost";
$username = "FIKHRI";
$password = "abc123";
$dbname = "Test";
// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get the user ID from the URL
if (isset($_GET['id'])) {
$user_id = $_GET['id'];
// Fetch the user details for editing
$stmt = $conn->prepare("SELECT id, name FROM users WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
if (!$user) {
echo "User not found.";
exit();
}
$stmt->close();
}
// Handle the form submission to update the user's name
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update_name'])) {
$new_name = $_POST['name'];
// Update the user name in the database
$stmt = $conn->prepare("UPDATE users SET name = ? WHERE id = ?");
$stmt->bind_param("si", $new_name, $user_id);
if ($stmt->execute()) {
echo "Name successfully updated!";
header("Location: index.php"); // Redirect back to the main page
exit();
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
}
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Name</title>
</head>
<body>
<h1>Edit Name</h1>
<form method="post" action="">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php echo htmlspecialchars($user['name']); ?>" required>
<button type="submit" name="update_name">Update</button>
</form>
<a href="index.php">
<button>Back to List</button>
</a>
</body>
</html>