I have the following query and I can get it to insert into the database only if I don't use mysqli_num_rows($stmt); For some reason, when I am using it, it says something about boolean error but my query looks correct:
<pre><?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 {
mysqli_stmt_bind_param($stmt, "s", $_SESSION['u_uid']);
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'])) {
$question_number = $_POST['question_number'];
$question_text = $_POST['question_text'];
$correct_choice = $_POST['correct_choice'];
$choices = array();
$choices[1] = $_POST['choice1'];
$choices[2] = $_POST['choice2'];
$choices[3] = $_POST['choice3'];
$choices[4] = $_POST['choice4'];
$sql2 = "INSERT INTO questions (question_number, `text`) VALUES (?,?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql2)) {
echo 'SQL statement failed';
} else {
mysqli_stmt_bind_param($stmt, "is", $question_number, $question_text);
$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;
}
$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 {
mysqli_stmt_bind_param($stmt, "iis", $question_number, $is_correct, $value);
mysqli_stmt_execute($stmt);
}
}
}
} else {
echo "There are no results";
}
}
}
}
}
What I have tried:
I have tried to use mysqli_num_rows to see if it does return any row in the database but for some reason, it just can't insert it into the database.... I am just wondering... can you use num_rows if you don't have auto increment in the database?