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>';  
    }  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题