faqts : Computers : Programming : Languages : PHP : Common Problems : Example Scripts : Product Comments

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

11 of 12 people (92%) answered Yes
Recently 6 of 7 people (86%) answered Yes

Entry

How can I create a script that allows people to comment on products?

May 16th, 2000 22:19
Nathan Wallace, Richard Lynch


Untested code off the top of my head:

CREATE table comments(
  productid int4,
  comment text
);

product.php3
<?php
  $productid = 42;
  $product_name = 'widget';
  echo "<A HREF=comment.php3?productid=$productid>Comments</A> about
$product_name<BR>\n";
  $comments = mysql_query("select comment from comments where productid
= $productid") or die(mysql_error());
  while (list($comment) = mysql_fetch_row($comments)){
    echo $comments, "<HR>";
  }
?>

comment.php3
<?php
  if (isset($submit)){
    mysql_query("INSERT into comments(productid, comment)
values($productid, '$comments')") or die(mysql_error());
    echo "Thank you for your comment.<BR>\n";
  }
?>
<FORM ACTION=comment.php3 METHOD=POST>
  <TEXTAREA NAME=comments WRAP=VIRTUAL></TEXTAREA><BR>
  <INPUT TYPE=SUBMIT NAME=submit>
</FORM>