简体   繁体   English

在mysql表中插入2个php变量

[英]Insert 2 php variable in mysql table

I have 2 checkboxes fields grabbing data from table.我有 2 个复选框字段从表中获取数据。 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 select_role_insert.php - 只插入一个变量的代码

     $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 select_role_insert.php - 插入所有以专业开头的变量的代码


 $Speciality = $_POST;

 foreach($Speciality as $i => $Speciality)
 {
    if(substr($Speciality,0,10)=="speciality"){
       $carGroups = mysqli_query($dbc,"INSERT INTO    
       client_services (Speciality) VALUES ('$Speciality')");
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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