Updating Mysql with checked boxes from Registration

A

Anonymous

Guest
I have a registration form i am trying to work on as part of a project and in my code i want to know which checkboxes have been selected and which havent. SO far the below code will enter in to a varchar DB the entry "on" if the checkbox is checked but nothing else, so i am not sure how to make sense of that, i.e. would it not be off, off, on, on if you only were to select the last 2 checkboxes for example. here is some of my code only the relevant bit for checking each box, adding to SQL and the html checkbox itself..

Code:
<?PHP
$Industrybtn = '';
                   if(!empty($_POST["Industry"]))
                   {
                       foreach($_POST["Industry"] as $Industry)
                       {
                           $Industrybtn .= $Industry . ', ';
                       }
                   }
      
                 $stmt = mysqli_prepare($con, "INSERT INTO inf VALUES (?, ?, ?, ?, ?, ?)");
                 mysqli_stmt_bind_param($stmt, "ssssss", $int, $inf_fname, $inf_lname, $inf_email, $inf_password, $Industrybtn);
                 mysqli_stmt_execute($stmt);
                  ?>

<div class="checkboxes">
                    <input type="checkbox" name="Industry[]" id="checkbox_id" style="margin-bottom:15px"><label for="checkbox_id">Dinning</label>
                    <input type="checkbox" name="Industry[]" id="checkbox_id1" style="margin-bottom:15px"><label for="checkbox_id1">Jewellery</label>
                    <input type="checkbox" name="Industry[]" id="checkbox_id2" style="margin-bottom:15px"><label for="checkbox_id2">Clothing</label>                 
                    </div><br>
 
Thanks, so how do I know which box was selected? There doesn’t seem to be any logic to the order so how will we know what they selected?
 
so how do I know which box was selected?
If you only want one selection, use a radio control.
There doesn’t seem to be any logic to the order so how will we know what they selected?
You're the one writing the logic.


Have you read the link?
 
This behavior occurs because unchecked checkboxes are not submitted as part of the form data. Only checked checkboxes are included in the $_POST["Industry"] array.

To fix this problem, change the code to explicitly check whether the checkbox is checked before adding the value to the $Industrybtn variable. Here is the updated version of the code:


''''php
$Industrybtn = '';
if (!empty($_POST["Industry"])) {
$checkboxes = array("Food", "Jewelry", "Clothing");

for each ($checkbox as $checkboxes) {
if (in_array($checkbox, $_POST["industry"])) {
$Industrybtn .= $checkbox . ',';
}
}
}

$stmt = mysqli_prepare($con, "INTO VALUES (?, ?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, "ssssss", $int, $inf_fname, $inf_lname, $inf_email, $inf_password, $Industrybtn);
mysqli_stmt_execute($stmt);
""

This
code defines an array of $checkboxes containing all possible checkbox values. Then, in for each loop, check if each checkbox value exists in the $_POST["Industry"] array. In that case, the checkbox value is added to the $Industrybtn variable.

The $Industrybtn variable then contains a comma-separated list of values for the selected checkboxes. If the checkbox is unchecked, the variable remains empty.

Note:
Note that storing multiple values as a comma-separated list in a single database column may not be the best approach for relational database design. Consider using a separate table to store the selected checkboxes with a relationship to the main data table. This allows for more efficient queries and better data organization.
 
Back
Top