为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

繁星淼淼 2023-09-11 16:34:15
我希望用户的输入进入数据库,但它不起作用。我有一个 sql 代码工作块,可以将数据插入数据库,但运行时我不断收到“未定义”错误。这是完整运行的sql代码<?phprequire '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 的代码,它只是一个简单的测验模板,根据用户输入进行迭代。
查看完整描述

1 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

在您的 HTML 中,所有<input>元素都在 之外<form>,因此您会收到错误undefined。要解决该错误,请将您的<input>元素移动到<form>标签中。尝试如下替换您的 html 代码。


<html>

  <body>

    <form id='namennumber' action="includes/dbquestions.php" method="post">

      <label for="quizname"><b>Quiz name</b></label>

      <input name="quizname" type="text" placeholder="Enter quizname" required>

      <div id="myHTMLWrapper">

      </div>

    </form>

    <div style="border:solid 1px #ddd; padding:10px; display:none;" id="cont">

      <div id="questionform" class="modal">

        <form action="includes/dbquestions.php"> //moved all the input elements

          <label for="question"><b>Enter a question</b></label>

          <input name="question" type="text" placeholder="Enter a question" value="question" required>

          <br>

          <label for="answer1">Possible answer A</label>

          <input type="text" name="answer1" placeholder="Enter a possible answer" value="answer1" required>

          <br>

          <label for="answer2">Possible answer B</label>

          <input type="text" name="answer2" placeholder="Enter a possible answer" value="answer2" required>

          <br>

          <label for="answer3">Possible answer C</label>

          <input type="text" name="answer3" placeholder="Enter a possible answer" value="answer3" required>

          <br>

          <label for="answer4">Possible answer D</label>

          <input type="text" name="answer4" placeholder="Enter a possible answer" value="answer4" required>

          <br>


          <h3>Correct answer</h3>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="A") echo "checked"; ?> value="A" />

          <label for="A">A</label>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="B") echo "checked"; ?> value="B" />

          <label for="B">B</label>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="C" )echo "checked"; ?> value="C" />

          <label for="C">C</label>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="D" )echo "checked"; ?> value="D" />

          <label for="D">D</label>

          <br>

          <button style="width:auto;" id="enter" class="btn btn-primary big-btn">Save</button>

        </form>

      </div>

    </div>

    <script>

      var wrapper = document.getElementById("myHTMLWrapper");


      var number = prompt("Enter the number of questions");

      var myHTML = '';

      for (var i = 0; i < number; i++) {

        myHTML += '<span class="test">Question ' + (i + 1)

        myHTML += '<input type="button" value="Content" id="bt" onclick="toggle(\'cont\')">' +

          '</span><br/><br/>';

      }


      wrapper.innerHTML = myHTML


      function toggle(cont) {

        var cont = document.getElementById('cont');

        if (cont.style.display == 'block') {

          cont.style.display = 'none';


          document.getElementById(ele.id).value = 'Show DIV';

        } else {

          cont.style.display = 'block';

          document.getElementById(ele.id).value = 'Hide DIV';

        }

      };

    </script>

  </body>

</html>

请注意,您对HTML 中的action两个s使用相同的参数, 。<form>见下文。


action="includes/dbquestions.php"

由于您在服务器端代码中使用,因此$_POST您必须指定methodas ,因为 a 的默认方法是 is和 not 。post<form><form>getpost


更新 根据您在评论中发布的错误,我合并了您使用的两种表格。


<html>

  <body>

    <form id='namennumber' action="includes/dbquestions.php" method="post">

      <label for="quizname"><b>Quiz name</b></label>

      <input name="quizname" type="text" placeholder="Enter quizname" required>

      <div id="myHTMLWrapper">

      </div>

    <div style="border:solid 1px #ddd; padding:10px; display:none;" id="cont">

      <div id="questionform" class="modal">

          <label for="question"><b>Enter a question</b></label>

          <input name="question" type="text" placeholder="Enter a question" value="question" required>

          <br>

          <label for="answer1">Possible answer A</label>

          <input type="text" name="answer1" placeholder="Enter a possible answer" value="answer1" required>

          <br>

          <label for="answer2">Possible answer B</label>

          <input type="text" name="answer2" placeholder="Enter a possible answer" value="answer2" required>

          <br>

          <label for="answer3">Possible answer C</label>

          <input type="text" name="answer3" placeholder="Enter a possible answer" value="answer3" required>

          <br>

          <label for="answer4">Possible answer D</label>

          <input type="text" name="answer4" placeholder="Enter a possible answer" value="answer4" required>

          <br>


          <h3>Correct answer</h3>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="A") echo "checked"; ?> value="A" />

          <label for="A">A</label>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="B") echo "checked"; ?> value="B" />

          <label for="B">B</label>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="C" )echo "checked"; ?> value="C" />

          <label for="C">C</label>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="D" )echo "checked"; ?> value="D" />

          <label for="D">D</label>

          <br>

          <button style="width:auto;" id="enter" class="btn btn-primary big-btn">Save</button>

        </form>

      </div>

    </div>

    <script>

      var wrapper = document.getElementById("myHTMLWrapper");


      var number = prompt("Enter the number of questions");

      var myHTML = '';

      for (var i = 0; i < number; i++) {

        myHTML += '<span class="test">Question ' + (i + 1)

        myHTML += '<input type="button" value="Content" id="bt" onclick="toggle(\'cont\')">' +

          '</span><br/><br/>';

      }


      wrapper.innerHTML = myHTML


      function toggle(cont) {

        var cont = document.getElementById('cont');

        if (cont.style.display == 'block') {

          cont.style.display = 'none';


          document.getElementById(ele.id).value = 'Show DIV';

        } else {

          cont.style.display = 'block';

          document.getElementById(ele.id).value = 'Hide DIV';

        }

      };

    </script>

  </body>

</html>



查看完整回答
反对 回复 2023-09-11
  • 1 回答
  • 0 关注
  • 58 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信