Jump to content

Recommended Posts

Ok I am In the process of building a comment form which you can see on this page at the bottom.

 

http://mesquitechristmas.com/local/display.php?id=2

 

Now I have the form built and the test comment is actually coming from the database. Now here is where my problem comes in that I realized. I am going to need a separate table just for comments instead of just adding a column to the existing table because it can not hold more then one entry. Creating the new table is not a big deal but how would I go about sending the information to the table so that it I can retrieve the comments just for a certain page that comments have been left for.

 

For example.

 

Page A users leaves a comment. How can I enter that so they comments are just to displayed on page A and not other pages. I can set a user ID field in the comments table but how do I send that ID to the table in from the form?

 

Would it be?

 

<input type="hidden" name="id" value=" " />

 

Also what do I set as the value?

 

Any help or suggestions is appreciated.

 

-Thanks

Link to comment
https://forums.phpfreaks.com/topic/87291-building-a-comment-script/
Share on other sites

Give the table these fields (along with the other ones you plan on):

 

- comment_id (auto_increment)

 

And maybe one of the following:

- category

- topic_id

- topic_name

 

etc

 

So you keep track of what category the comment is in and you keep track of all the comments' ID. You can pull them out by filtering by category/topic_id/topic_name and ordering by ID.

 

I am not sure why I would need

 

- category

- topic_id

- topic_name

 

As you can see from the link I posted above it is a simple comment form. Do you look at it or even read my post?

 

How would I go about making sure the comment for that page is entered into the DB so I can retrieve it for that page. Since that page is displayed by ID as you can see in the URL I was thinking

 

- comment_id (auto_increment)

- comment

- name

- location

- id

 

but how would I go about inserting the id for that page into the database into the form. If I can figure that how I think I can figure out the rest.

 

-Thanks

 

 

Ok I have this set up but I need some help now cause I am stuck with what I thought would happen. I have the display id inserting into the new the new table for comments for each display. Now here is where the problem is. The comments for other displays are shown on all pages. So how can I just pull the comments to those pages for which the comments where left for.

 

For example you will notice both pages show the same comments.

 

http://mesquitechristmas.com/local/display.php?id=21

 

http://mesquitechristmas.com/local/display.php?id=2

 

Here is the code I am using. Any help would be great.

 

<?php

include ('db_connect.php');

if (isset($_GET['id'])) {
  $id = mysql_real_escape_string($_GET['id']);
  $sql = "SELECT * FROM users WHERE id = $id;";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_array($result);
      $id = $row["id"];
      $displayname = $row["displayname"];
      $displaytype = $row["displaytype"];
      $address = $row["address"];
      $city = $row["city"];
      $state = $row["state"]; 
      $postal = $row["postal"];
      $country = $row["country"];
      $rating = $row['rating'];
      $votes = $row['votes'];
      $imgurl = $row['imgurl'];
    } else {
      die("No user found");
    }
  } else {
    die(mysql_error());
  }
}

function show_data(){
   $id = array();
   $firstname = array();
   $location = array();
   $comment_date = array();
   $comment = array();
   $id = mysql_real_escape_string($_GET['id']);
   $i=0;
   if(isset($id)) {
      $sql = "SELECT * FROM comments";
      if($result = mysql_query($sql)) {
        $i = 0;
         while ($row = mysql_fetch_array ($result)) {
            $color = ($i%2== 0 || $i == 0)?'#DDDDDD':'#EEEEEE';
            $id[$i] = $row["id"];
            $firstname[$i] = $row["firstname"];
            $location[$i] = $row["location"];
            $comment_date[$i] = $row["comment_date"];
            $comment[$i] = $row["comment"];
            

print"<tr bgcolor='".$color."'>
    <td align='left' width='120'><strong> Name</strong>: $firstname[$i]</td>
    <td align='left' width='120'><strong> Location</strong>: $location[$i]</td>
    <td align='left' width='216'>$comment_date[$i]</td>
  </tr>
  <tr bgcolor='".$color."'>
    <td class='commentBorder' colspan='4'><strong> Comment</strong>: $comment[$i]</td>
  </tr>";
      ++$i;
         }
      }
   }
}

?>

 

-Thanks

The php is above but I have it set to date in the DB like this.

 

`comment_date` date NOT NULL default '0000-00-00',

 

 

The PHP is above which it is my understand the database is suppose to date stamp the entry then you can call that from the database as I am doing but it shows all 0.

 

Here is the form for the comment box

 

