gilestodd Posted May 17, 2012 Share Posted May 17, 2012 I am trying to install a comments box, im new to php and mysql so havent been able to work this out for myself following other threads, hope someone can shed some light on the problem. The posts ive entered arent being returned to the page essentially and im getting the error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in commentbox.php on line 42. This is the code from the entire page. <?php require('commentconnect.php'); $name=@$_POST['name']; $comment=@$_POST['comment']; $submit=@$_POST['submit']; if($submit) { if($name&&$comment) { $insert=mysql_query("INSERT INTO commenttable (name,comment) VALUES ('$name','$comment')"); /*header("Location: index.php");*/ echo "<script>document.location.href='index.php'</script>"; echo "<script>'Content-type: application/octet-stream'</script>"; } else { echo "Please fill out the fields"; } } ?> <div id="commentdiv"> <div id="commentinput"> <form action="index.php" method="POST"> <table> <tr><td>Name: </td><td><input type="text" name="name" /></td></tr> <tr><td colspan="2">Comment: </td></tr> <tr><td colspan="2"><textarea name="comment"></textarea></td></tr> <tr><td colspan="2"><input type="submit" name="submit" value="Comment" /></td></tr> </table> </form> </div> <div id="commentarea"> <?php $getquery=mysql_query("SELECT * FROM commenttable ORDER BY id DESC"); while($rows=mysql_fetch_array($getquery)) { $id=$rows['id']; $name=$rows['name']; $comment=$rows['comment']; $dellink="<a href=\"delete.php?id=" . $id . "\"> Delete </a>"; echo $name . '' . '<br />' . $comment . '<br />' . '<hr/>'; } ?> </div> </div> Any help is greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/262677-warning-mysql_fetch_array-expects-parameter-1-to-be-resource/ Share on other sites More sharing options...
mrMarcus Posted May 17, 2012 Share Posted May 17, 2012 Replace that bottom chunk with: <?php $sql = "SELECT * FROM commenttable ORDER BY id DESC"; if ($getquery = mysql_query($sql)) { while ($rows=mysql_fetch_array($getquery)) { $id=$rows['id']; $name=$rows['name']; $comment=$rows['comment']; $dellink="<a href=\"delete.php?id=" . $id . "\"> Delete </a>"; echo $name . '' . '<br />' . $comment . '<br />' . '<hr/>'; } } else { trigger_error(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/262677-warning-mysql_fetch_array-expects-parameter-1-to-be-resource/#findComment-1346349 Share on other sites More sharing options...
gilestodd Posted May 18, 2012 Author Share Posted May 18, 2012 Thanks mrMarcus, Do you think you'd be able to help me fix a repeating post issue im having? The most recent post I make duplicates when I refresh the page. Thanks again for the advice! Quote Link to comment https://forums.phpfreaks.com/topic/262677-warning-mysql_fetch_array-expects-parameter-1-to-be-resource/#findComment-1346630 Share on other sites More sharing options...
mrMarcus Posted May 18, 2012 Share Posted May 18, 2012 Thanks mrMarcus, Do you think you'd be able to help me fix a repeating post issue im having? The most recent post I make duplicates when I refresh the page. Thanks again for the advice! You mean the same values are inserting into the database when you hit refresh on the browser? That's expected. If not, please elaborate. Quote Link to comment https://forums.phpfreaks.com/topic/262677-warning-mysql_fetch_array-expects-parameter-1-to-be-resource/#findComment-1346632 Share on other sites More sharing options...
gilestodd Posted May 20, 2012 Author Share Posted May 20, 2012 Yep that is correct. After choosing a name and a message and hitting submit the post is posted. If at this point the page is refreshed the comment and name duplicate on in the comment display area and is duplaicated in the database. Do you need more information? Quote Link to comment https://forums.phpfreaks.com/topic/262677-warning-mysql_fetch_array-expects-parameter-1-to-be-resource/#findComment-1347102 Share on other sites More sharing options...
Jessica Posted May 20, 2012 Share Posted May 20, 2012 After you process the information, redirect them to a different page to display the results. Quote Link to comment https://forums.phpfreaks.com/topic/262677-warning-mysql_fetch_array-expects-parameter-1-to-be-resource/#findComment-1347156 Share on other sites More sharing options...
gilestodd Posted May 21, 2012 Author Share Posted May 21, 2012 How would I redirect them to the page they're currently on when entering data into the comments box. I've included the file with the comments box in on multiple pages so can't redirect them to a specific page as it might not be the one they were on initially. <?php require('commentconnect.php'); $name=@$_POST['name']; $comment=@$_POST['comment']; $submit=@$_POST['submit']; if($submit) { if($name&&$comment) { $insert=mysql_query("INSERT INTO commenttable (name,comment) VALUES ('$name','$comment')"); /*header("Location: index.php");*/ echo "<script>document.location.href='index.php'</script>"; echo "<script>'Content-type: application/octet-stream'</script>"; } else { echo "Please fill out the fields"; } } ?> <div id="commentdiv"> <div id="commentinput"> <form action="index.php" method="POST"> <table> <tr><td>Name: </td><td><input type="text" name="name" /></td></tr> <tr><td colspan="2">Comment: </td></tr> <tr><td colspan="2"><textarea name="comment"></textarea></td></tr> <tr><td colspan="2"><input type="submit" name="submit" value="Comment" /></td></tr> </table> </form> </div> <div id="commentarea"> <?php $sql = "SELECT * FROM commenttable ORDER BY id DESC"; if ($getquery = mysql_query($sql)) { while ($rows=mysql_fetch_array($getquery)) { $id=$rows['id']; $name=$rows['name']; $comment=$rows['comment']; $dellink="<a href=\"delete.php?id=" . $id . "\"></a>"; echo $name . '' . '<br />' . $comment . '<br />' . '<hr/>'; } } else { trigger_error(mysql_error()); } ?> </div> </div> This is the included comments box page. Quote Link to comment https://forums.phpfreaks.com/topic/262677-warning-mysql_fetch_array-expects-parameter-1-to-be-resource/#findComment-1347296 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.