在MYSQL数据库中插入下拉列表值
,所以我现在正在寻找解决方案了几个小时..我的问题是,从下拉列表(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>';
}
}
?>
难道我做错什么? 我真的希望你们能帮助我!提前感谢你!
尝试获得价值进入一个隐藏的输入,然后访问它像这样。 ?
<!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>';
}
}
>
它说未定义的inde x:yearlevel2 –
我编辑了我的答案以显示整个代码。你必须确保隐藏的输入字段在表单内,在你的情况下它应该在第二个表单中。让我知道这是否适合你。 – mikie
哇!它的工作非常感谢你! :) –
我想你应该试试这个乌拉圭回合最后两个查询:
$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')";
年级去哪里? –
它不会插入!! –
哦,但我想要发生的是插入在我的数据库中的下拉列表中的值.. –
不要在这里引用了'null':'VALUES( 'NULL',' –
较明显讲道其他反对使用mysql_ *功能,我不得不说的是打印出来在sql语句中看到它是你期望的,然后在mysql控制台中运行它 – e4c5
我已经改变了null部分..我不知道该怎么做'在mysql控制台中运行'的事情,因为我是一个初学者在php –