Click here to Skip to main content
16,020,706 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I i'm trying to insert multiple row of data into the database using an array and a foreach loop in php the data are collected from a form with multiple radiobuttons.

php returns me an illegal offset type error.
this is the arror in question
Warning: Illegal offset type in C:\wamp64\www\Projet Apache\My website\html\addquestionrequest.php on line 40

What I have tried:

the piece of code am working on is this

$req = $db->query('SELECT Questionid FROM question WHERE question = \''.$_POST['question'].'\'');
while ($donnees = $req->fetch())
{
$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";
}
...
Posted
Updated 2-Jan-20 22:54pm
v2
Comments
[no name] 3-Jan-20 4:21am    
Warning: Illegal offset type in C:\wamp64\www\Projet Apache\My website\html\addquestionrequest.php on line 40
Where is line 40 and what are you using as the offset?
winsderlich@yahoo.fr 3-Jan-20 4:25am    
sorry i'm not understanding your question
winsderlich@yahoo.fr 3-Jan-20 4:55am    
still a newbie to foreach and arrays

1 solution

try using this array

$insertArray =
 array(
 1=>array("Questionid"=>$questionid,"Options"=>$_POST['Option_1']),
 2=>array("Questionid"=>$questionid,"Options"=>$_POST['Option_2']),
 3=>array("Questionid"=>$questionid,"Options"=>$_POST['Option_3']),
 4=>array("Questionid"=>$questionid,"Options"=>$_POST['Option_4']));



and on foreach remove single quotes use it like this i.e
$value[Questionid]
 
Share this answer
 
Comments
winsderlich@yahoo.fr 7-Jan-20 10:33am    
thanks it have help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900