How can PHP be used to handle checkbox values from a form submission and insert them into a database table for user access rights management?
To handle checkbox values from a form submission and insert them into a database table for user access rights management, you can use PHP to process the form data, retrieve the checkbox values, and then insert them into the database table along with the user's ID. You can achieve this by first checking if the checkbox values are set in the form submission, then preparing an SQL query to insert the values into the database table.
// Process form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve checkbox values from form submission
$checkbox_values = isset($_POST['checkbox_name']) ? $_POST['checkbox_name'] : [];
// Insert checkbox values into database table for user access rights management
$user_id = 1; // Assuming user ID is 1
$conn = new mysqli("localhost", "username", "password", "dbname");
foreach ($checkbox_values as $value) {
$sql = "INSERT INTO user_access_rights (user_id, access_right) VALUES ('$user_id', '$value')";
$conn->query($sql);
}
$conn->close();
}