dqg63264 2019-01-23 02:57
浏览 59

为什么我将未选择的数据添加到我的数据库中?

Im trying to insert multiple data that has only been selected checkbox. But Unfortunately, It also inserted the the unselected checkbox and also getting duplicate data.

Here's the UI for selecting data.

enter image description here

Here's my code:

if(isset($check)){

$client = $_POST['client'];
$project = $_POST['project'];
$kit_no = $_POST['kit_no']; //array
$kit_desc = $_POST['kit_desc']; //array

for($i=0;$i<sizeof($check);$i++){
for($i=0;$i<sizeof($kit_no);$i++){
for($i=0;$i<sizeof($kit_desc);$i++){

$query=mysqli_query($con,"INSERT INTO tbltrainsets_kit(id, client, project, kit_no, kit_desc)VALUES(
    '',                      
    '".$client."',
    '".$project."',
    '".$kit_no[$i]."',
    '".$kit_desc[$i]."'
    )")or die(mysqli_error($con));          
} 
} 
}

}

The output that I produce is like this: (Please see the actual pix below):

enter image description here

The unselected data ('NA') was inserted into the database. It should only be the item 'DTR4000037253' inserting into the database.

Thank you in advace. Please respect my questions. I'm still studying do please do not bash. :|

  • 写回答

1条回答 默认 最新

  • duandi5328 2019-01-24 03:03
    关注

    Ok, I find the solution for this. When I study my code I realized that all of the logic is wrong.. (Shame on me!) :D,

    Now for the newbies like me who are not yet found the answer for this kind of problem here's my code:

    if(isset($check)){ // verify if checkbox is checked
    foreach($check as $selected){ // select each checkbox that was selected an
    
     // select value from the column equal to the ID of the selected checkbox
    $kit_no = $this->dbquery->modGetdynamicval('tblkit','id', $selected, 'kit_no');   
    $kit_desc = $this->dbquery->modGetdynamicval('tblkit','id', $selected, 'kit_desc');
    
    
    
    $query=mysqli_query($con,"INSERT INTO tbltrainsets_kit(id, client, project, kit_no, kit_desc)
    VALUES('',  '".$client."','".$project."','".$kit_no."','".$kit_desc."')")or die(mysqli_error($con));
    
    echo $selected."</br>";
    echo $kit_no."</br>";
    echo $kit_desc."</br>";
    
    }//end for loop
    

    Im using CI framework for getting the value the other 2 variables that I want to insert. And thats it.

    Hope it helps. :D

    评论

报告相同问题?

悬赏问题

  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析