0

HTML CODE :

<input type='checkbox' name='cbox[]' value='Jaywalking'  /> 
Jaywalking<br>
<input type='checkbox' name='cbox[]' value='Littering'/> 
Littering<br>
<input type='checkbox' name='cbox[]' value='Illegal Vendor' /> 
Illegal Vendor

POST CODE:

if(is_array($_POST['cbox'])) $violation_save=implode(',',$_POST['cbox']); else $violation_save=$_POST['cbox'];

mysql_query("UPDATE tblcitizen SET violation='$violation_save' WHERE id='$id'") or die mysql_error());

What is the easiest way to retrieve checked checkbox value into the database? What is the proper way to retrieve it base on my code?

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

Try this code,

<form method='post'>
<input type='checkbox' name='cbox[]' value='Jaywalking'  /> 
Jaywalking<br>
<input type='checkbox' name='cbox[]' value='Littering'/> 
Littering<br>
<input type='checkbox' name='cbox[]' value='Illegal Vendor' /> 
Illegal Vendor
    <input type='submit' name='submit' />
</form>
<?php
if(isset($_POST['submit'])){
    $violation_save = '';
    if(is_array($_POST['cbox']))
        $violation_save=implode(',',$_POST['cbox']);
    else
        $violation_save=$_POST['cbox'];
    //debug purpose
    var_dump($violation_save);
    $sql = "UPDATE `tblcitizen` SET `violation`='$violation_save' WHERE `id`='$id'";
    var_dump($sql);
    mysql_query($sql) or die(mysql_error());
}
?>

This just demonstrates on the values being stored in $_POST['cbox'] which is shown as output using var_dump

rsakhale
  • 1,018
  • 13
  • 26
  • sir is this code will retrieve the checkbox coming from the database and it will be checked if it has a record? – user3397748 Mar 09 '14 at 07:52
  • Values in variable `$_POST` comes from HTTP Request only which is of `METHOD - POST`, here we are not referring to database and so we cannot consider that. `name='cbox[]'` means we are stating that variable to be acted as array which will contain all the checked values, and you can insert these values in database by doing iteration over this array example: `foreach($_POST['cbox'] as $cbox_val)` – rsakhale Mar 09 '14 at 07:57
  • if (!$result) { die("Error: Data not found.."); } $id=$test['id'] ; $license= $test['license'] ; $stickerno=$test['stickerno'] ; $fname=$test['fname'] ; $mname= $test['mname'] ; $lname=$test['lname'] ; $no=$test['no'] ; $street= $test['street'] ; $city=$test['city'] ; $bdate=$test['bdate'] ; $violationplace=$test['violationplace'] ; $dd=$test['dd'] ; $mm=$test['mm'] ; $yy=$test['yy'] ; $hh=$test['hh'] ; $min=$test['min'] ; $ampm=$test['ampm'] ; $type=$test['type'] ; $officer=$test['officer'] ; – user3397748 Mar 09 '14 at 08:00
  • Can't understand what you want to say from this code – rsakhale Mar 09 '14 at 08:21
  • that is my retrieving method sir. and i'll use – user3397748 Mar 09 '14 at 08:25
  • Your above post code in question seems to be good, what is the error you are seeing? – rsakhale Mar 09 '14 at 08:39
  • @user3397748 I have edited the code to identify the values for debug purpose can u please refer in my answer – rsakhale Mar 09 '14 at 08:44
  • sir i want to fetch checked values from the database – user3397748 Mar 09 '14 at 08:54