我正在为我的顶点课程创建一个Web应用程序,并且遇到了从下拉列表中将数据插入到我的表中的问题。我们可以填充它,但是当我试图将它发布到表格中时,我们并没有看到它出现。我们试图将其插入的表是一个关联表,我们正在尝试在其中插入QMID,Category和Questions。如果没有同时插入所有三个字段,我猜它不会显示一行。我的表格代码是:
<?php
require_once('php/CreateQuizSQL.php');
require_once('php/Connection.php');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Create a Quiz</title>
<link href="css/jquery.ui.core.min.css" rel="stylesheet" type="text/css">
<link href="css/jquery.ui.theme.min.css" rel="stylesheet" type="text/css">
<link href="css/jquery.ui.button.min.css" rel="stylesheet" type="text/css">
<link href="css/capm styles.css" rel="stylesheet" type="text/css">
<link href="css/jquery-ui.css" rel="stylesheet" type="text/css">
<link href="css/jquery-ui.min.css" rel="stylesheet" type="text/css">
<link href="css/jquery-ui.structure.css" rel="stylesheet" type="text/css">
<link href="css/jquery-ui.structure.min.css" rel="stylesheet" type="text/css">
<link href="css/jquery-ui.css" rel="stylesheet" type="text/css">
<link href="css/jquery-ui.theme.css" rel="stylesheet" type="text/css">
<link href="css/jquery-ui.theme.min.css" rel="stylesheet" type="text/css">
<link href="css/jQueryAssets/jquery.ui.datepicker.min.css" rel="stylesheet" type="text/css">
<script src="css/jQueryAssets/jquery-1.11.1.min.js"></script>
<script src="css/jQueryAssets/jquery.ui-1.10.4.datepicker.min.js"></script>
</head>
<body class="ui-widget" style="background:linear-gradient(white, white, white, white, #fece2f, #e78f08); background-repeat:no-repeat; background-attachment:fixed">
<ul style="list-style-type: none">
<li><a class="active" href="php/logoff.php">Log Out</a></li>
<li class="dropdown"><a href="#" class="dropbtn">Quizzes</a>
<div class="dropdown-content">
<a href="#">Course Quizzes</a>
<a href="PracticeExamStart.php">Practice Exam</a>
<a href="CreateAQuiz.php">Design A Quiz</a>
</div></li>
<li class="dropdown"><a href="Profile.php" class="dropbtn">Profile</a>
<div class="dropdown-content">
<a href="Profile.php">My Dashboard</a>
<a href="Settings.php">Settings</a>
<a href="Contacts.php">Contacts</a>
</div></li>
<li><a href="MainPage.php">Home</a></li>
<li class="dropdown"><a href="#" class="dropbtn">Admin</a>
<div class="dropdown-content">
<a href="AdminViewGrades.php">View Grades</a>
<a href="AdminReports.php">Build Reports</a>
<a href="AdminQuestions.php">Add/Edit Questions</a>
<a href="AdminCreateQuiz.php">Create A Quiz</a>
<a href="AdminUsers.php">Add/Edit Users</a>
</div></li>
</ul>
<header>
<a href="MainPage.php"><img src="images/THE ONE.png" width="341" height="136" alt=""/></a>
</header>
<section>
<div align="center"><a href="#addq">Create Quiz</a> • <a href="#uploadq">Assign Quiz</a> • <a href="#editdeleteq">Edit Quiz</a> •</div>
<form method="POST" action= "<?php $_PHP_SELF ?>">
<h4 id="addq" >Quiz Requirements</h4>
</p>
<p>
<label>
Number of Attempts: <input type="text" placeholder="Attempts" name="attempts" size="4"></label>
<br><br>
<label>
Time Limit: <input type="text" placeholder="Time Limit" name="timelimit" size="6"></label>
<br><br>
<label>Due Date: <input type="text" placeholder ="Date" name="date" size="10"></label>
<br><br>
</p>
<input type="submit" name="submit" value="Submit" />
</form>
<form method="POST" action= "<?php $_PHP_SELF ?>">
<h4 id="addq" >Select Category</h4>
<p>Quiz Master ID: </p>
<select name= "qmid">
<?php
$sql= mysqli_query($connect, "SELECT QMID FROM QuizMaster");
while ($row = $sql->fetch_assoc()){
echo "<option value=\"{$row['QMID']}\">" . $row['QMID'] . "</option>";
}
?>
</select>
<!--<?php if (!empty($dollarsign)) {echo("<b>$dollarsign</b>");}?></p>-->
<p>Category:</p>
<select name="category">
<?php
$sql = mysqli_query($connect, "SELECT Category FROM Category");
while ($row = $sql->fetch_assoc()){
echo "<option value=\"{$row['Category']}\">" . $row['Category'] . "</option>";
}
?>
</select>
<p><label>Number of Questions: <input type="text" placeholder="Quantity" name="questions" size="4"></label>
<p>If you want to add another category, click yes below.</p>
<!--<button id="Button2" type="Submit">Yes</button>--!>
<input type="submit" name="submit1" value="Submit" />
</form>
<script type="text/javascript">
function myFunction() {
<?php
$result = mysqli_query($connect, 'SELECT QMID FROM QuizMaster ORDER BY QMID DESC LIMIT 1;');
if (mysqli_num_rows($result) > 0) {
$max_QMID=$result-> fetch_assoc();
echo $max_QMID['QMID']; //Here it is
}
?>
}
</script>
</body>
</html>
然后该表单中的进程代码为:
<?php
include('Connection.php');// Connects us to the database
$error = ""; //Variable for storing our errors
//Check to see if the post was to itself.
if($_SERVER['REQUEST_METHOD']=== 'POST') {
//If any of the textboxes are empty, then show the error.
if(empty($_POST["attempts"]) || empty($_POST["timelimit"]) || empty ($_POST["date"]))
{
$error = "Please fill in all blanks.";
}
else{
/*if (mysqli_num_rows($result) > 0) {
$max_QMID=$result-> fetch_assoc();*/
$attempts= ($_POST['attempts']);
$timelimit= ($_POST['timelimit']);
$date= ($_POST['date']);
if(isset($_POST['questions'])){
$questions= ($_POST['questions']);}
if(isset($_POST['category'])){
$category= ($_POST['category']);}
if(isset($_POST['qmid'])){
$QMID= ($_POST['qmid']);}
$prepared_statement =$connect->prepare( 'INSERT INTO QuizMaster (DueDate, AttemptsMax, Time) VALUES (?,?, ?)');
$prepared_statement->bind_param("sss", $_POST["date"], $_POST["attempts"], $_POST["timelimit"]);
$prepared_statement->execute();
$prepared_statement->close();
// $dollarsign= $max_QMID['QMID']; //Here it is
// }
}
if(empty($_POST["attempts"]) && empty($_POST["timelimit"]) && empty ($_POST["date"]))
{
$prepared_statement=$connect->prepare('INSERT INTO Qmcategory (Quantity, Category, QMID) VALUES (?,?,?)');
$prepared_statement->bind_param("sss", $questions, ".$_POST['category'].", ".$_POST[qmid].");
$prepared_statement->execute();
$prepared_statement->close();
}
}
?>