How can the PHP version being used affect the implementation of deleting users from a table using links?
The PHP version being used can affect the implementation of deleting users from a table using links due to changes in syntax or deprecated functions. To ensure compatibility across different PHP versions, it is recommended to use prepared statements with parameterized queries to prevent SQL injection attacks and ensure the proper deletion of users from the table.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if user ID is set and valid
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
$id = $_GET['id'];
// Prepare and execute the SQL statement to delete user
$stmt = $conn->prepare("DELETE FROM users WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
// Redirect back to the user list page
header("Location: user_list.php");
exit();
} else {
echo "Invalid user ID";
}
$conn->close();
?>
Keywords
Related Questions
- How can PHP beginners improve their understanding of handling multiple checkbox selections in email transmissions?
- Are there specific PHP settings that should be avoided when trying to override configurations with a local php.ini file?
- What are best practices for downloading files in PHP to avoid memory issues?