doomli3721 2019-05-22 22:37
浏览 93
已采纳

我想在多列但同一个表中插入多个复选框值

My code inserts multiple selected values into two columns but both columns hold the value of the first listed(selected) checkbox. eg. if i selected .Net2 and Java3 two cloumns will be affected but both holds the value of 2.

<form action="" method="post" >  
   <div style="width:200px;border-radius:6px;margin:0px auto">  
<table border="1">  
   <tr>  
      <td colspan="2">Select Technolgy:</td>  
   </tr>  
   <tr>  
      <td>PHP1</td>  
      <td><input type="checkbox" name="techno[]" value="1"></td>  
   </tr>  
   <tr>  
      <td>.Net2</td>  
      <td><input type="checkbox" name="techno[]" value="2"></td>  
   </tr>  
   <tr>  
      <td>Java3</td>  
      <td><input type="checkbox" name="techno[]" value="3"></td>  
   </tr>  
   <tr>  
      <td>Javascript4</td>  
      <td><input type="checkbox" name="techno[]" value="4"></td>  
   </tr>  
   <tr>  
      <td colspan="2" align="center"><input type="submit" value="submit" name="sub"></td>  
   </tr>  
</table>  
</div>  
</form>  
<?php  
if(isset($_POST['sub']))  {  
    $connection = mysqli_connect("localhost", "root", ""); // Establishing Connection with Server
    $db = mysqli_select_db($connection, "ofobms"); // Selecting Database from Server
    // Check connection
    if (!$connection) {
        die("Connection failed: " . mysqli_connect_error());
    }  
$checkbox1=$_POST['techno']; 
//$hobb= $_POST['Hobbies']; 
$chk="";  
foreach($checkbox1 as $chk1)  
   {  
      $chk .= $chk1.",";   
   }  

   $N = count($checkbox1);
   echo("<p>You selected $N techno(s): ");
   for($i=0; $i < $N; $i++)
   {
    $in_ch=mysqli_query($connection,"INSERT INTO order_table (item_ID)
    values ('$chk')"); 
   } 

if($in_ch==2)  
   {  
      echo'<script>alert("Inserted Successfully")</script>';  
   }  
else  
   {  
      echo'<script>alert("Failed To Insert")</script>';  
   }  
}  
  ?>

this is what my current table looks like

| item_ID |
|    2      
|    2

i want it to be like this, for the second column to hold the value 3

| item_ID |
|    2      
|    3      
  • 写回答

1条回答 默认 最新

  • donglinli2027 2019-05-22 22:47
    关注

    You shouldn't be concatenating all the checkbox values. Each INSERT query needs to insert just the current element of $checkbox. You should also use a prepared statement to protect against SQL injection.

    You also don't need two loops, just one.

    Also, mysqli_query() returns either TRUE or FALSE when it's executing an INSERT query, I'm not sure why you're comparing it to 2.

    $N = count($checkbox1);
    echo("<p>You selected $N techno(s): ");
    $stmt = mysqli_prepare($connection, "INSERT INTO order_table (item_ID) values (?)");
    mysqli_stmt_bind_param($stmt, "i", $chk1);
    $success = true;
    foreach($checkbox1 as $chk1) {
        if (!mysqli_stmt_execute($stmt)) {
            $success = false;
            break;
        }
    }
    if($success)  
    {  
        echo'<script>alert("Inserted Successfully")</script>';  
    }  
    else  
    {  
        echo'<script>alert("Failed To Insert")</script>';  
    }  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog