Jump to content

While loop works till a box is checked


mdmartiny

Recommended Posts

I am creating a while loop puts a checkmark in a check box when two variables match. The code works till I place a checkmark in a check box. When that happens I get this error message :Fatal error: Uncaught Error: Call to a member function bind_param() on boolean. I can't figure out what is causing this to error out. 

This is the code that I am using 

<fieldset>
    <legend>Categories</legend>

    <?php

    $stmt2 = mysqli_query($db, 'SELECT catID, catTitle FROM blog_cats ORDER BY catTitle');
    while($row2 = mysqli_fetch_assoc($stmt2)){

        $stmt3 = $db->prepare('SELECT catID FROM blog_post_cats WHERE catID = ? AND postID = ?') ;
        $stmt3->bind_param("ii", $row2['catID'], $postID);
        $stmt3->execute();
        $stmt3->bind_result($row3catID);
        $stmt3->fetch();

        if($row3catID == $row2['catID']){
            $checked = 'checked = "checked"';
        } else {
            $checked = null;
        }

        echo '<input type="checkbox" name="catID[]" value="'.$row2['catID'].'" '.$checked.'>'.$row2['catTitle'].'<br />';
    }

    ?>

</fieldset>

This is what the source looks like when using google chrome

<fieldset>
    <legend>Categories</legend>

    <input type="checkbox" name="catID[]" value="2" >Development<br /><input type="checkbox" name="catID[]" value="9" >Earn Money From Home<br /><input type="checkbox" name="catID[]" value="1" checked = "checked">General<br /><br />
<b>Fatal error</b>:  Uncaught Error: Call to a member function bind_param() on boolean in /storage/ssd2/059/12276059/public_html/admin/edit-post.php:141
Stack trace:
#0 {main}
  thrown in <b>/storage/ssd2/059/12276059/public_html/admin/edit-post.php</b> on line <b>141</b><br />

The first two first two empty check boxes work just fine then the third is checked and after that the code stops working,

I don't understand why.

Link to comment
Share on other sites

It's not about the checkbox being checked. The problem is that the $stmt3 query returned a row, you read it, and that you didn't finish reading until it was done. Unbuffered statements (which you are apparently using) must be fully read from before you can start another query, and "fully read from" means you don't just read the one row you were expecting but you keep going until you don't get any more rows.

But the solution here isn't to do that.

The whole point of prepared statements is that you only need to do one of them. Preparing the same statement over and over again inside a loop is the exact opposite of how it should be used. Prepared statements should be prepared once, have their input variables set up once, and then what you repeat in a loop is setting the variables to some new values and executing the statement.

But the solution here isn't to do that either.

You're potentially running a bunch of queries here, right? One to get the list of categories, then one for each of those categories to see if the post uses it. Wouldn't it be better if you just ran one query for all the categories and it told you whether the post used each one?

SELECT bc.catID, bc.catTitle, NOT ISNULL(bpc.postID) AS inuse
FROM blog_cats bc
LEFT JOIN blog_post_cats bpc ON bc.catID = bpc.catID
WHERE bpc.postID = ?
ORDER BY bc.catTitle

Prepare that query, bind in your $postID, execute it, then fetch all the results from it. Each row will tell you the category ID and name, as well as whether it is in use for the post.

  • Thanks 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.