3

I just begin learn php and js, and have difficulty how to write code like below. I have 5 checkboxes, when i check all, sql insert add 5 values like below. I mean, how to write it dynamically in sql insert section, when i choose only one, or 2, or some, and even all. That way, I do not have to write

    ('$idkeg',01, '$kabkot'),
    ('$idkeg',02, '$kabkot'),
    ('$idkeg',03, '$kabkot'),
    ('$idkeg',04, '$kabkot'),
    ('$idkeg',05, '$kabkot')

one by one

Full codes:

<label><input type='checkbox' name="all" id="all" value='all'>Seluruh kab/kota</label>
<label><input type='checkbox' value='01'>Sibolga</label>
<label><input type='checkbox' value='02'>Tanjung Balai</label>
<label><input type='checkbox' value='03'>Pematang Siantar</label>
<label><input type='checkbox' value='04'>Tebing Tinggi</label>
<label><input type='checkbox' value='05'>Medan</label>

mysqli_query($koneksi, "INSERT INTO event(idkeg,kdwil,kabkot) VALUES 
('$idkeg',01, '$kabkot'),
('$idkeg',02, '$kabkot'),
('$idkeg',03, '$kabkot'),
('$idkeg',04, '$kabkot'),
('$idkeg',05, '$kabkot')
;") or die(mysqli_error());
5
  • So you want to insert a different number of value-pairs everytime? E.g. checkboxes 1, 2 and 5 in one run, then only 2 and 5 ansd so on? Commented May 16, 2017 at 9:08
  • you have to use loop for it. Commented May 16, 2017 at 9:08
  • @TobiasF. no, say i just check some checkbox, and insert it to sql Commented May 16, 2017 at 9:12
  • So you want to have a live-update everytime you check or uncheck a single checkbox or do you want only the values to be added which are checked when you hit the submit button? Commented May 16, 2017 at 9:13
  • Yap @TobiasF. wanna only the values to be added which are checked when hit the submit. any idea? Commented May 16, 2017 at 9:16

1 Answer 1

5

Give same array name to each check box, then after form submit it will give you values of selected check boxes in array

eg.

 <input type='checkbox' name="arrayName[]" value='05'>Medan</label>

output of $_POST['arrayName'] will be ['02','03','05']

then perform INSERT Operation in Loop

$checkedArray = $_POST['arrayName'];

foreach($checkedArray as $checked){
  $query = "INSERT INTO event(idkeg,kdwil,kabkot) VALUES('$idkeg','$checked','$kabkot')";
  mysqli_query($koneksi, $query) or die(mysqli_error());
}
Sign up to request clarification or add additional context in comments.

4 Comments

I would recommend to run the query outside of the loop and concatenate the results afterwars, so you only have to execute one query, and not multiple.
@Zaheer Altar you should fix the typo within your answer, replace the first ' after $query with ".
thanks @TobiasF. for typo. Your approach is also good for optimization purpose.
But what if my value is "'; DROP DATABASE..."? At least use escaping.

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.