猿问

如何将sql添加到javaScipt函数中?

我希望用户的输入进入数据库,但它不起作用。我有一个 sql 代码工作块,可以将数据插入数据库,但运行时我不断收到“未定义”错误。这是完整运行的sql代码


<?php

require 'creationlink.php';


$quizname = $_POST['name'];

$quest = $_POST['question'];

$ans1 = $_POST['answer1'];

$ans2 = $_POST['answer2'];

$ans3 = $_POST['answer3'];

$ans4 = $_POST['answer4'];

$correct = $_POST['A'];


  //initiates connection with database with creation file

  $sql = "INSERT INTO Quiz(question, answer1, answer2, answer3, answer4, correct) VALUES (?, ?, ?, ?, ?, ?)";


  if($stmt = mysqli_prepare($conn, $sql)){

      // Bind variables to the prepared statement as parameters

      mysqli_stmt_bind_param($stmt, "ssssss", $quest, $ans1, $ans2, $ans3, $ans4, $correct);


      // Attempt to execute the prepared statement

      if(mysqli_stmt_execute($stmt)){

          echo "Records inserted successfully.";

      } else{

          echo "ERROR: Could not execute query: $sql. " . mysqli_error($conn);

      }

  }

  else{

      echo "ERROR: Could not prepare query: $sql. " . mysqli_error($conn);

  }

  ?>

这是我希望应用 sql 的代码,它只是一个简单的测验模板,根据用户输入进行迭代。


繁星淼淼
浏览 59回答 1
1回答

紫衣仙女

在您的 HTML 中,所有<input>元素都在 之外<form>,因此您会收到错误undefined。要解决该错误,请将您的<input>元素移动到<form>标签中。尝试如下替换您的 html 代码。<html>&nbsp; <body>&nbsp; &nbsp; <form id='namennumber' action="includes/dbquestions.php" method="post">&nbsp; &nbsp; &nbsp; <label for="quizname"><b>Quiz name</b></label>&nbsp; &nbsp; &nbsp; <input name="quizname" type="text" placeholder="Enter quizname" required>&nbsp; &nbsp; &nbsp; <div id="myHTMLWrapper">&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </form>&nbsp; &nbsp; <div style="border:solid 1px #ddd; padding:10px; display:none;" id="cont">&nbsp; &nbsp; &nbsp; <div id="questionform" class="modal">&nbsp; &nbsp; &nbsp; &nbsp; <form action="includes/dbquestions.php"> //moved all the input elements&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="question"><b>Enter a question</b></label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="question" type="text" placeholder="Enter a question" value="question" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="answer1">Possible answer A</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="answer1" placeholder="Enter a possible answer" value="answer1" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="answer2">Possible answer B</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="answer2" placeholder="Enter a possible answer" value="answer2" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="answer3">Possible answer C</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="answer3" placeholder="Enter a possible answer" value="answer3" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="answer4">Possible answer D</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="answer4" placeholder="Enter a possible answer" value="answer4" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3>Correct answer</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="A" <?php if (isset($answer) && $answer=="A") echo "checked"; ?> value="A" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="A">A</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="A" <?php if (isset($answer) && $answer=="B") echo "checked"; ?> value="B" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="B">B</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="A" <?php if (isset($answer) && $answer=="C" )echo "checked"; ?> value="C" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="C">C</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="A" <?php if (isset($answer) && $answer=="D" )echo "checked"; ?> value="D" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="D">D</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button style="width:auto;" id="enter" class="btn btn-primary big-btn">Save</button>&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; var wrapper = document.getElementById("myHTMLWrapper");&nbsp; &nbsp; &nbsp; var number = prompt("Enter the number of questions");&nbsp; &nbsp; &nbsp; var myHTML = '';&nbsp; &nbsp; &nbsp; for (var i = 0; i < number; i++) {&nbsp; &nbsp; &nbsp; &nbsp; myHTML += '<span class="test">Question ' + (i + 1)&nbsp; &nbsp; &nbsp; &nbsp; myHTML += '<input type="button" value="Content" id="bt" onclick="toggle(\'cont\')">' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</span><br/><br/>';&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; wrapper.innerHTML = myHTML&nbsp; &nbsp; &nbsp; function toggle(cont) {&nbsp; &nbsp; &nbsp; &nbsp; var cont = document.getElementById('cont');&nbsp; &nbsp; &nbsp; &nbsp; if (cont.style.display == 'block') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cont.style.display = 'none';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(ele.id).value = 'Show DIV';&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cont.style.display = 'block';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(ele.id).value = 'Hide DIV';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; </script>&nbsp; </body></html>请注意,您对HTML 中的action两个s使用相同的参数, 。<form>见下文。action="includes/dbquestions.php"由于您在服务器端代码中使用,因此$_POST您必须指定methodas ,因为 a 的默认方法是 is和 not 。post<form><form>getpost更新 根据您在评论中发布的错误,我合并了您使用的两种表格。<html>&nbsp; <body>&nbsp; &nbsp; <form id='namennumber' action="includes/dbquestions.php" method="post">&nbsp; &nbsp; &nbsp; <label for="quizname"><b>Quiz name</b></label>&nbsp; &nbsp; &nbsp; <input name="quizname" type="text" placeholder="Enter quizname" required>&nbsp; &nbsp; &nbsp; <div id="myHTMLWrapper">&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; <div style="border:solid 1px #ddd; padding:10px; display:none;" id="cont">&nbsp; &nbsp; &nbsp; <div id="questionform" class="modal">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="question"><b>Enter a question</b></label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="question" type="text" placeholder="Enter a question" value="question" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="answer1">Possible answer A</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="answer1" placeholder="Enter a possible answer" value="answer1" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="answer2">Possible answer B</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="answer2" placeholder="Enter a possible answer" value="answer2" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="answer3">Possible answer C</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="answer3" placeholder="Enter a possible answer" value="answer3" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="answer4">Possible answer D</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="answer4" placeholder="Enter a possible answer" value="answer4" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3>Correct answer</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="A" <?php if (isset($answer) && $answer=="A") echo "checked"; ?> value="A" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="A">A</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="A" <?php if (isset($answer) && $answer=="B") echo "checked"; ?> value="B" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="B">B</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="A" <?php if (isset($answer) && $answer=="C" )echo "checked"; ?> value="C" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="C">C</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="A" <?php if (isset($answer) && $answer=="D" )echo "checked"; ?> value="D" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="D">D</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button style="width:auto;" id="enter" class="btn btn-primary big-btn">Save</button>&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; var wrapper = document.getElementById("myHTMLWrapper");&nbsp; &nbsp; &nbsp; var number = prompt("Enter the number of questions");&nbsp; &nbsp; &nbsp; var myHTML = '';&nbsp; &nbsp; &nbsp; for (var i = 0; i < number; i++) {&nbsp; &nbsp; &nbsp; &nbsp; myHTML += '<span class="test">Question ' + (i + 1)&nbsp; &nbsp; &nbsp; &nbsp; myHTML += '<input type="button" value="Content" id="bt" onclick="toggle(\'cont\')">' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</span><br/><br/>';&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; wrapper.innerHTML = myHTML&nbsp; &nbsp; &nbsp; function toggle(cont) {&nbsp; &nbsp; &nbsp; &nbsp; var cont = document.getElementById('cont');&nbsp; &nbsp; &nbsp; &nbsp; if (cont.style.display == 'block') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cont.style.display = 'none';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(ele.id).value = 'Show DIV';&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cont.style.display = 'block';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(ele.id).value = 'Hide DIV';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; </script>&nbsp; </body></html>
随时随地看视频慕课网APP

相关分类

Html5
我要回答