我正在尝试比较两个变量以查看它们是否相同,这些变量是$choice
和$correct_choice
如果 $choice == $correct_choice
那么这将给出1在$is_correct
,否则 0 。这将导致只有一个答案是正确的。不幸的是,我没有得到所需的结果,因为它们都是 0 或 1 。我查看了各种论坛并尝试了不同的方法,但仍然无法解决问题。
<?php
session_start();
include 'includes/dbh.inc.php';
include_once 'header.php';
if (isset($_POST['submit'])) {
//get the post variables
$question_number = $_POST['question_number'];
$question_text = $_POST['text'];
$correct_choice = $POST['correct_choice'];
//choice array
$choice = array(); //$choice is the key and choice1 etc.. is the value
$choice[1] = $_POST['choice1'];
$choice[2] = $_POST['choice2'];
$choice[3] = $_POST['choice3'];
$choice[4] = $_POST['choice4'];
$choice[5] = $_POST['choice5'];
//if the user clicks submit without entering a question an error is displayed otherwise the question is submitted.
if ($question_text == ''){
$error = 'ERROR: Please add question before clicking submit!';
echo $error;
} else {
//question query
$query = "INSERT INTO `questions`(question_number,text) VALUES ($question_number,'$question_text')";
//echo $query;
//run query
if (mysqli_query($conn,$query)){
echo "A new question has been created successfully";
} else {
echo "Error: " . $query . "<br>" . mysqli_error($conn);
}
}
if ($query) {
foreach ($choice as $choice => $value){
if ($value != '') {
if ($choice == $correct_choice){//if choices and correct_choice are the same put 1 in is_correct otherwise 0
$is_correct = 1;
}else{
$is_correct = 0;
}
//choice query
$query = "INSERT INTO `choices` (`question_number`,`is_correct`,`text`) VALUES ($question_number,$is_correct,'$value')";
//echo $query;
if (mysqli_query($conn,$query)) {
echo "New choices have been added successfully";
} else {
echo "Error: " . $query . "<br>" . mysqli_error($conn);
}
}
}
}
}
// get the total questions
$query = "SELECT * FROM `questions`";
//echo $query;
//get results from questions
$questions = $conn -> query($query) or die($mysqli->error.__LINE__);
$total = $questions -> num_rows;
$next = $total ++;
?>
<section class="main-container">
<div class="main-wrapper">
<h2>Quiz</h2>
<?php
if (isset($_SESSION['u_id'])) {
echo "You are logged in!";
}
?>
<main>
<div class = "container">
<br>
<h1>Add a question</h1>
<br>
<form method = "POST" action = "test2.php">
<p>
<label> Question Number:</label>
<input type = "number" value = "<?php echo $total?>" name = "question_number"/>
</p>
<br>
<p>
<label> Question Text</label>
<input type = "text" placeholder = "Enter the question here" name = "text"/>
</p>
<br>
<p>
<label>Choices #1:</label>
<input type = "text" placeholder = "Enter the first answer"name = "choice1"/>
</p>
<br>
<p>
<label>Choices #2:</label>
<input type = "text" placeholder = "Enter the second answer"name = "choice2"/>
</p>
<br>
<p>
<label>Choices #3:</label>
<input type = "text" placeholder = "Enter the third answer"name = "choice3"/>
</p>
<br>
<p>
<label>Choices #4:</label>
<input type = "text" placeholder = "Enter the fourth answer"name = "choice4"/>
</p>
<br>
<p>
<label>Choices #5:</label>
<input type = "text" placeholder = "Enter the fifth answer" name = "choice5"/>
</p>
<br>
<p>
<label>Correct Choice Number:</label>
<input type = "number" name = "correct_choice"/>
</p>
<br>
<input type = "submit" name = "submit" value = "submit" />
<br>
</form>
</div>
</main>
</section>
<br>
<?php
include_once 'footer.php';
?>
答案 0 :(得分:0)
您未正确访问SUPERGLOBAL $_POST
。
将$correct_choice = $POST['correct_choice'];
更改为$correct_choice = $_POST['correct_choice'];
因为你的代码有很多改进建议,所以我冒昧地试图尽可能多地升级它而不用看你的数据库结构(或测试我的代码)。
请尝试实施此代码。如果有语法错误或您希望我解释任何要点,请尽可能详细地给我留言,我会尽力帮助您。在查询中使用用户提供的数据时,始终使用带有占位符的预准备语句非常重要。
<?php
session_start();
include 'includes/dbh.inc.php';
include_once 'header.php';
if(isset($_POST['submit']) && isset($_POST['text']) && isset($_POST['choices']) && is_array($_POST['choices']) && isset($_POST['correct_choice'])){
// $question_number = $_POST['question_number']; don't do this, set the database table to auto-increment this Primary Key
$question_text=$_POST['text'];
$correct_choice=$_POST['correct_choice']; // underscore previously missing
//choice array
/*$choice = array(); //$choice is the key and choice1 etc.. is the value
$choice[1] = $_POST['choice1'];
$choice[2] = $_POST['choice2'];
$choice[3] = $_POST['choice3'];
$choice[4] = $_POST['choice4'];
$choice[5] = $_POST['choice5'];*/ // this is no longer necessary due to input field name change
//if the user clicks submit without entering a question an error is displayed otherwise the question is submitted.
if($question_text == ''){
echo 'ERROR: Please add question before clicking submit!';
}else{
if(!$stmt=$conn->prepare("INSERT INTO `questions`(text) VALUES (?)")){ // use ?s as placeholders to declare where the values will be inserted into the query
echo "<p>Prepare failed: ",$conn->error,"</p>"; // comment this out or remove error details when finished testing
}elseif(!$stmt->bind_param("s",$question_text)){ // assign the value type and variable name to be used when looping
echo "<p>Binding failed: (",$stmt->errno,") ",$stmt->error,"</p>"; // comment this out or remove error details when finished testing
}elseif(!$stmt->execute()){ // if the execute call fails
echo "<p>Execute failed: (",$stmt->errno,") ",$stmt->error,"</p>"; // comment this out or remove error details when finished testing
}else{
$id=$conn->insert_id;
echo "A new question ($id) has been created successfully";
if(!$stmt=$conn->prepare("INSERT INTO `choices` (`question_number`,`is_correct`,`text`) VALUES (?,?,?)")){ // use ?s as placeholders to declare where the values will be inserted into the query
echo "<p>Prepare failed: ",$conn->error,"</p>"; // comment this out or remove error details when finished testing
}elseif(!$stmt->bind_param("iis",$id,$is_correct,$choice)){ // assign the value type and variable name to be used when looping
echo "<p>Binding failed: (",$stmt->errno,") ",$stmt->error,"</p>"; // comment this out or remove error details when finished testing
}else{
$choices=array_filter($_POST['choices'],'strlen'); // remove empty (it is careful not to remove zeros in case zeros are valid choices) elements before looping
foreach($choices as $num=>$choice){
$is_correct=($choice==$correct_choice?1:0);
if(!$stmt->execute()){ // if the execute call fails
echo "<p>Execute failed @ choice #$num: (",$stmt->errno,") ",$stmt->error,"</p>"; // comment this out or remove error details when finished testing
}else{
echo "Choices #$num has been added successfully";
}
}
}
}
}
}
if(!$questions=$conn->query("SELECT `question_number` FROM `questions`")){ // this can be done a few different ways
echo $conn->error;
}else{
?>
<section class="main-container">
<div class="main-wrapper">
<h2>Quiz</h2>
<?php
if(isset($_SESSION['u_id'])){
echo "You are logged in!"; // I am assuming you will do something with this in the future
}
?>
<main>
<div class = "container">
<br>
<h1>Add a question</h1>
<br>
<form method="POST" action="test2.php">
<p>
<label> Question Number:</label>
<input type="number" value="<?php echo ++$questions->num_rows; ?>" disabled/> <!-- I would omit this <p> block from the form -->
</p>
<br>
<p>
<label> Question Text</label>
<input type="text" placeholder="Enter the question here" name="text" required/>
</p>
<br>
<p>
<label>Choices #1:</label>
<input type="text" placeholder="Enter the first answer" name="choices[1]"/> <!-- all of these choice fields are now arrays with static keys -->
</p>
<br>
<p>
<label>Choices #2:</label>
<input type="text" placeholder="Enter the second answer" name="choices[2]"/>
</p>
<br>
<p>
<label>Choices #3:</label>
<input type="text" placeholder="Enter the third answer" name="choices[3]"/>
</p>
<br>
<p>
<label>Choices #4:</label>
<input type="text" placeholder="Enter the fourth answer" name="choices[4]"/>
</p>
<br>
<p>
<label>Choices #5:</label>
<input type="text" placeholder="Enter the fifth answer" name="choices[5]"/>
</p>
<br>
<p>
<label>Correct Choice Number:</label>
<input type="number" name="correct_choice"/>
</p>
<br>
<input type="submit" name="submit" value="submit" />
<br>
</form>
</div>
</main>
<div> <!--- this line was missing -->
</section>
<br>
<?php
}
include_once 'footer.php';
?>