How can PHP be utilized to manage and store fixed product attributes efficiently in a database when checkboxes are selected?
To manage and store fixed product attributes efficiently in a database when checkboxes are selected, you can use PHP to iterate through the selected checkboxes and store their values in the database. This can be achieved by creating an array of the selected checkbox values and then inserting them into the database using prepared statements to prevent SQL injection.
// Assuming you have a form with checkboxes named 'attribute[]'
if(isset($_POST['submit'])){
$selectedAttributes = $_POST['attribute'];
// Connect to your database
$conn = new mysqli("localhost", "username", "password", "database");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare a SQL statement to insert the selected attributes
$stmt = $conn->prepare("INSERT INTO product_attributes (attribute_name) VALUES (?)");
// Bind parameters and execute the statement for each selected attribute
foreach($selectedAttributes as $attribute){
$stmt->bind_param("s", $attribute);
$stmt->execute();
}
// Close the statement and connection
$stmt->close();
$conn->close();
}