php - Checkboxes are not updating in the database -


i've got question databases , checkboxes. i've got table looking like:

table structure

website looking like:

website screenshot

at bottom of page have button, when submit checked checkboxes updated 1 or 0 in database. (true or false)

so when click on 3rd checkbox under trained, update trained column in database user/room id of '3583'. (id shown right of screen)

code:

<form class='verwerkinfo' method='post' action='<?php echo $_server['php_self']; ?>?license=6'>                 <td>                     <?php if($room->trained == 1) { ?> <input type='checkbox' name="<?php echo $room->room_id; ?>" checked> <?php echo "y"; } else{ ?> <input type='checkbox' name="<?php echo $room->room_id; ?>"> <?php echo "n"; }?> </td>                 <td><?php if($room->active == 1) { ?> <input type='checkbox' name="<?php echo $room->room_id; ?>" checked> <?php echo "active"; } else { ?> <input type='checkbox' name="<?php echo $room->room_id; ?>"  <?php echo "inactive"; } ?>                 </td>                  <td><?php echo $room->configuration; ?></td>                 <td><?php echo $room->room_id; ?></td>                 <td><?php var_dump($room->user_id); }?></td>                 </tr> 

so guess have problem in names of checkboxes.

the query looking like:

$trainedquery = "update room_users                             set trained = 1                             user_id = $room->user_id"; 

the $room->user_id referring user_id in database.

here's way give checkboxes unique names , pass information each element:

name="trained[<?php echo $room->room_id; ?>]" value="<?php echo $room->user_id; ?>" 

then in php script processes form submission can:

foreach ( $_post['trained'] $room_id => $user_id ) {     // query needs protection sql injection!     $trainedquery = "update room_users set trained = 1 user_id = $user_id"; } 

it's not clear relationship between room_id , user_id , why you're updating room_user table user_id. do room_id?

is need:

// query needs protection sql injection! $trainedclear = "update room_users set trained = 0 user_id = $user_id"; $db->exec($trainedclear); // first clear  foreach ( $_post['trained'] $room_id => $user_id ) {     // query needs protection sql injection!     $trainedquery = "update room_users set trained = 1          user_id = $user_id , room_id = $room_id";     $db->exec($trainedquery); // add selections } // assuming there's database connection `$db-exec`. // replace actual connection , query method. 

refactored checkbox columns clarity:

<?php  $room_id = $room->id; $room_configuration = $room->configuration; $room_user_id = $room->user_id;  if ( $room->trained == 1 ) {     $trained_checked = 'checked';     $trained_label = 'y'; } else {     $trained_checked = '';     $trained_label = 'n'; }  if ( $room->active == 1 ) {     $active_checked = 'checked';     $active_label = 'active'; } else {     $active_checked = '';     $active_label = 'inactive'; }  echo <<<eot      <td><input type="checkbox" name="trained[$room_id]" value="$room_user_id" $trained_checked> $trained_label</td>     <td><input type="checkbox" name="active[$room_id]" value="$room_user_id" $active_checked> $active_label</td>     <td>$room_configuration</td>     <td>$room_id</td>     <td>$room_user_id</td>  eot;  ?> 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -