Jump to content

Recommended Posts

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!

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());
}

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.

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?

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.

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.