0

I get an error/notice like: Undefined offset: 0

I know the problem is my array, but i have no idea to fix it.. so i need your help :/

I have a form in a jquery datatables, where the data comes from mysql. the form looks like this example:

+-----+-------+----------------------+
| no. | name  |    visitorstatus     |
+-----+-------+----------------------+
|   1 | max   | o private o business |
|   2 | peter | o private o business |
|   3 | lisa  | o private o business |
+-----+-------+----------------------+

(the "o"´s are the radio buttons)

the form for the radio buttons are:

echo '<td><input type="radio" id="radio" name="visitorstatus['.$row->visitorid.']" value="private"> private <td>';
echo '<td><input type="radio" id="radio2" name="visitorstatus['.$row->visitorid.']" value="business"> business</td>';

the form for the names is:

echo '<td><input type="checkbox" name="visitorname[]" value="' .$row->visitornameid. '"></td>';

and this is my way of insert the data into sql:

    $visitorstatus = $_POST['visitorstatus'];
    $visitorname = $_POST['visitorname'];
    $count_visitors = count($_POST['visitorname']);

        for ($i = 0; $i < $count_visitors; $i++) {
            $_visitorname = $visitorname[$i];
            $_visitorstatus = $visitorstatus[$i]; //<-- this is line 80


            $sql = "INSERT INTO exchange (visitorname, visitorstatus)
                  VALUES (
                  :visitorname,
                  :visitorstatus)";

            $stmt = $pdo->prepare($sql);
            $stmt->bindParam(':visitorname', $_visitorname, PDO::PARAM_INT);
            $stmt->bindParam(':visitorstatus', $_visitorstatus, PDO::PARAM_INT);
            $stmt->execute();
        }

result: Undefined offset: 0 at line 80

:(

robsn
  • 7
  • 4
  • pls tell me, where my question answered at your reference?? i need help with a solution,.. i know the problem but i can't fix it! – robsn Nov 14 '16 at 09:03
  • `$_POST['visitorstatus']` doesn't seem to be defined as you have not created any input element to take its value – jitendrapurohit Nov 14 '16 at 09:08
  • Check the type of the `$visitorstatus` variable, and you should be able to see the problem then. "Undefined offset" tells me PHP recognizes this as a `string`, not an array. – ChristianF Nov 14 '16 at 09:09
  • i have edit my form to get the $_POST['visitorstatus'] @jitendrapurohit.. the radio buttons send the visitorstatus via post – robsn Nov 14 '16 at 09:13
  • done @ ChristianF this is what var_dump(); says: Array(1) { [61]=> string(6) "private" }.. but how can i use this information for my insert/for loop ?_? – robsn Nov 14 '16 at 09:14
  • Use `array_values($visitorstatus);` just after `$visitorstatus = $_POST['visitorstatus'];` line – jitendrapurohit Nov 14 '16 at 09:19
  • thx so much jitendrapurohit, this helps me a lot! – robsn Nov 14 '16 at 09:30

0 Answers0