Boxerman Posted October 26, 2011 Share Posted October 26, 2011 Hi all, I've got a news table as such; `id` int(255) NOT NULL auto_increment, `title` mediumtext NOT NULL, `postedby` mediumtext NOT NULL, `text` mediumtext NOT NULL, `brief` mediumtext NOT NULL, `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP, `image` varchar(255) NOT NULL default '', PRIMARY KEY (`id`) I was wondering in PHP how would i be able to add comments to the news? i.e Form: Name, IP (hidden), comments I'll add the name, ip and comments to the database But how do i go around adding that into php? Quote Link to comment https://forums.phpfreaks.com/topic/249837-adding-comments/ Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 26, 2011 Share Posted October 26, 2011 firstly, get the id of the new post when the user clicks comment. then the rest should be simple enough. Quote Link to comment https://forums.phpfreaks.com/topic/249837-adding-comments/#findComment-1282379 Share on other sites More sharing options...
Boxerman Posted October 26, 2011 Author Share Posted October 26, 2011 Hi, at the moment im using this: /* create query */ $query = "SELECT * FROM news ORDER BY id DESC"; /* execute the query */ $result = mysql_query($query); while($row=mysql_fetch_array($result)) { $image = $row['image']; $title = $row['title']; $posted_by = $row['postedby']; $brief = shorten_string($row['text'], $N); ?><p> <table width="461" height="113" border=0"> <tr> <th width="123" scope="col"><a href="news.php?id=<?PHP echo $row['id']; ?>"><img src="<?PHP echo $image; ?>" width="111" height="103" /></a></th> <th width="322" scope="col"><b><font size="5"><?PHP echo $title; ?></font></b> <br> <font size="2">Posted By: <?PHP echo $posted_by; ?></font> <br><br> </p><p><?PHP echo $brief; ?></p><p><b> <a href="news.php?id=<?PHP echo $row['id']; ?>">Read More</a></b></p><br></th> </tr> </table> <?PHP } ?> how would i add ontop that? Quote Link to comment https://forums.phpfreaks.com/topic/249837-adding-comments/#findComment-1282380 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 26, 2011 Share Posted October 26, 2011 create a new file called comments.php then create a link from the main page to the comments page with the new id. <? /* create query */ $query = "SELECT * FROM news ORDER BY id DESC"; /* execute the query */ $result = mysql_query($query); while($row=mysql_fetch_array($result)) { $image = $row['image']; $title = $row['title']; $posted_by = $row['postedby']; $brief = shorten_string($row['text'], $N); ?><p> <table width="461" height="113" border=0"> <tr> <th width="123" scope="col"><a href="news.php?id=<?PHP echo $row['id']; ?>"><img src="<?PHP echo $image; ?>" width="111" height="103" /></a></th> <th width="322" scope="col"><b><font size="5"><?PHP echo $title; ?></font></b> <br> <font size="2">Posted By: <?PHP echo $posted_by; ?></font> <br><br> </p><p><?PHP echo $brief; ?></p><p><b> <a href="news.php?id=<?PHP echo $row['id']; ?>">Read More</a></b></p><p><b> <a href="comment.php?id=<?PHP echo $row['id']; ?>">comment</a></b></p><br></th> </tr> </table> <?PHP } ?> Quote Link to comment https://forums.phpfreaks.com/topic/249837-adding-comments/#findComment-1282386 Share on other sites More sharing options...
Boxerman Posted October 26, 2011 Author Share Posted October 26, 2011 Ok i'm starting to understand, but is there no way i can add the comments bit onto the news page itself? I.e News display here: Title: Story: Leave a comment: Name: textfield Comment: textfield Submit | Reset Then post it to the database? and it automatically refreshes the page and displays comment? If so, how on gods earth would i attempt this? Cheers pal! Quote Link to comment https://forums.phpfreaks.com/topic/249837-adding-comments/#findComment-1282387 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 26, 2011 Share Posted October 26, 2011 yes you can, you just did it? Quote Link to comment https://forums.phpfreaks.com/topic/249837-adding-comments/#findComment-1282388 Share on other sites More sharing options...
Boxerman Posted October 26, 2011 Author Share Posted October 26, 2011 So in a nut shell i need to add the fields to database and the forms to the news page... how do i add it to database in the same page and get it to refresh? Quote Link to comment https://forums.phpfreaks.com/topic/249837-adding-comments/#findComment-1282392 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 26, 2011 Share Posted October 26, 2011 submit the form? Quote Link to comment https://forums.phpfreaks.com/topic/249837-adding-comments/#findComment-1282393 Share on other sites More sharing options...
Boxerman Posted October 26, 2011 Author Share Posted October 26, 2011 What i'm tyring to ask is, when i click submit it needs to rule the INSERT job... how do i do it? Quote Link to comment https://forums.phpfreaks.com/topic/249837-adding-comments/#findComment-1282400 Share on other sites More sharing options...
jcbones Posted October 27, 2011 Share Posted October 27, 2011 You need a properly coded form with all of the information you wish to collect from the user, and the action set to the processing file. Processing file checks the form parameters to make sure they are what you expect them to be. Sanitizes the inputs so they will contain no database injection. Inserts the data into a NEW database table, linking the comment to the news article by the news articles ID (primary key). Then re-directs back to the news story page, which then calls the comments with a JOIN query, and displays them however you want to. Quote Link to comment https://forums.phpfreaks.com/topic/249837-adding-comments/#findComment-1282563 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.