Search code examples
phpmysqlimplode

Implode function doesn't store data as expected


I have already posted this but finally I didn't figure this out.I have a php form with some checkboxes (some of them are enabled and some disabled). Each of these has an id (id1=1, id2=2, id3=3, ...) which comes from database.

So when I submit the form I want to store these id's in a database table like this: if I choose only the 1st checkbox which has id=1 I should store '1' on my table and if I choose 1st and 3rd I should store '1, 3'. The problem is that I choose only the 1st and the stored data is '1, 2, 3, 4' because I have 4 checkboxes ENABLED.

PHP form :

<form method='post' action='insert.php'>
  .
  .
  .

 while($row_select4 = $stmt_select4->fetch(PDO::FETCH_ASSOC)){

    if($form_points>=$row_select4['points']) {

         $points = $row_select4['points'];  


       echo '

          <div>
             <div class="feed-activity-list"><div style="border: 0.5px solid green; border-right-style:none;" class="input-group m-b"><span class="input-group-addon"> 
             <input type="checkbox" onclick="enable_form();" id="checkbox" name="opt[]" value="'.$points.'"></span>
             <input type="hidden" name="opt2[]" value="'.$row_select4['id'].'">


             <div class="feed-element">
             <a href="profile.html" class="pull-left">
             <img alt="image" class="img-circle" src="'. $row_select4['image_url']. '"
             </a>';
   ?>


   <button type="submit" id="submit" name="eksasrgirwsh" class="btn btn-w-m btn-primary">ΕΞΑΡΓΥΡΩΣΗ</button> 
             </form> 

IMPLODE before insertion :

if(isset($_POST['opt2'])){



        foreach ($_POST['opt2'] as $value) {
            $gift = $_POST['opt2'];   

            $sliced = array_slice($gift, 0, -1);
            $gift_id = implode(", ", $sliced);
            }

And I store $gift_id in the table..

Check :

if(isset($_POST['opt'])){

        $total_points = 0;   
        $points =  array_values($_POST['opt']);



            foreach ($points as $value) {
                $total_points += $value;
                               }
             echo $total_points;




              $gift_ids = '';
              $gift = array_keys($_POST['opt']);

                foreach ($gift as $key => $value) {
                $gift_ids = $value;
                $gift_ids2 = implode(", ", $gift_ids);
                               }
                    echo $gift_ids2;

    } 

Solution

  • Instead of using separate hidden controls to store the ids, use the name property of the checkboxes to do this:

    '...
    <input type="checkbox" onclick="enable_form();" id="checkbox" name="opt['. $row_select4['id'].']" value="'.$points.'"></span>
    ...';
    

    The name of a checkbox will be like name="opt[3]" for id 3 record.

    In the php script processing the form submission array_keys($_POST['opt']) will give you the gift ids, while array_values($_POST['opt']) will return the points. You will know which point is associated with which gift id because the keys of the $_POST['opt'] are the gift ids.

    Pls also note that storing multiple values in a comma separated string in a single field is not really considered a good practice. You should normalise your data structure and store the separate values in separate records.