2

So i was looking for solution for a couple of hours now.. my problem is that the data from the dropdown list (yearlevel) wont save in the database...

here's the full code of my work named 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>';
            }    
    }

?>

Did i do anything wrong ? I really hope that you guys will be able to help me ! Thank you so much in advance !

3
  • Don't quote the null here: VALUES ('NULL', Commented Oct 13, 2015 at 10:43
  • Other than the obvious sermon against using mysql_* functions, what i have to say is print out the sql statement to see that it's what you expect it to be. Then run it in the mysql console. Commented Oct 13, 2015 at 10:46
  • i have changed the null part.. I dont know how to do that 'running in the mysql console' thing since i'm a beginner in php Commented Oct 13, 2015 at 15:00

2 Answers 2

1

I think you should try this on ur last two query:

  $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')";
Sign up to request clarification or add additional context in comments.

3 Comments

where did the yearlevel go ?
it will not inserted!!
oh, but what i want to happen is to insert the value in the dropdown list in my database..
0

try getting the value into a hidden input and then access it like this.

<!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>';
        }    
}

?>

2 Comments

it says Undefined index: yearlevel2
I've edited my answer to show the entire code. You must make sure that the hidden input field is within a form, in your case it should be within the second form. Let me know if this works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.