set_charset("utf8"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Get user details $user = []; if (isset($_GET['id'])) { $user_id = $conn->real_escape_string($_GET['id']); $sql = "SELECT * FROM `".$GLOBALS['arif_users']."` WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("i", $user_id); $stmt->execute(); $result = $stmt->get_result(); $user = $result->fetch_assoc(); $stmt->close(); if (!$user) { die("User not found"); } } // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { $id = $_POST['id']; $user_name = $_POST['user_name']; $user_phone = $_POST['user_phone']; $type = $_POST['type']; $user_id = $_POST['user_id']; // Validate inputs if (empty($user_name) || empty($user_phone) || empty($user_id)) { $error = "All fields are required except password"; } elseif (!preg_match("/^[0-9]{10}$/", $user_phone)) { $error = "Invalid phone number format"; } else { // Update query if (!empty($_POST['password'])) { // Update with password $password = password_hash($_POST['password'], PASSWORD_DEFAULT); $sql = "UPDATE `".$GLOBALS['arif_users']."` SET user_id = ?, user_name = ?, user_phone = ?, type = ?, password = ? WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("sssssi", $user_id, $user_name, $user_phone, $type, $password, $id); } else { // Update without password $sql = "UPDATE `".$GLOBALS['arif_users']."` SET user_id = ?, user_name = ?, user_phone = ?, type = ? WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("ssssi", $user_id, $user_name, $user_phone, $type, $id); } if ($stmt->execute()) { $success = "User updated successfully!"; // Refresh user data $sql = "SELECT * FROM `".$GLOBALS['arif_users']."` WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("i", $id); $stmt->execute(); $result = $stmt->get_result(); $user = $result->fetch_assoc(); $stmt->close(); } else { $error = "Error updating user: " . $conn->error; } } } ?>