1

I have many checkbox items where each employee has access to different department . I want to add checked items into database.

code to display access to doors if checked

 $door1 = $_POST['door1'];
 echo ' dooraccess is available for ' .$door1. '<br>'; 

 $door2 = $_POST['door2'];
 echo 'dooraccess is available for  ' .$door2. '<br>'; 

 $door3 = $_POST['door3'];
 echo 'dooraccess is available for  ' .$door3. '<br>'; 

 $door4 = $_POST['door4'];
 echo 'dooraccess is available for  ' .$door4. '<br>'; 

 $door5 = $_POST['door5'];
 echo 'dooraccess is available for  ' .$door5. '<br>'; 

 $door6 = $_POST['door6'];
 echo 'dooraccess is available for  ' .$door6. '<br>'; 

 $door7 = $_POST['door7'];
 echo 'dooraccess is available for  ' .$door7. '<br>'; 

 $door8 = $_POST['door8'];
 echo 'dooraccess is available for  ' .$door8. '<br>'; 

 $door9 = $_POST['door9'];
 echo 'dooraccess is available for  ' .$door9. '<br>'; 

 $door10 = $_POST['door10'];
 echo 'dooraccess is available for  ' .$door10. '<br>'; 

 $door11 = $_POST['door11'];
 echo 'dooraccess is available for  ' .$door11. '<br>'; 

 $door12 = $_POST['door12'];
 echo 'dooraccess is available for  ' .$door12. '<br>'; 

In case the values that was not checked then it will display the error as below:

Undefined index: door7 in [since door7 was not checked]

Code to insert these values along with other values to database php mysql database

 if(isset($_POST['submit']))
  { 
   $query = "INSERT INTO form_details1(firstname,secondname,location,designation,fileno,doa,doj,cardtype1,cardcolor,cardtype2,door1,door2,door3,door4,door5,door6,door7,door8,door9,door10,door11,door12,door13) VALUES('$firstname','$secondname','$location','$designation','$fileno','$doa','$doj','$cardtype1',$cardcolor,'$cardtype2','$door1','$door2','$door3','$door4','$door5','$door6','$door7','$door8','$door9','$door10','$door11','$door12')"; //query
  if(mysqli_query($connect,$query)){ //check query executed or not
     echo 'inserted' . '<br>' ;
   }

Query doesn't insert into database since query has the values which is unchecked

I want to check all the checked items using if loop or any other means and enter only those items that has been checked. How can I do this ?

My table looks like

+---------+---------+-------+
|   Id    |  door1  | door2 |
+---------+---------+-------+
| Value 1 | Value 2 |   123 |
| Value 2 | NULL    |   123 |
+---------+---------+-------+

The uncheck box will be empty in the table .

what should be the mysql field datatype for array having values that has been selected

Community
  • 1
  • 1
iman
  • 19
  • 8

3 Answers3

1

You need to use checkbox array. Here check this stackoverflow answer.

Get $_POST from multiple checkboxes

Community
  • 1
  • 1
Sylvester
  • 388
  • 4
  • 12
1

Change PHP code to:

function doorAccess($doorNum){
    return 'dooraccess is available for ' .$doorNum. '<br>';
}

if(!empty($_POST['door1'])) echo ($_POST['door1']);
if(!empty($_POST['door2'])) echo ($_POST['door2']);
if(!empty($_POST['door3'])) echo ($_POST['door3']);
if(!empty($_POST['door4'])) echo ($_POST['door4']);
if(!empty($_POST['door5'])) echo ($_POST['door5']);
if(!empty($_POST['door6'])) echo ($_POST['door6']);
if(!empty($_POST['door7'])) echo ($_POST['door7']);
if(!empty($_POST['door8'])) echo ($_POST['door8']);
if(!empty($_POST['door9'])) echo ($_POST['door9']);
if(!empty($_POST['door10'])) echo ($_POST['door10']);
if(!empty($_POST['door11'])) echo ($_POST['door11']);
if(!empty($_POST['door12'])) echo ($_POST['door12']);
Hossam
  • 1,126
  • 8
  • 19
1

first check if the checkboxes are set: $door1 = isset($POST['door1'])? 1 : 0 ;

Then your query works: Doors which an employee have access to have a 1, others have a 0.

optimization:

  1. always use parametrized queries as described here: Parameterized Query

  2. to loop easier over your checkboxes, you can set the name like this: <input type="checkbox" name="door[1]"> <input type="checkbox" name="door[2]">... and then loop:

    foreach($_POST['doors'] as $num => $value){

    }

List item

  1. Normalize database (make a table doors where you put all the doors. Then make a table is_checked with a foreign key to the employees table and the door table. If there is an entry with the employee id and the 1st door id, then this employee has access to the first door. And it is easier to add a new door, if necessary.)
Community
  • 1
  • 1
BacLuc
  • 93
  • 1
  • 4