Saya ingin masukan dari pengguna saya untuk masuk ke database, tetapi tidak berfungsi. Saya memiliki blok kode sql yang berfungsi yang akan memasukkan data ke dalam database, tetapi saya terus mendapatkan kesalahan "tidak terdefinisi" ketika dijalankan. Ini adalah kode sql yang berfungsi penuh

<?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);
  }
  ?>

Dan ini adalah kode yang saya ingin sql untuk diterapkan, yang hanya merupakan template kuis sederhana yang diulang tergantung pada input pengguna.

<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">
         <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>
         <form action="includes/dbquestions.php">
<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>
</div>
</body>
1
katekatekate 15 Maret 2020, 22:19

1 menjawab

Jawaban Terbaik

Dalam HTML Anda, semua elemen <input> berada di luar <form> dan karenanya Anda mendapatkan kesalahan undefined. Untuk mengatasi kesalahan, pindahkan elemen <input> Anda ke tag <form>. Coba ganti kode html Anda seperti di bawah ini.

<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>

Sebagai catatan, Anda menggunakan parameter yang sama untuk action, untuk <form> dalam HTML Anda. Lihat di bawah.

action="includes/dbquestions.php"

Karena Anda menggunakan $_POST dalam kode sisi server, Anda harus menetapkan method sebagai post untuk <form> Anda, sebagai metode default untuk <form> adalah get dan bukan post.

PERBARUI Dari kesalahan Anda, yang telah Anda posting di komentar, saya telah menggabungkan 2 formulir yang Anda gunakan.

<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>
0
Lal 15 Maret 2020, 19:52