duanmin0941 2014-11-13 15:51
浏览 51
已采纳

通过PHP将HTML表单数据插入mySQL(表单数组与表行数量不同)

I have a HTML form where users can checkmark up to 50 different questions. They have to checkmark minimum 1 and at the most all 50 questions.

After they submit the form I need to insert the data (form_array) to my mySQL table questions. The table contains of 50 questions rows (see below question table ex.).

I know the INSERT INTO 'questions' (question1, question2, question3, question4,....question50) VALUES (value1, value2, value3...), but my challenge is that since the amount of checked questions (values) can vary in the form, I do not know how to insert the form_array to my questions table.

The questions are inserted as true or false in the form_array depending on if they are marked or unmarked in the html form.

The questions table has a primary auto incremented ID and 2 foreign keys besides the 50 questions.

I welcome all suggestions/examples on how to insert an array with the above scenario?

The question table will look like this:

  `question1` tinyint(1) NOT NULL,
  `question2` tinyint(1) NOT NULL,
  `question3` tinyint(1) NOT NULL,
  `question4` tinyint(1) NOT NULL,
  `question5` tinyint(1) NOT NULL,
  `question6...etc etc...up to 50 questions
  • 写回答

1条回答 默认 最新

  • doulan8152 2014-11-13 16:44
    关注

    I think you want to store the answers to the 50 questions, right?

    First of all, your table structure should look something like this:

    questions
    ---------
    id: primary key, int(11), auto increments
    question/title: varchar(255)
    
    answers
    -------
    id: primary key, int(11), auto increments
    question_id: int(11)
    answer: tinyint(1)
    

    This is the tricky part. To display the questions (I used mysqli as I don't know what you're using):

    $result = $mysqli->query("SELECT * from questions");
    
    while ($question = $result->fetch_assoc()) {
        print 'Question: ' . $question['title'];
        print '<input type="hidden" name="answers[]['question_id']" value="' . $question['id'] . '">';
        print '<input type="radio" name="answers[]['answer']" value="1"> Yes'; 
        print '<input type="radio" name="answers[]['answer']" value="0"> No';
    }
    

    Now when a user submits his answers:

    if(isset($_POST['submit'])) {
        $answers = $_POST['answers'];
    
        if(count($answers) > 1) {
    
            //Loop through all the answers
            foreach($answers as $answer) {
                $mysqli->query("INSERTO INTO answer ('question_id', 'answer') VALUES ('{$answer['question_id']}', '{$answer['answer']')"); //Insert the answers one by one
            }
    
        } else {
           print 'You need to submit at least one answer!'
        }
    
    }
    

    I haven't tested the code. This is still pretty basic, but it's exactly what you need, I think... :-).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络