Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

PHP
<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 {
         //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'])) {

   
   
   $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'];



  $sql2 = "INSERT INTO questions (question_number, `text`) VALUES (?,?);";

  $stmt = mysqli_stmt_init($conn);
    //Prepare the prepared statement
    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);
    //     $result2 = mysqli_stmt_get_result($stmt);
         
    //   $resultCheck2 = mysqli_num_rows($result2);

        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);
    //Prepare the prepared statement
    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?
Posted
Updated 14-Aug-18 5:36am
Comments
piano0011 15-Aug-18 0:31am    
I hope that I have entered in the correct place as I need more clarification here.. I have tried to use mysqli_stmt_affected_rows as follows:

$sql2 = "INSERT INTO questions (question_number, `text`) VALUES (?,?);";

$stmt = mysqli_stmt_init($conn);
//Prepare the prepared statement
if (!mysqli_stmt_prepare($stmt, $sql2)) {
echo 'SQL statement failed';
} else {
mysqli_stmt_bind_param($stmt, "is", $question_number, $question_text);
mysqli_stmt_execute($stmt);
$result2 = mysqli_stmt_get_result($stmt);
$resultCheck2 = mysqli_stmt_affected_rows($result2);




if ($resultCheck2 < 1) {
header("Location: header.php?add=error");
exit();

but still get the same error:

You need to use mysqli_stmt_affected_rows[^] to get the number of inserted rows. mysqli_num_rows() is used only with result sets from SELECT statements.
 
Share this answer
 
Thanks for the reply and appreciate the assistance provided.. No wonder I can't use mysqli_num_rows as it is only used for select statements
 
Share this answer
 
So, would i do it similar to mysqli_num_rows? Can i do the following? $result=mysqli_stmt_affected_rows ($stmt);

If ($result >0) {}; etc
 
Share this answer
 
Comments
Richard MacCutchan 14-Aug-18 12:06pm    
Please do not post questions as Solutions. People will think you no longer need help. Edit your question and add any updates there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900