在MYSQL数据库

时间:2015-10-13 10:41:18

标签: php mysql

所以我现在正在寻找几个小时的解决方案..我的问题是下拉列表中的数据(年级)不会保存在数据库中......

这里是我的作品的完整代码,名为 qwe.php

<!DOCTYPE html>
<html>
<head>
</head>

<form method = "POST" name = "subjects" action ="qwe.php">

<?php
require_once('xcon.php');
$query = "SELECT yearlevel from yearlevel";
$result = mysql_query($query);

echo "Select Year Level: <select name ='yearlevel'>";
    while($row = mysql_fetch_array($result)){
        echo "<option value = '" . $row['yearlevel'] . "'>" . $row['yearlevel'] . "</option>";
    }
    echo "</select><br>";

?>

Multiple Choice: <input type = "text" name="MC"><br>
Identification: <input type = "text" name ="Identification"><br>

                 <input type = "submit" name = "confirm" value = "Confirm">
</form>


<?php

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

$MC = $_POST['MC'];
$Identification = $_POST['Identification'];
echo "<form method = 'POST' name = 'items' action ='qwe.php'>";
$items = 1;
$items2 = 1;

echo "<center>MULTIPLE CHOICE</center><br><br><br>";
    for ($x = 1; $x <= $MC; $x++) {

        echo "Question Number $items:"; echo "<input type = 'text' name = 'questions[]' style='width: 500px'><br><br>";
        echo "A. "; echo "<input type = 'text' name = 'ans1[]'>";
        echo "B. "; echo "<input type = 'text' name = 'ans2[]'><br>";
        echo "C. "; echo "<input type = 'text' name = 'ans3[]'>";
        echo "D. "; echo "<input type = 'text' name = 'ans4[]'><br>";
        echo "Correct Answer: "; echo "<input type = 'text' name ='correctans[]'><br><br>";
        $items++;

    }
echo "<center>IDENTIFICATION</center><br><br><br>";
    for ($y = 1; $y <= $Identification; $y++){
        echo "Question # $items2:"; echo "<input type = 'text' name = 'identification[]' style='width: 500px'><br>";
        echo "Answer: "; echo "<input type = 'text' name = 'identificationans[]'><br><br>";
        $items2++;
    }
        echo "<input type ='submit' name = 'save' value = 'Save'>";
        echo "</form>";
}

?>

<?php

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

        $yearlvl = $_POST['yearlevel'];
        require_once('xcon.php');

        foreach ($_POST['questions'] as $key => $question){
            $ans1 = $_POST['ans1'][$key];
            $ans2 = $_POST['ans2'][$key];
            $ans3 = $_POST['ans3'][$key];
            $ans4 = $_POST['ans4'][$key];
            $correctans = $_POST['correctans'][$key];

            echo "<input type = 'hidden' value = '$question'>";
            echo "<input type = 'hidden' value = '$yearlvl'>";

            $query = "INSERT INTO mcq (mc_id, questions, ans1, ans2, ans3, ans4, correctans, yearlvl) 
                  VALUES ('NULL','$question','$ans1','$ans2','$ans3','$ans4','$correctans', '$yearlvl')";
        $result = mysql_query($query);

        }


        foreach($_POST['identification'] as $key => $identification){
            $identificationans = $_POST['identificationans'][$key];

            $query = "INSERT INTO identification (identification_id, identification_question, identification_answer, yearlvl)
                    VALUES ('NULL','$identification','$identificationans','$yearlvl')";
            $result = mysql_query($query);
        }

            if($result){
                echo 'Insert Success!<br>';
            }else{
            echo 'Error<br>';
            }    
    }

?>

我做错了什么吗? 我真的希望你们能够帮助我!非常感谢你!

2 个答案:

答案 0 :(得分:1)