<p><form action="comments.php" method="post">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="550">
     <tr>
        <td>
		<fieldset class="fieldset">
			<legend class="legend"><strong>Leave A Comment</strong></legend>
			      <input type="hidden" name="id" value="<? echo $row['id']; ?>" />
			Name: <input type="text" name="firstname">   Location: <input type="text" name="location"> <br>Comment (Max 255 characters): <br><textarea name="comment" rows="5" style="width: 500px;" onkeydown="javascript:limittext(this.form.comment);"></textarea>
<input type="submit" value="Submit"></fieldset></p></td></tr></table></form>

 

-Thanks

 

 

Ok I have the date problem fixed and sorted out.

 

One last problem I am having is when you leave a comment and you hit submit then a white screen comes up and shows "Query was empty". You back page and refresh and the comment is here. Now I would like to put sql on the same page as the form so you see the comment as soon as you enter it but when I put the sql on the same page you get "Query was empty" right off the bat which is why I have it on its own page right now.

 

You can test this out at the bottom of this page

 

http://mesquitechristmas.com/local/display.php?id=2

 

Here is the code for the insert, Any help would be great.

 

<?php

include ('db_connect.php');

$firstname = $_POST['firstname'];
$location = $_POST['location'];
$comment_date = date("m-d-Y");
$comment = $_POST['comment'];
$id = $_POST['id'];

// setup our query
mysql_query("INSERT INTO comments (firstname, location, comment_date, comment, id) VALUES ('$firstname', '$location', '$comment_date', '$comment', '$id')"); 
                $result=mysql_query($query) or die(mysql_error()); // run our query

mysql_close($con);

?>

 

-Thanks

 

 

Change:

<p><form action="comments.php" method="post">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="550">
     <tr>
        <td>
		<fieldset class="fieldset">
			<legend class="legend"><strong>Leave A Comment</strong></legend>
			      <input type="hidden" name="id" value="<? echo $row['id']; ?>" />
			Name: <input type="text" name="firstname">   Location: <input type="text" name="location"> <br>Comment (Max 255 characters): <br><textarea name="comment" rows="5" style="width: 500px;" onkeydown="javascript:limittext(this.form.comment);"></textarea>
<input type="submit" value="Submit"></fieldset></p></td></tr></table></form>

 

To:

<p><form action="comments.php" method="post">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="550">
     <tr>
        <td>
		<fieldset class="fieldset">
			<legend class="legend"><strong>Leave A Comment</strong></legend>
			      <input type="hidden" name="id" value="<? echo $row['id']; ?>" />
			Name: <input type="text" name="firstname">   Location: <input type="text" name="location"> <br>Comment (Max 255 characters): <br><textarea name="comment" rows="5" style="width: 500px;" onkeydown="javascript:limittext(this.form.comment);"></textarea>
<input type="submit" name="submit" value="Submit"></fieldset></p></td></tr></table></form></p>

 


 

Change:


<?php

include ('db_connect.php');

$firstname = $_POST['firstname'];
$location = $_POST['location'];
$comment_date = date("m-d-Y");
$comment = $_POST['comment'];
$id = $_POST['id'];

// setup our query
mysql_query("INSERT INTO comments (firstname, location, comment_date, comment, id) VALUES ('$firstname', '$location', '$comment_date', '$comment', '$id')"); 
                $result=mysql_query($query) or die(mysql_error()); // run our query

mysql_close($con);

?>

 

To:


<?php
if ($_POST['submit']){
include ('db_connect.php');

$firstname = $_POST['firstname'];
$location = $_POST['location'];
$comment_date = date("m-d-Y");
$comment = $_POST['comment'];
$id = $_POST['id'];

// setup our query
mysql_query("INSERT INTO comments (firstname, location, comment_date, comment, id) VALUES ('$firstname', '$location', '$comment_date', '$comment', '$id')"); 
                $result=mysql_query($query) or die(mysql_error()); // run our query

mysql_close($con);
}
?>

 


 

If you want them together:


<?php
if ($_POST['submit']){
include ('db_connect.php');

$firstname = $_POST['firstname'];
$location = $_POST['location'];
$comment_date = date("m-d-Y");
$comment = $_POST['comment'];
$id = $_POST['id'];

// setup our query
mysql_query("INSERT INTO comments (firstname, location, comment_date, comment, id) VALUES ('$firstname', '$location', '$comment_date', '$comment', '$id')"); 
                $result=mysql_query($query) or die(mysql_error()); // run our query

mysql_close($con);
}
else {
?>
<p><form method="post">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="550">
     <tr>
        <td>
		<fieldset class="fieldset">
			<legend class="legend"><strong>Leave A Comment</strong></legend>
			      <input type="hidden" name="id" value="<? echo $row['id']; ?>" />
			Name: <input type="text" name="firstname">   Location: <input type="text" name="location"> <br>Comment (Max 255 characters): <br><textarea name="comment" rows="5" style="width: 500px;" onkeydown="javascript:limittext(this.form.comment);"></textarea>
<input type="submit" name="submit" value="Submit"></fieldset></p></td></tr></table></form></p>
<?php
}
?>

 

