Search code examples
phpmysqlarrayscheckboxpdo

Store checkbox selections into MySQL database table using PDO


I have a list of optional clothing items as checkboxes, there may be a greater number than the 5 below.

shoes, pants, skirt, socks, jacket //list of possible choices

A comma-separated array is created in jquery of the chosen item. Let's say the following are chosen:

shoes, socks, jacket //array posted as $_POST['clothes']

In the db, each customer has these options in the clothes table with 'yes' or 'no' under the clothing items. However, the clothing item are named a bit differently but map out to the same options:

'clothes' table before insert

customer_id  dress_shoes  elegant_pants  long_skirt  ankle_socks  biker_jacket
     1            no            yes           no           no           no

With the $_POST['clothes'], I'm trying to loop through the array, updating the corresponding fields to yes, and the non corresponding fields to no in the db. I'm having a hard time doing that.

'clothes' table after insert

customer_id  dress_shoes  elegant_pants  long_skirt  ankle_socks  biker_jacket
     1            yes            no           no          yes          yes

I tried using array_intersect() to get the items to mark as 'yes':

$clothesArray = array("shoes", "socks", "jacket"); // Posted clothes
$clothesArrayAll = array("shoes", "pants", "skirt", "socks", "jacket"); // All clothes
$common = array_intersect($clothesArrayAll,$clothesArray);
print_r($common);
Array ( [0] => shoes [3] => socks [4] => jacket )

I'm trying to somehow loop through the $clothesArrayAll, give a 'yes' to common clothes, and a 'no' to all others in the array. Then, I'm trying to update the 'clothes' table via PDO, setting each corresponding field to a 'yes' or 'no' in the most efficient way. I'm stuck after getting the common clothes array above and not sure how to proceed.


Solution

  • I think you are on the right track. I would just add one additional array that contains the mappings of your fields, e.g.

    $mappingArray = array('shoes' => 'dress_shoes', 'socks' => 'ankle_socks', ...);
    

    With this array and the previous you can loop through and set your SQL accordingly based on the value of the $common field with the key in the $mappingArray

    Edit with example (probably not the most optimized):

    $finalArray = array();
    
    foreach ($mappingArray as $key => $value) {
       $finalArray[$value] = in_array($key, $common) ? 'yes' : 'no';
    }
    

    $finalArray will now have an yes/no statement for each value that matches your db table.

    Edit to include PDO: I would actually update the above loop as follows:

    $finalArray = array();
    $sql = "INSERT INTO cloths (" . implode(",", array_values($mappingArray)) . ") VALUES (:" . implode(",:", array_values($mappingArray)) . ")";;
    foreach ($mappingArray as $key => $value) {
        $finalArray[":" . $value] = in_array($key, $common) ? 'yes' : 'no';
    }
    
    $q = $conn->prepare($sql);
    $q->execute($finalArray);
    

    Going on the fly with this one, so something like that...