0

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"> 
5
  • provide result of print_r of POST data after submit, pls Commented Jul 25, 2018 at 5:20
  • 2
    When checkbox is not checked , the array is not created. So if you do not select kirana first and you select it on the second category . Kirana will have key 0 as if you picked him first. Commented Jul 25, 2018 at 5:22
  • @AlexandrePainchaud Thank you...so how do I force the creation of array...should I use a if condition to check for null and push 0? Commented Jul 25, 2018 at 6:28
  • 1
    You just have to force array index of each lines like : kirana[0], chemist[0] next category kirana[1],chemist[1] ... Commented Jul 25, 2018 at 7:50
  • @AlexandrePainchaud Thank you...instead of forcing to recursively building an array...I converted the checkboxes to select option with 0 and 1....which resolved my issue temporarily. Thank you for your answer about checkbox is not checked, the array is not created...I can accept it if you post it as an ansewr. Commented Jul 25, 2018 at 8:54

1 Answer 1

2

I resume my comments here.

First of all if a checkbox is not checked the array is not created.

So if I check only kirana on the first line and i check mall in the second. Your code will provide this values:

$_POST['kirana'][0] = 1
$_POST['mall'][0] = 1

that's why you got trouble when you insert data. An easy way to fix this and fit to your code is to force indexes on each line.

<!-- First Line -->
<input type="checkbox" name="kirana[0]" id="kirana_0" value="1" />
<input type="checkbox" name="chemist[0]" id="chemist_0" value="2" />
<!--Second line-->
<input type="checkbox" name="kirana[1]" id="kirana_1" value="1" />
<input type="checkbox" name="chemist[1]" id="chemist_1" value="2" />

Now if we examine another time the first example with this HTML

    //Now indexes are correct
    $_POST['kirana'][0] = 1
    $_POST['mall'][1] = 1

Note id attribute on html must be unique,else you must use class attribute.

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

@AlexandrePainchuad Thank you...I agree with the first point....however, since the checkboxes were getting created dynamically, providing them with unique id's may not be most appropriate...besides, while posting since I have to hardcode all the id's and values....it may not be an optimal solution. I have agreed to your answer based on the indication that the array gets created only when the checkbox is checked. Thank you.
@Apricot glad to help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.