如何将多项选择调查答案插入数据库?

时间:2014-04-24 07:34:48

标签: php html mysql

我正在建立一个用户可以回答调查的网站。现在我的代码看起来像那样(answersering.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']++;
?>

我有第二个代码,如果问题类型是多个答案问题(如果$ type ==&#39; 3&#39;)(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 个答案:

答案 0 :(得分:0)

嗯,一种方式可能是这样的:

answers (question_id*,answer_id*,answer) 

其中* =(组件)PRIMARY KEY