开发者社区> 问答> 正文

PHP如何单选按钮一一提交所选答案

0

我想在数据库中一一提交单选按钮中给出的答案,但是我不知道如何。我尝试了一些事情,但没有成功。

这是为了进行调查,这就是为什么我要保存问题的答案的原因。有关代码的问题是代码中的for循环。

我尝试更改我的Sql查询,但没有帮助

<form method="POST">
<?php
    include '../include/db_conn.php';
        $url = $_GET['surveyUrl'];
        $sqlGetSurvey = 'SELECT * FROM `survey_info` WHERE `survey_url`="'. $url .'"';
        $resultGetSurvey = mysqli_query($conn, $sqlGetSurvey);

        if ($resultGetSurvey ->num_rows > 0) {
            while ($row = $resultGetSurvey-> fetch_assoc()) {
                $surveyId = $row['survey_id'];
                $sqlGetQuestion = 'SELECT * FROM `survey_questions` WHERE `survey_id`="'. $surveyId .'"';
                $sqlGetSurvey = 'SELECT * FROM `survey_info` WHERE `survey_id`="'. $surveyId .'"';
                $resultGetQuestion = mysqli_query($conn, $sqlGetQuestion);
                $resultGetSurvey = mysqli_query($conn, $sqlGetSurvey);
                if ($row = mysqli_fetch_assoc($resultGetSurvey)) {
                    $text_color = $row['survey_text_color'];
                    $background_color = $row['survey_background_color'];
                    $theme_color = $row['survey_theme_color'];
                }
                if ($resultGetQuestion ->num_rows > 0) {
                    while ($row = $resultGetQuestion-> fetch_assoc()) {
                        $questionId = $row['question_id'];
                        $count = 'A';


                        $yuyu = "<ul  class='list-group mx-auto mt-3'>
                        <li class='list-group-item'>". $row['question'] ."</li>";

                        $sqlGetAnswers = 'SELECT * FROM `survey_question_answers` WHERE `question_id`='. $questionId .'';
                        $resultGetAnswers = mysqli_query($conn, $sqlGetAnswers);

                        if ($resultGetAnswers ->num_rows > 0) {
                            while ($row = $resultGetAnswers-> fetch_assoc()) {
                                $yuyu .= "<li class='list-group-item'><span class='badge badge-light main-color-bg'>". $count .". <input type='radio' name='$questionId' value='$count'></span> ". $row['answer'] ."</li>";

                                ++$count;

                            }
                        }
                        echo '</ul>';
                        echo $yuyu;
                    }
                }
            }
        }


        if(isset($_POST['submit']))
        {

        for($x= 1; $x <= $questionId; $x++){
            $antwoord = $_POST[$questionId];
            $sql = "INSERT INTO survey_answers (question_id, survey_id, answers) VALUES ('$questionId', '$surveyId', '$antwoord')";
        }
    }


?>
    <input type="submit" name="submit" value="Show answer" class="btn btn-primary mb-3 mt-3">
我希望它在我的数据库中一一插入

展开
收起
被纵养的懒猫 2019-09-27 19:39:16 445 0
0 条回答
写回答
取消 提交回答
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
阿里云栖开发者沙龙PHP技术专场-深入浅出网络编程与swoole内核-吴镇宇 立即下载
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载