2015-10-13 73 views
2

,所以我現在正在尋找解決方案了幾個小時..我的問題是,從下拉列表(yearlevel)中的數據不會保存到數據庫中...在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>'; 
      }  
    } 

?> 

難道我做錯什麼? 我真的希望你們能幫助我!提前感謝你!

+0

不要在這裏引用了'null':'VALUES( 'NULL',' –

+0

較明顯講道其他反對使用mysql_ *功能,我不得不說的是打印出來在sql語句中看到它是你期望的,然後在mysql控制檯中運行它 – e4c5

+0

我已經改變了null部分..我不知道該怎麼做'在mysql控制檯中運行'的事情,因爲我是一個初學者在php –

回答

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

>

+0

它說未定義的inde x:yearlevel2 –

+0

我編輯了我的答案以顯示整個代碼。你必須確保隱藏的輸入字段在表單內,在你的情況下它應該在第二個表單中。讓我知道這是否適合你。 – mikie

+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')"; 
+0

年級去哪裏? –

+0

它不會插入!! –

+0

哦,但我想要發生的是插入在我的數據庫中的下拉列表中的值.. –

相關問題