mysql如何向数据库中插入多个选择调查答案?

标签: MySQL PHP HTML
发布时间: 2017/2/17 2:47:39
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我正在做一个网站,在那里用户可以回答调查。现在我的代码看起来像 that(answering.php):

<?php
include_once 'init/init.funcs.php';
$_SESSION['pollid']=(int) $_GET['pollid'];
$questions = array();
if (!isset($_SESSION['answering'])) {
    $result = mysql_query('SELECT * from katse_kysimused where kysimustik_id="' . $_SESSION['pollid'] . '"');
    while($row = mysql_fetch_assoc($result)) {
        $questions[] = $row['kysimus'];
        }
    $_SESSION['answering']['questions'] = $questions;
    $_SESSION['answering']['index'] = 0;
}
    $x = $_SESSION['answering']['index'];
    $result3 = mysql_query('SELECT tyyp_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x] . '"');
    $type = mysql_result($result3, 0);
    if ($type=='3'){
        echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/valikvastustega_kysimusele_vastamine.php'>";
        $answer = $_POST['answer'];
        }

    if ($type=='1'){
            echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/tekstkysimusele_vastamine2.php'>";
            $answer = $_POST['answer'];
        }

if(isset($_POST['submit'])){
    $result2 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x -1] . '"');
    $q_id = mysql_result($result2, 0);
    mysql_query('INSERT INTO katse_vastused2 (id, vastus,kysimus_id, vastustik_id) VALUES (NULL,"' . $answer . '","' . $q_id . '","1")');
    }
$_SESSION['answering']['index']++;
?>

我有第二个代码,这意味着运行如果问题类型就是多个答案 question(if $type=='3')(valikvastustega_kysimusele_vastamine.php):

<?php
include_once 'init/init.funcs.php';
$x = $_SESSION['answering']['index'];
        echo $_SESSION['answering']['questions'][$x-1];
        $result4 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x-1] . '"');
        $question_id = mysql_result($result4, 0);
        $result5 = mysql_query('SELECT * from katse_valik_vastused where kysimus_id="' . $question_id . '"');
        if($result5 === FALSE) {
            die(mysql_error());
        }
        while($row = mysql_fetch_assoc($result5)) {
            $options[] = $row['vasuts'];
        }
        //foreach($options as $option=>$option_value) {
            //echo $option_value;
        $count=count($options);
?>
<html>
<br>
<form method="post" action="answering.php">
<?php 
foreach($options as $option=>$option_value) {

?>

<input type="radio" name="<?= $option; ?>"><?php echo $option_value?><br>


<?php }?>

<input name= "submit" type="submit" value="Vasta">
</form>

现在,一切工作正常,接下来我想要做的事情是向数据库中插入的答案。我应该写进我的代码,使其工作。现在,它只适用于文本的问题。

解决方法 1:

嗯,一种方法可能如下所示︰

answers (question_id*,answer_id*,answer) 

在那里 * = (组件) 的主键

官方微信
官方QQ群
31647020