Feel free to edit them of course.

change this...

 

<?php

include ('db_connect.php');

$firstname = $_POST['firstname'];
$location = $_POST['location'];
$comment_date = date("m-d-Y");
$comment = $_POST['comment'];
$id = $_POST['id'];

// setup our query
mysql_query("INSERT INTO comments (firstname, location, comment_date, comment, id) VALUES ('$firstname', '$location', '$comment_date', '$comment', '$id')"); 
                $result=mysql_query($query) or die(mysql_error()); // run our query

mysql_close($con);

?>

 

to this ...

<?php

include ('db_connect.php');

$firstname = $_POST['firstname'];
$location = $_POST['location'];
$comment_date = date("m-d-Y");
$comment = $_POST['comment'];
$id = $_POST['id'];

// setup our query
$query ="INSERT INTO comments (firstname, location, comment_date, comment, id) VALUES ('$firstname', '$location', '$comment_date', '$comment', '$id')"; 
                $result=mysql_query($query) or die(mysql_error()); // run our query

mysql_close($con);

?>

 

what you had was, $result=mysql_query($query) but nothing was assigned to $query so it was coming up as an empty result :)

or if u want just leave it as this....

 

<?php

include ('db_connect.php');

$firstname = $_POST['firstname'];
$location = $_POST['location'];
$comment_date = date("m-d-Y");
$comment = $_POST['comment'];
$id = $_POST['id'];

// setup our query
mysql_query("INSERT INTO comments (firstname, location, comment_date, comment, id) VALUES ('$firstname', '$location', '$comment_date', '$comment', '$id')") or die(mysql_error()); // run our query

mysql_close($con);

?>

I have almost got this all sorted the one last thing I need sorted is I noticed that if you refreshed the page the last comment would be added. So you could fresh over and over again and a comment would be added. So I did this.

 

if ($_POST['submit']){
$firstname = $_POST['firstname'];
$location = $_POST['location'];
$comment_date = date("m-d-Y");
$comment = $_POST['comment'];
$id = $_POST['id'];

// setup our query
$query ="INSERT INTO comments (firstname, location, comment_date, comment, id) VALUES ('$firstname', '$location', '$comment_date', '$comment', '$id')"; 
                $result=mysql_query($query) or die(mysql_error()); // run our query

}

 

 

Well that stops the refresh issue but now nothing is inserted or displayed. Did I miss something else?

 

-Thanks

 

