duanjun7801 2018-05-01 08:13
浏览 111

PHP SQL将Select语句转换为DB Insert的变量

I am currently in the process of creating a Quiz Builder, I am having a slight issue with some DB inserts. What I am trying to do within the code is insert a Quiz Title and Description to the Quiz table which will in turn create a Quiz ID. I want to insert this Quiz ID along with Class ID values from the checkbox. Any suggestions/advice would be greatly appreciated.

Note: I know I should be using prepare statements compared to what I am currently using. I am planning on fixing this issue once I get my main functionalities working.

 <form method="post" action="#">
                    <label>Quiz Title: </label>
                    <input type="text" placeholder="Insert Quiz Title here" name="quizTitle" class="form-control" />
                    <label>Quiz Description: </label>
                    <input type="text" placeholder="Insert Quiz Description here" name="description" class="form-control"  />


$showAllClasses = "SELECT * FROM class";
mysqli_query($mysqli, $showAllClasses) or die ('Error finding Classes');

$showClassesResult = mysqli_query($mysqli, $showAllClasses);

echo"<table border='1' cellpadding='10' align='center'>";

echo "<tr><th></th><th>Class ID</th><th>Class Name</th><th>Class 

 //while ($row = mysqli_fetch_assoc($result)){
  while ($row = $showClassesResult->fetch_object()){
  echo "<tr>";
  echo "<td><input type='checkbox' id='" .$row->classID . "' name='check_box[]' value='" .$row->classID . "'></td>";
  echo "<td>" .$row->classID . "</td>";
  echo "<td>" .$row->className . "</td>";
  echo "<td>" .$row->classDesc . "</td>";
  //echo "<td><button type='button' name='add' id='add' data-toggle='modal' data-target='#questionType' class='btn btn-success'>Edit Students</button></td>";
  echo "</tr>";


if (isset($_POST['submit'])) {
//Get POST variables
$quizTitle = '"' . $mysqli->real_escape_string($_POST['quizTitle']) . '"';
$description = '"' . $mysqli->real_escape_string($_POST['description']) . '"';
//echo $quizTitle;
//echo $description;

$getQuizIDQuery = "SELECT quizID FROM quiz ORDER BY quizID DESC LIMIT 1";

mysqli_query($mysqli, $getQuizIDQuery) or die ('Error getting Quiz ID');

$result = mysqli_query($mysqli, $getQuizIDQuery);
//$insertedQuizId = $mysqli->insert_id;

//Question query
$quizCreationQuery = "INSERT INTO quiz (quizTitle, description) VALUES($quizTitle, $description)";

foreach ($_POST['check_box'] as $classID) {

$ClassQuizQuery = "INSERT INTO quiz_class(classID, quizID) VALUES ('$classID', '$result')";

//$insert_ClassQuiz = $mysqli->query($ClassQuizQuery) or die($mysqli->error . __LINE__);

//Run Query
$insert_row = $mysqli->query($quizCreationQuery) or die($mysqli->error . __LINE__);

        <div align="center">
                    <input type="submit" name="submit" value="Submit" 
                    class="btn btn-info"/>
  • 写回答

1条回答 默认 最新


      相关推荐 更多相似问题


      • ¥15 香农解码的代码问题,无法输出解码结果
      • ¥15 内网同一网段设备和wifi隔离
      • ¥15 Python操作注册表
      • ¥45 入门级别的一段VUE前端拍照像后端发送请求的代码,帮排错
      • ¥15 anaconda打开spyder后一直闪退,不知道怎么办
      • ¥15 解决迷宫问题中无法运行的问题
      • ¥15 关于aspnetcore中使用mqttnet库的entire
      • ¥15 关于#python#的问题,请各位专家解答!
      • ¥100 关于远控软件的两个问题
      • ¥15 基于STM32的AD8232心电采集装置设计