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

无法通过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

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.