Jump to content

comments


zhangy

Recommended Posts

Hi,

 

I'm trying to create a comments section.

This following code is selecting and displaying the information that the comments would be about. I need the comments sql selection to be based on the id of the topic thats being commented on. Anyone know how I can do so?

 

 

<?php

require_once('loadGoods.php');

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Woops! YOU broke the system! Quick! Refresh the page!");

if (!mysql_select_db($database))
    die("Can't... se... select... data... base.");

$id = (int) $_GET['id']; 

$sql = "SELECT * FROM $table WHERE submission_id=$id";
$result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql);

if(mysql_num_rows($result) == 1){
$row = mysql_fetch_array($result);

echo "<table id=table1>";
	echo "<tr><td><strong>";
	echo $row['col_1'];
	echo "</strong></td></tr>";
	echo "<tr><td>";
	echo $row['col_2'];
	echo "</td></tr>";
	echo "<tr><td>";
	echo date("l M dS, Y", $row['submission_date']);
	echo "</td></tr>";
	echo "<tr><td>".(nl2br($row['col_5']))."</td></tr>";
	echo "</table>";

}else{
echo 'Oops! Something went wrong!';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/136920-comments/
Share on other sites

what you need to do is create a table that is name comment or sometihng of that kind, and have a id tha tlink back to the topic or section you want to be comment so when somoene summit a comment it could be like

 

comment table

id | topic_id | comment |

 

and then the query would be like

 

select * from comment_table where topic_id = $topic_id

Link to comment
https://forums.phpfreaks.com/topic/136920-comments/#findComment-715064
Share on other sites

i did say i assumed and this bit of your code made me think so

 

$id = (int) $_GET['id'];

 

you havent told us a load or shown us alot so we can only guess that what that line is doing is grassing an ID of some sort from the URL so as we havent been told differently we assume thats your topic id. If the topic id isn't in the url or that VAR that you need to findthe topic id, probably using a MySQL query

 

maybe more code would help us understand where your at.

 

is this comment section on the same page as the topic?? if so the topic id will be in that page somewhere

Link to comment
https://forums.phpfreaks.com/topic/136920-comments/#findComment-716129
Share on other sites

Please forgive the obscurity.

The comment form/section is on the same page as the topic.

Now, with the below code I get this error message:

Duplicate entry '0' for key 1

 

Comments processing code:

<?php 
if ( isset($_POST['submit']) ) {

require_once('Load.php');

$connect = mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($database);

$comments = check_input($_POST['message'], "Please enter your comment.");
   	$ip = $_SERVER['REMOTE_ADDR'];

   $insert = mysql_query("INSERT INTO $table (topic_id, comments, submission_date, ip_address) VALUES ('$id', '$comments', ".time().", '$ip')") or die(mysql_error()); 
}
header('Location: ../somehow back to topic page.php');
exit();
?>

 

 

Topic:

<?php

   require_once('loadGoods.php');

   if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Woops! YOU broke the system! Quick! Refresh the page!");

   if (!mysql_select_db($database))
    die("Can't... se... select... data... base.");
   
   $id = (int) $_GET['id']; 

   $sql = "SELECT * FROM $table WHERE submission_id=$id";
   $result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql);
   
   if(mysql_num_rows($result) == 1){
   $row = mysql_fetch_array($result);

   echo "<table id=table1>";
      echo "<tr><td><strong>";
      echo $row['col_1'];
      echo "</strong></td></tr>";
      echo "<tr><td>";
      echo $row['col_2'];
      echo "</td></tr>";
      echo "<tr><td>";
      echo date("l M dS, Y", $row['submission_date']);
      echo "</td></tr>";
      echo "<tr><td>".(nl2br($row['col_5']))."</td></tr>";
      echo "</table>";

   }else{
   echo 'Oops! Something went wrong!';
   }
?>

Link to comment
https://forums.phpfreaks.com/topic/136920-comments/#findComment-716481
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.