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.

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

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名