【问题标题】:how to fix php illegal offset errors [duplicate]如何修复php非法偏移错误[重复]
【发布时间】:2020-04-21 01:50:46
【问题描述】:

我正在尝试使用数组将多个单选按钮值插入到数据库中,但出现错误

警告:C:\wamp64\www\Projet Apache\My 中的偏移类型非法 website\html\addquestionrequest.php 在第 40 行

这是正在处理的代码部分

$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";
        }

【问题讨论】:

标签: php html css mysql arrays


【解决方案1】:

在创建数组时,您的数组键不应包含括号。

$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']
    )
);

【讨论】:

  • 更重要的是,在这种情况下不需要键,只需要数组值,逗号分隔。
猜你喜欢
  • 2011-02-13
  • 1970-01-01
  • 2014-04-12
  • 2023-04-10
  • 2013-06-25
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 2017-09-05
相关资源
最近更新 更多