zhangy Posted December 14, 2008 Share Posted December 14, 2008 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 More sharing options...
corillo181 Posted December 14, 2008 Share Posted December 14, 2008 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 More sharing options...
zhangy Posted December 14, 2008 Author Share Posted December 14, 2008 Ok sounds good, but how to get the id of the topic into the comments table? Link to comment https://forums.phpfreaks.com/topic/136920-comments/#findComment-715089 Share on other sites More sharing options...
laPistola Posted December 14, 2008 Share Posted December 14, 2008 when you write query add it in there like INSERT INTO comment_table (tid,comment) VALUES ('$topic_id','$comment'); thats assuming your id column is auto increasing Link to comment https://forums.phpfreaks.com/topic/136920-comments/#findComment-715093 Share on other sites More sharing options...
zhangy Posted December 14, 2008 Author Share Posted December 14, 2008 ok, and how to give $topic_id the value of the current topic? Link to comment https://forums.phpfreaks.com/topic/136920-comments/#findComment-715104 Share on other sites More sharing options...
laPistola Posted December 14, 2008 Share Posted December 14, 2008 well i take it $id is the current topic id already?? if so INSERT INTO comment_table (tid,comment) VALUES ('$id','$comment'); Link to comment https://forums.phpfreaks.com/topic/136920-comments/#findComment-715196 Share on other sites More sharing options...
zhangy Posted December 14, 2008 Author Share Posted December 14, 2008 Sorry, I dont understand. How can $id know what the id of the topic is. I want to select data from comments table based upon the id of data in the topics table? Link to comment https://forums.phpfreaks.com/topic/136920-comments/#findComment-715432 Share on other sites More sharing options...
laPistola Posted December 15, 2008 Share Posted December 15, 2008 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 More sharing options...
zhangy Posted December 16, 2008 Author Share Posted December 16, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.