,所以我現在正在尋找解決方案了幾個小時..我的問題是,從下拉列表(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>';
}
}
?>
難道我做錯什麼? 我真的希望你們能幫助我!提前感謝你!
不要在這裏引用了'null':'VALUES( 'NULL',' –
較明顯講道其他反對使用mysql_ *功能,我不得不說的是打印出來在sql語句中看到它是你期望的,然後在mysql控制檯中運行它 – e4c5
我已經改變了null部分..我不知道該怎麼做'在mysql控制檯中運行'的事情,因爲我是一個初學者在php –