我认为你应该在最后两个问题上尝试这个:

  $query = "INSERT INTO mcq (mc_id, questions, ans1, ans2, ans3, ans4, correctans, ) 
                          VALUES ('NULL','$question','$ans1','$ans2','$ans3','$ans4','$correctans')";


    $query = "INSERT INTO identification (identification_id, identification_question, identification_answer, )
                        VALUES ('NULL','$identification','$identificationans')";

答案 1 :(得分:0)

尝试将值放入隐藏的输入中,然后像这样访问它。

<!DOCTYPE html>
<html>
<head>
</head>

<form method = "POST" name = "subjects" action ="">

<?php
require_once('xcon.php');
$query = "SELECT yearlevel from yearlevel";
$result = mysql_query($query);

echo "Select Year Level: <select name ='yearlevel'>";
while($row = mysql_fetch_array($result)){
    echo "<option value = '" . $row['yearlevel'] . "'>" . $row['yearlevel'] . "</option>";
}
echo "</select><br>";

?>

Multiple Choice: <input type = "text" name="MC"><br>
Identification: <input type = "text" name ="Identification"><br>

<input type = "submit" name = "confirm" value = "Confirm">
</form>


<?php

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

$MC = $_POST['MC'];
$Identification = $_POST['Identification'];
echo "<form method = 'POST' name = 'items' action =''>";
$items = 1;
$items2 = 1;

$level = $_POST['yearlevel'];
echo'<input type="hidden" name="yearlevel2" value="'.$level.'">';
echo "<center>MULTIPLE CHOICE</center><br><br><br>";
for ($x = 1; $x <= $MC; $x++) {
echo "Question Number $items:"; echo "<input type = 'text' name = 'questions[]' style='width: 500px'><br><br>";
    echo "A. "; echo "<input type = 'text' name = 'ans1[]'>";
    echo "B. "; echo "<input type = 'text' name = 'ans2[]'><br>";
    echo "C. "; echo "<input type = 'text' name = 'ans3[]'>";
    echo "D. "; echo "<input type = 'text' name = 'ans4[]'><br>";
    echo "Correct Answer: "; echo "<input type = 'text' name ='correctans[]'><br><br>";
    $items++;

}
echo "<center>IDENTIFICATION</center><br><br><br>";
for ($y = 1; $y <= $Identification; $y++){
    echo "Question # $items2:"; echo "<input type = 'text' name = 'identification[]' style='width: 500px'><br>";
    echo "Answer: "; echo "<input type = 'text' name = 'identificationans[]'><br><br>";
    $items2++;
}
    echo "<input type ='submit' name = 'save' value = 'Save'>";
    echo "</form>";
}

?>


<?php

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

    $yearlvl = $_POST['yearlevel2'];

    echo $yearlvl;
    exit();
    require_once('xcon.php');

    foreach ($_POST['questions'] as $key => $question){
        $ans1 = $_POST['ans1'][$key];
        $ans2 = $_POST['ans2'][$key];
        $ans3 = $_POST['ans3'][$key];
        $ans4 = $_POST['ans4'][$key];
        $correctans = $_POST['correctans'][$key];

        echo "<input type = 'hidden' value = '$question'>";
        echo "<input type = 'hidden' value = '$yearlvl'>";

        $query = "INSERT INTO mcq (mc_id, questions, ans1, ans2, ans3, ans4, correctans, yearlvl) 
              VALUES ('NULL','$question','$ans1','$ans2','$ans3','$ans4','$correctans', '$yearlvl')";
    $result = mysql_query($query);

    }


    foreach($_POST['identification'] as $key => $identification){
        $identificationans = $_POST['identificationans'][$key];

        $query = "INSERT INTO identification (identification_id, identification_question, identification_answer, yearlvl)
                VALUES ('NULL','$identification','$identificationans','$yearlvl')";
        $result = mysql_query($query);
    }

        if($result){
            echo 'Insert Success!<br>';
        }else{
        echo 'Error<br>';
        }    
}

&GT;