Ok I have reworked this code but still not getting it to insert anymore and I am not getting any errors. This use to work until I added if ($_POST['submit']){ I only added that because when you refreshed the page it would insert the last comment over and over again as many times as you refreshed the page. So when I put that in it stopped but now it doesn't insert or post the comment at all.

 

What am I missing or doing wrong?

 

<?php

// start code for leaving comments

if ($_POST['submit']) {

$firstname = $_POST['firstname'];
$location = $_POST['location'];
$comment_date = date("m-d-Y");
$comment = $_POST['comment'];
$id = $_POST['id'];

// setup our query
$query ="INSERT INTO comments (firstname, location, comment_date, comment, id) VALUES ('$firstname', '$location', '$comment_date', '$comment', '$id')";
                $result=mysql_query($query) or die(mysql_error()); // run our query

} else {

                                print"<p><form method='POST' action='display.php?id=$id'>
<table align='center' border='0' cellpadding='0' cellspacing='0' width='550'>
     <tr>
        <td><fieldset class='fieldset'>
			<legend class='legend'><strong>Leave A Comment</strong></legend>
			      <input type='hidden' name='id' value='$id' />
			Name: <input type='text' name='firstname' value='Anonymous' onClick='this.value='''> Location: <input type='text' name='location' value='Anonymous' onClick='this.value=''''> <br>Comment (Max 255 characters): <br><textarea name='comment' rows='5' style='width: 500px;' onkeydown='javascript:limittext(this.form.comment);'></textarea>

<input type='submit' value='Submit'> <input type='reset' value='Reset'></fieldset></p></td></tr></table></form>";

}

?>


-Thanks

That doesn't really work ether. I think I have tried that before. It will allow it to insert but then you or a visitor has to refresh the page to see their comment. Then they can refresh the page over and over again inserting the same comment, then the comment box disappears after you hit submit also.

 

Any other ideas or suggestions. This is a pretty common function, I wouldn't think this should be that hard should it?

 

-Thanks

Ok were back to square one since that last code jacked up my page so I had to revert back to the last working code. This code inserts the comments into that database and displays them on the page but vistior can refresh the page over and over again inserting the last comment made.

 

Here is the full code for the entire file. URL is on page 1 of this thread

 

<?php

// connect to database

include ('db_connect.php');

// start code for showing display information

if (isset($_GET['id'])) {
  $id = mysql_real_escape_string($_GET['id']);
  $sql = "SELECT * FROM users WHERE id = $id;";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_array($result);
      $id = $row["id"];
      $displayname = $row["displayname"];
      $displaytype = $row["displaytype"];
      $address = $row["address"];
      $city = $row["city"];
      $state = $row["state"];
      $postal = $row["postal"];
      $country = $row["country"];
      $rating = $row['rating'];
      $votes = $row['votes'];
      $imgurl = $row['imgurl'];
    } else {
      die("No user found");
    }
  } else {
    die(mysql_error());
  }
}

// end code for showing display information

// start code for displaying comments

function show_data(){
   $id = array();
   $firstname = array();
   $location = array();
   $comment_date = array();
   $comment = array();
   $id = mysql_real_escape_string($_GET['id']);
   $i=0;
   if(isset($id)) {
      $sql = "SELECT * FROM comments WHERE id = $id;";
      if($result = mysql_query($sql)) {
        $i = 0;
         while ($row = mysql_fetch_array ($result)) {
            $color = ($i%2== 0 || $i == 0)?'#DDDDDD':'#EEEEEE';
            $id[$i] = $row["id"];
            $firstname[$i] = $row["firstname"];
            $location[$i] = $row["location"];
            $comment_date[$i] = $row["comment_date"];
            $comment[$i] = $row["comment"];


print"<tr bgcolor='".$color."'>
    <td align='left' width='120'><strong> Name</strong>: $firstname[$i]</td>
    <td align='left' width='120'><strong> Location</strong>: $location[$i]</td>
    <td align='left' width='216'><strong> Date</strong>: $comment_date[$i]</td>
  </tr>
  <tr bgcolor='".$color."'>
    <td class='commentBorder' colspan='4'><strong> Comment</strong>: $comment[$i]</td>
  </tr>";
      ++$i;
         }
      }
   }
}

// end code for displaying comments

// Start code for inserting comments

$firstname = $_POST['firstname'];
$location = $_POST['location'];
$comment_date = date("m-d-Y");
$comment = $_POST['comment'];
$id = $_POST['id'];

// setup our query
$query ="INSERT INTO comments (firstname, location, comment_date, comment, id) VALUES ('$firstname', '$location', '$comment_date', '$comment', '$id')";
                $result=mysql_query($query) or die(mysql_error()); // run our query

// end code for inserting comments

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Mesquite Texas Country Christmas" />
<meta name="keywords" content="Mesquite, Texas, Country Christmas" />
<meta name="author" content="NA" />
<link rel="stylesheet" type="text/css" href="/stylesheet.css" media="screen" title="FBC" />
<script type="text/javascript" src="drop_down.js"></script>
<title>A Mesquite Country Christmas - <? echo $row['displayname']; ?></title>
</head>
<body>

<div id="wrap">

<a href="/index.html">
<img id="frontphoto" src="/images/header.png" width="760" height="237" alt="Mesquite Country Christmas" border="0"></a>

<div id="menu">

<h2 class="hide">Menu:</h2>

<ul id="avmenu">
<li><a href="/index.html">Home</a></li>
<li><a href="/christmasstory.html">The Christmas Story</a></li>
<li><a href="/directions.html">Directions</a></li>
<li><a href="#">Information</a><ul>
      <li><a href="/information.html">Display Facts & Info</a></li>
      <li><a href="/faq.html">FAQ</a></li>
      <li><a href="/playlist.html">2008 Playlist</a></li>
      <li><a href="#">Christmas History</a></li>
  </ul></li>
<li><a href="#">Photos</a>
  <ul>
      <li><a href="/2007photos.html">2007</a></li>
  </ul></li>
<li><a href="#">Videos</a>
  <ul>
      <li><a href="/2007videos.html">2007</a></li>
  </ul></li>
<li><a href="/guestbook.php">Guestbook</a></li>
<li><a href="/webcam.html">Web Cam</a></li>
<li><a href="/webradio.html">Internet Radio</a></li>
<li><a href="http://www.noradsanta.org/" TARGET="_blank">Track Santa</a></li>
<li><a href="/projects.html">Projects & How Tos</a></li>
<li><a href="/links.html">Links</a></li>
<li><a href="/contact_us.html">Contact Us</a></li>
</ul>

<center><a href="http://www.toysfortots.org/" TARGET="_blank"><img src="/images/toys_for_tots.jpg" border="0" width="110" height="153" vspace="10"></a></center>

<center><a href="http://christmas.bronners.com/2007/house/534.html"><img src="http://christmas.bronners.com/voteforme/vote.jpg" border="0" width="110" height="153" alt="christmas decorations" vspace="10"></a></center>

</div>

<div id="content">

<div class="fadebox">

<h2><? echo $row['displayname']; ?></h2>

<hr />

<table align="center" border="0" cellpadding="0" cellspacing="0" width="550">
     <tr>
        <td align="center"><img src="<? echo $row['imgurl']; ?>" class="border" height="345" width="460"></td>
     </tr>
      <tr>
<td>
<table align="center"><tr><td>Rated 0 from 0 people | <b>Rate This</b>: <a href="displays.php?id=<?php echo $row['id']; ?>&rate=1">1</a> | <a href="displays.php?id=<?php echo $row['id']; ?>&rate=2">2</a> | <a href="displays.php?id=<?php echo $row['id']; ?>&rate=3">3</a> | <a href="displays.php?id=<?php echo $row['id']; ?>&rate=4">4</a> | <a href="displays.php?id=<?php echo $row['id']; ?>&rate=5">5</a>
</td><td> </td></tr></table></td></tr></table>

<table align="center" style="border-top: 1px solid #990000;" border="0" cellpadding="4" cellspacing="0" width="550">

<p><span class="title">About this Display...</span>
<tr><td class="bg2" width="100"><b>Display Name</b>:</td><td class="bg2"><? echo $row['displayname']; ?></td></tr><tr><td class="bg1"><b>Display Type</b>:</td><td class="bg1"><? echo $row['displaytype']; ?></td></tr><tr><td class="bg2"><b>Description</b>:</td><td class="bg2"><? echo $row['description']; ?></td></tr><tr><td class="bg1"><b>Address</b>:</td><td class="bg1"><? echo $row['address']; ?><br><? echo $row['city']. "," . $row['state']. " " . $row['postal']; ?><br><? echo $row['country']; ?><br><a href="http://www.mapquest.com/maps/map.adp?searchtype=address&formtype=search&countryid=US&addtohistory=&country=US&address=<? echo $row['address']; ?>&city=<? echo $row['city']; ?>&zipcode=<? echo $row['postal']; ?>&historyid=&submit=Get+Map" target="_NEW"><b>Get Directions</b></a></td></tr><tr><td class="bg2"><b>Website</b>:</td><td class="bg2"><a href="<? echo $row['website']; ?>" target="_blank"><? echo $row['website']; ?></a></td></tr><tr><td class="bg1"><b>Sponsor Links</b>:</td><td class="bg1">
<script type="text/javascript"><!--
google_ad_client = "pub-8048181801684156";
//displays
google_ad_slot = "4239948836";
google_ad_width = 234;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
	</td></tr></table></p>

<p><span class="title">Comments...</span><table align="center" style='border-top: 1px solid #990000' border="0" cellpadding="4" cellspacing="0" width="550">
  <?php show_data(); ?>
</table></p>

<p><form method="POST" action="display.php?id=<?php echo $row['id']; ?>">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="550">
     <tr>
        <td>
		<fieldset class="fieldset">
			<legend class="legend"><strong>Leave A Comment</strong></legend>
			      <input type="hidden" name="id" value="<? echo $row['id']; ?>" />
			Name: <input type="text" name="firstname" value="Anonymous" onClick="this.value=''">   Location: <input type="text" name="location" value="Anonymous" onClick="this.value=''"> <br>Comment (Max 255 characters): <br><textarea name="comment" rows="5" style="width: 500px;" onkeydown="javascript:limittext(this.form.comment);"></textarea>
<input type="submit" value="Submit"> <input type="reset" value="Reset"></fieldset></p></td></tr></table></form>


       </div>

   </div>

</div>

<div id="footer">
© 2007 Mesquite Country Christmas<br />
This site best viewed with <a href="http://www.spreadfirefox.com/node&id=0&t=219"><img border="0" alt="Firefox 2" title="Firefox 2" src="http://sfx-images.mozilla.org/affiliates/Buttons/firefox2/ff2b80x15.gif"/></a>

<br />
<br />

<script type="text/javascript"><!--
google_ad_client = "pub-8048181801684156";
//468x60, created 1/8/08
google_ad_slot = "0360766123";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>


</div>

</div>
</body>
</html>

 

-Thanks

 

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.