-1

I have 2 checkboxes fields grabbing data from table. I would like to insert both of them in table. i success when i insert one of them but when i try to insert both of them i fail.

My form:

    <form action="select_role_insert.php" method="post" >
    <label>Supervisor</label> 
    <?php

        $reqm = "SELECT manager_name FROM manager_name ";
        $repm = mysqli_query($dbc, $reqm);
        while ($rowm = mysqli_fetch_array($repm))
        {
           $manager_name= $rowm['manager_name'];
        ?>

     <input type="checkbox"  name="Supervisor[]"    
     value="<?php echo $manager_name?>" /> <?php echo $manager_name?><hr/>
     <?php
     }
     ?>

    <label>Speciality</label>
    <?php
       $req = "SELECT  name FROM claims_follow_up.user_speciality";
       $rep = mysqli_query($dbc, $req);
       while ($row = mysqli_fetch_array($rep)) {         
       $name = $row['name'];
    ?>
    <input type="checkbox" name="Speciality[]" 
      value="<?php echo $name?>" /> <?php echo $name ?><hr/>
    <?php}?>

select_role_insert.php - code that insert only one variable

     $Speciality = $_POST['Speciality'];

     foreach($Speciality as $i => $Speciality)
     {
        $carGroups = mysqli_query($dbc,"INSERT INTO    
        client_services SET Speciality ='$Speciality '");
     }

Please help me to insert both supervisor and speciality variables.. Thanks in advance

profesor79
  • 9,213
  • 3
  • 31
  • 52
baptpro
  • 23
  • 5
  • Your code is vulnerable to SQL injection. Please read [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) for information on how to fix it. – Matt Raines Mar 11 '16 at 00:05
  • I fix that sql injection issue. I just don't post it... my goal was to insert variables. Just to simplify i post it like this. thanks – baptpro Mar 11 '16 at 00:10

2 Answers2

-1

Behold the solution i find it works perfectly. Thank you for your advices

$Supervisor = $_POST['Supervisor'];
$Speciality2 = $_POST['Speciality'];

    $arraye = array_combine($Supervisor, $Speciality2);
    foreach($arraye as $k=> $a){
$carGroups = mysqli_query($dbc,"INSERT INTO  claims_follow_up.client_services SET supervisor='$k', service='$a'");
    }
baptpro
  • 23
  • 5
-1

Your form:

<form action="select_role_insert.php" method="post" >
<label>Supervisor</label> 
<?php

    $reqm = "SELECT manager_name FROM manager_name ";
    $repm = mysqli_query($dbc, $reqm);
    while ($rowm = mysqli_fetch_array($repm))
    {
       $manager_name= $rowm['manager_name'];
    ?>

 <input type="checkbox"  name="Supervisor[]"    
 value="<?php echo $manager_name?>" /> <?php echo $manager_name?><hr/>
 <?php
 }
 ?>

<label>Speciality</label>
<?php
   $req = "SELECT  name FROM claims_follow_up.user_speciality";
   $rep = mysqli_query($dbc, $req);
   while ($row = mysqli_fetch_array($rep)) {         
   $name = $row['name'];
?>
<input type="checkbox" name="speciality_<?php echo $name?>" 
  value="<?php echo $name?>" /> <?php echo $name ?><hr/>
<?php}?>

select_role_insert.php - code that insert all variables starting with speciality


 $Speciality = $_POST;

 foreach($Speciality as $i => $Speciality)
 {
    if(substr($Speciality,0,10)=="speciality"){
       $carGroups = mysqli_query($dbc,"INSERT INTO    
       client_services (Speciality) VALUES ('$Speciality')");
    }
 }
esdebon
  • 2,460
  • 5
  • 26
  • 33