I'm trying to add questions to my quiz database, but when I submit a question I get the number 23?
The sql statements have been checked and are working OK ,if I alter the $insert_row =$conn, The question id is 0 instead of the next number, so no question or answers are added to the sql tables, if I try again it say that is a Duplicate entry '0' for key 'PRIMARY'23
I was following the video on you tube
https://www.youtube.com/watch?v=jwY8XlVAcfE&t=211sPHP Code:
<?php
session_start();
include 'dbh.inc.php';
include_once 'header.php';
if (isset($_POST['submit'])) {
//get the post varibles
$question_number = $_POST['question_number'];
$question_text = $_POST['question_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'];
//question query
$query = "INSERT INTO `questions` (`question_number`, `text`) VALUES ('$question_number', '$question_text')";
//run query
$insert_row = $conn->query($query) or die ($mysqli->error.__LINE__);
//Vaildate insert
if ($insert_row){
foreach($choice as $choice => $value){
if ($value != ''){
if ($correct_choice == $choice){
$is_correct = 1;
}else{
$is_correct = 0;
}
//choice query
$query = "INSERT INTO `choices`(`question_number`, `is _correct`, `text`) VALUES ('$question_number', '$is_correct','$value')";
//run query
$insert_row = $conn ->query($query) or die($mysqli->error.__LINE__);
//Vaildate insert
if ($insert_row){
continue;
}else{
die ('Error: ('.$mysqli->erron . ') '. $mysqli->error);
}
}
}
$msg = 'Question has been added';
}
}
// 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>
<?php if(isset($msg)){echo'<p>'.$msg.'</p>';}?>
<form method = "POST" action = "add.php"
<p>
<label> Question Number:</label>
<input type = "number" value = "<?php echo $next; ?>" name "question_number"/>
</p>
<br>
<p>
<label> Question Text</label>
<input type = "text" name "questiontext"/>
</p>
<br>
<p>
<label>Choices #1:</label>
<input type = "text" name "choice1"/>
</p>
<br>
<p>
<label>Choices #2:</label>
<input type = "text" name "choice2"/>
</p>
<br>
<p>
<label>Choices #3:</label>
<input type = "text" name "choice3"/>
</p>
<br>
<p>
<label>Choices #4:</label>
<input type = "text" name "choice4"/>
</p>
<br>
<p>
<label>Choices #5:</label>
<input type = "text" name "choice4"/>
</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';
?>
CodingForums...
ما را در سایت CodingForums دنبال می کنید
برچسب : نویسنده : codingforums بازدید : 165 تاريخ : شنبه 28 مرداد 1396 ساعت: 6:05