Php 在MYSQL数据库中插入下拉列表值
所以我花了几个小时寻找解决方案。。我的问题是下拉列表(yearlevel)中的数据不会保存在数据库中 下面是我名为qwe.php的工作的完整代码Php 在MYSQL数据库中插入下拉列表值,php,mysql,Php,Mysql,所以我花了几个小时寻找解决方案。。我的问题是下拉列表(yearlevel)中的数据不会保存在数据库中 下面是我名为qwe.php的工作的完整代码 <!DOCTYPE html> <html> <head> </head> <form method = "POST" name = "subjects" action ="qwe.php"> <?php require_once('xcon.php'); $query = "SEL
<!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>';
}
}
?>
多项选择:
标识:
我想你应该在最后两个问题上尝试一下:
$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')";
尝试将值放入隐藏的输入中,然后像这样访问它
<!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>';
}
}
多项选择:
标识:
不要在此处引用null
:值('NULL',
除了反对使用mysql_*函数的明显说教之外,我要说的是打印sql语句,看看它是否符合您的期望。然后在mysql控制台中运行它。我已经更改了NULL部分。.我不知道如何做“在mysql控制台中运行”的事情,因为我是phpit的初学者:yearlevel2我已经编辑了我的答案以显示整个代码。你必须确保隐藏的输入字段在表单中,在你的情况下,它应该在第二个表单中。让我知道这是否适用于你。yearlevel到哪里去了?它不会被插入!!哦,但我想做的是在我的数据库的下拉列表中插入值。。