dousi4950 2018-10-26 19:42
浏览 59
已采纳

无法通过Gyrocode将$ _POST ARRAY INSERT到MySQL - JQuery DataTables复选框

I am aware that there have been a lot of questions on $_POST ARRAY and MySQL, and I've been through a lot of them. However, none of the ones I have looked at do the trick for me.

I am trying to pass the row id's from a JQuery DataTable (using Gyrocode's ckeckboes plugin), form submit.

If I use:-

$i = 0;
    foreach($_POST['id'] as $value){

        echo "value : ".$value. '<br/>';

        $i++;
    }

I get output of

value: 1 value: 25 value: 32 value: 5 value: 17

Which is what I would expect (the amount of lines and the number values depend on how many checkboes I check).

But when I put the query in, it won't save to the database:-

$i = 0;
    foreach($_POST['id'] as $value){

        $sql = "INSERT INTO attendance (rider_id, at_date) VALUES ('$value', NOW())";

        $i++;
    }

I have tried all kinds of variations of this but nothing I do seems to work and it seems like a dumb mistake I'm making :)

Thanks

  • 写回答

1条回答 默认 最新

  • duanhui7329 2018-10-27 09:57
    关注

    Thanks for the comments. The js and HTML are the samples taken from Gyrocode. I know it's open to SQL injection attacks, but this is not for an open network, just something I am playing with.

    I managed to get it working with the following PHP

    session_start();
    include_once('connection.php');
    
    $rider_id = isset($_POST['id']) ? $_POST['id'] : [];
    
    foreach($rider_id as $k=>$id) {
    
      $query = "INSERT INTO attendance (rider_id, at_date) VALUES ('{$id}', NOW())";
    
      $conn->query($query); 
    }
    
    header('location: index.php');
    

    Just need to add some error handling.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了