mardi 24 juillet 2018

PHP - posting checkbox values foreach record

My html form has the following fields:

subscriberid - number input product category - select option typeofoutlet - 4 checkboxes.

Of this subscriberid is a static field. The html elements for product category and typeofoutlet are dynamically generated through a add record button.

enter image description here

I am trying to post the form data to mysql using PHP. The following is the code:

if(isset($_POST['submit'])){
    //connect to db
    $mysqli = NEW MySQLi('localhost', 'root','Abc@123def', 'tsl');
    $subscriberid = $_POST['subscriberid'];
    $category = $_POST['category'];
    $brand = $_POST['brand'];
    $kirana = $_POST['kirana'];
    $chemist = $_POST['chemist'];
    $mall = $_POST['mall'];
    $online = $_POST['online'];

    foreach($category as $key => $value) { 
            //perform insert
            $query = "insert into hhpurchase (subscriberid, category,brand,kirana,chemist,mall,online) 
                    VALUES (
                        '". $mysqli->real_escape_string($subscriberid) ."',
                        '". $mysqli->real_escape_string($category[$key]) ."',
                        '". $mysqli->real_escape_string($brand[$key]) ."',
                        '". $mysqli->real_escape_string($kirana[$key]) ."',
                        '". $mysqli->real_escape_string($chemist[$key]) ."',
                        '". $mysqli->real_escape_string($mall[$key]) ."',
                        '". $mysqli->real_escape_string($online[$key]) ."'        

            )";
            $insert = $mysqli->query($query);
            if(!$insert) {
                echo $mysqli-> error;
                echo "<script type='text/javascript'>alert('Submission failed!')
                window.location.href='test.php';
                </script>";
            } else {
                echo "<script type='text/javascript'>alert('Submitted successfully!')
                window.location.href='test.php';
                </script>";
            }
        }
        $mysqli->close(); 
    }

While all the records gets recorded correctly, the data from 4 check boxes gets stored in the one line irrespective of number of lines of data that I have in the form. The example output is as follows:

enter image description here

Now in the above image, the 1 under kirana is right, however, the 2 under chemist should be in row 2 but always gets posted in row 1. However, if I have two rows and for both of which if I have selected the same option, they are getting posted correctly.

The HTML for checkboxes is as follows:

<label>Kirana</label>
<input type="checkbox" name="kirana[]" id="kirana" value="1">
<label>Chemist</label>
<input type="checkbox" name="chemist[]" id="chemist" value="2">
<label>Mall</label>
<input type="checkbox" name="mall[]" id="mall" value="3">
<label>Online</label>
<input type="checkbox" name="online[]" id="online" value="4"> 




Aucun commentaire:

Enregistrer un commentaire