mdmartiny Posted February 4, 2020 Share Posted February 4, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/309976-while-loop-works-till-a-box-is-checked/ Share on other sites More sharing options...
requinix Posted February 5, 2020 Share Posted February 5, 2020 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/309976-while-loop-works-till-a-box-is-checked/#findComment-1573999 Share on other sites More sharing options...
mdmartiny Posted February 5, 2020 Author Share Posted February 5, 2020 I want to display all of the categories that are in the database and if the post is using one of the categories it puts a check in the box. That way the categories can be changed if the user wants to change it. Quote Link to comment https://forums.phpfreaks.com/topic/309976-while-loop-works-till-a-box-is-checked/#findComment-1574001 Share on other sites More sharing options...
requinix Posted February 5, 2020 Share Posted February 5, 2020 Yeah. I got that. Quote Link to comment https://forums.phpfreaks.com/topic/309976-while-loop-works-till-a-box-is-checked/#findComment-1574002 Share on other sites More sharing options...
mdmartiny Posted February 5, 2020 Author Share Posted February 5, 2020 I reread what you wrote and thought about it and it made sense and I seen what you were saying. Thank you for your help Quote Link to comment https://forums.phpfreaks.com/topic/309976-while-loop-works-till-a-box-is-checked/#findComment-1574006 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.