Hey guys!
I am trying to follow this tutorial, which is based on php oop but I am doing php procedure but I can only insert the first option of my array into my database:
<?php
include_once 'includes/dbh.php';
include_once 'header.php';
$sql = "SELECT * FROM users WHERE user_uid = ?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo 'SQL statement failed';
} else {
//Bind parameters to the placeholder
mysqli_stmt_bind_param($stmt, "s", $_SESSION['u_uid']);
//Run parameters inside database
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
if ($row['admin'] == 0) {
header("Location: header.php?add=notadmin");
exit;
} else {
if (isset($_POST['submit'])) {
include_once 'includes/dbh.php';
$question_number = $_POST['question_number'];
$question_text = $_POST['question_text'];
$correct_choice = $_POST['correct_choice'];
//Choices array
$choices = array();
$choices[1] = $_POST['choice1'];
$choices[2] = $_POST['choice2'];
$choices[3] = $_POST['choice3'];
$choices[4] = $_POST['choice4'];
$correct_choice = $_POST['correct_choice'];
$sql2 = "INSERT INTO questions (question_number, text) VALUES (?,?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql2)) {
echo 'SQL statement failed';
} else {
//Bind parameters to the placeholder
mysqli_stmt_bind_param($stmt, "is", $question_number, $question_text);
//Run parameters inside database
$result = mysqli_stmt_execute($stmt);
if ($result) {
foreach($choices as $choice => $value) {
if ($value != '') {
if ($correct_choice == $choice) {
$is_correct = 1;
} else {
$is_correct = 0;
}
echo $value;
echo $choices[1];
echo $choices[2];
echo $choices[3];
echo $choices[4];
// Choice query
$sql3 = "INSERT INTO choices (question_number, is_correct, text) VALUES (?,?,?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql3)) {
echo 'SQL statement failed';
} else {
//Bind parameters to the placeholder
mysqli_stmt_bind_param($stmt, "iis", $question_number, $is_correct, $value);
mysqli_stmt_execute($stmt);
}
header("Location: quiz.php?add=success");
exit();
}
}
}
}
$sql = "SELECT * FROM questions;";
$stmt = mysqli_stmt_init($conn);
//Prepare the prepared statement
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo 'SQL statement failed';
} else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($result); // total questions
$next = $resultCheck+1;
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP QUIZZER</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
<div class="quiz_container">
<h2>For Administrator uses only! Add a Question</h2>
<form action="" method="POST" class="signup-form">
<p>
<label id="question_number">Question Number: </label>
<input type="number" value="<?php echo $next; ?>" name="question_number" />
</p>
<br />
<p>
<label>Question Text: </label>
<input type="text" name="question_text" />
</p>
<br />
<p>
<label>Choice #1: </label>
<input type="text" name="choice1" />
</p>
<br />
<p>
<label>Choice #2: </label>
<input type="text" name="choice2" />
</p>
<br />
<p>
<label>Choice #3: </label>
<input type="text" name="choice3">
</p>
<br />
<p>
<label>Choice #4: </label>
<input type="text" name="choice4">
</p>
<br />
<br />
<p>
<label>Correct Choice Number: </label>
<input type="number" name="correct_choice">
</p>
<p>
<input type="submit" name="submit" value="submit">
</p>
</form>
</div>
</main>
</body>
</html>
This is the video that I am following: