简体   繁体   中英

Insert 2 php variable in mysql table

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

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'");
    }

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')");
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM