0

I am trying to insert multiple radio button value in to the database using an array but I've obtained an error

Warning: Illegal offset type in C:\wamp64\www\Projet Apache\My website\html\addquestionrequest.php on line 40

this is the part of code am working on

$questionid = $donnees['Questionid'];

    $insertArray = array(
        [0]=> array(

                ['Questionid'] =>$questionid,
                ['Options'] => $_POST['Option_1'],
        ),

        [1]=> array(
            ['Questionid'] =>$questionid,
            ['Options'] => $_POST['Option_2'],
        ),

        [2]=> array(
            ['Questionid'] =>$questionid,
            ['Options'] => $_POST['Option_3'],
        ),

        [3]=> array(
            ['Questionid'] =>$questionid,
            ['Options'] => $_POST['Option_4'],
        ));
        if (!empty($_POST['Option_1']) && !empty($_POST['Option_2']) && !empty($_POST['Option_3']) && !empty($_POST['Option_4']))
         {
                foreach ($insertArray as $key => $value) {
                     $db->exec('INSERT INTO answerproposal(Questionid,Options) VALUES(\'$value["Questionid"]\', \'$value["Options"] \')');

                }


             echo "question ajoutée";
        }
        else {
            echo " erreur option 1";
        }
KyleK
  • 4,643
  • 17
  • 33
winnie
  • 1

1 Answers1

2

Your array keys should not contain brackets, when creating the array.

$insertArray = array(
    0 => array(
        'Questionid' => $questionid,
        'Options' => $_POST['Option_1']
    ),
    1 => array(
        'Questionid' =>$questionid,
        'Options' => $_POST['Option_2']
    ),
    2 => array(
        'Questionid' =>$questionid,
        'Options' => $_POST['Option_3']
    ),
    3 => array(
        'Questionid' => $questionid,
        'Options' => $_POST['Option_4']
    )
);
nl-x
  • 11,762
  • 7
  • 33
  • 61