PHP - Comment Page (forum Style)
Similar TutorialsSo I wanted to create stickers on my forum where people type in say ":Blobby-Tired" and then instead of the comment showing ":Blobby-Tired" it shows an html image tag displaying a picture of Blobby in the comment box So I created the following page CommentUpload.php However my sql fails to insert it despite the fact when I "echo $new" it shows the image on the page correctly And it can't be my sql insert code as if I change $new variable for "$BodyText" int he SQL query it it inserts ":Blobby-Tired" OK?
<?php include 'dbconnect.php'; session_start(); if(isset($_POST["submit"]) && !empty($_POST["CommentText"])){ $id = intval($_SESSION['id']); echo $_SESSION['id'] . '<p> </p>' ; $BodyText = $conn -> real_escape_string($_POST['CommentText']) ; $User = $_SESSION['username']; //Replace flairs with <img> tags /*not working currently) */ $new = str_replace(":Blobby-Tired","<img src='flairs/Blobby-Tired.jpg'> </img>","'$BodyText'"); echo "$new"; /************************/ $sql = "INSERT INTO Posts (User, CommentText, IdOfThread) VALUES ('$User','$new','$id')"; if (mysqli_query($conn, $sql)) { echo "New record has been added successfully !"; } else { echo "Error: " . $sql . ":-" . mysqli_error($conn); } mysqli_close($conn); } ?> Edited April 28 by Fishcakes CREATE TABLE posts ( postId INT(11) NOT NULL UNIQUE AUTO_INCREMENT, title VARCHAR(255) NOT NULL, author VARCHAR(24) NOT NULL, description TEXT NOT NULL, createdAt TIMESTAMP, PRIMARY KEY (postId) ); CREATE TABLE comments( commentId INT(11) NOT NULL UNIQUE AUTO_INCREMENT, comment TEXT NOT NULL, postId INT(11), userId INT(11), createdAt TIMESTAMP, PRIMARY KEY (commentId), FOREIGN KEY (userId) REFERENCES users(userId), FOREIGN KEY (postId) REFERENCES posts(postId) ); CREATE TABLE replies ( repId INT(11) NOT NULL UNIQUE AUTO_INCREMENT, reply TEXT NOT NULL, userId INT(11), commentId INT(11), createdAt TIMESTAMP, PRIMARY KEY (repId), FOREIGN KEY (userId) REFERENCES users(userId), FOREIGN KEY (commentId) REFERENCES comments(commentId) ); CREATE TABLE users ( userId INT(11) NOT NULL UNIQUE AUTO_INCREMENT, userName VARCHAR(100) NOT NULL,, email VARCHAR(100) NOT NULL, PRIMARY KEY (userId) ); how to retrive userName,comment, and createdAt from users and comments table while I have used userId as a Foreign key on the comment table if it isn't correct, correct me please Hi all i hope to find cure to my problems here is code of comment link <a href='/blogs/?page=comment&id=".$row['id']."'>Comment</a> here is code of whole comment page http://paste.php.lv/4c30d2286cdd7334d856ce966abc618f?lang=php And here is Pictures what's is going on on website.... Here is my main issue there are 3 comment's and they all well make such mess .... and if there aren't any comment's have such issue .... Hi guyz, I am looking to create a dymanic comment box for my website where already logged in users can comment on webpages. It is quite a challenge getting to dynamically display only the comments a particular user posted on a web page. What I mean is I would like to be able to click on a page, see only the comments logged users posted on that page, and then post my own comment on that page. At the moment I created a comment box for my webpages but the issue is my posted comments on one particular page is displaying on all web pages with is comment system..This is the problem and it is very difficult to find helpful material hopefully you guyz can come to the rescue. Please guys any help or suggestions are more than welcome. Thanks Kdiamonds Ok. Thought I'd be able to do this myself but ran into a couple of snags. This is the template page for the image thumbnails. WHen a thumbnail is clicked it is dragged into this template. I put the code for the comments on the page but it's just not working properly. When I post comment the comment posts but the picture turns into my BAD PHOTO ID error message. Have some ideas on whats going but it's getting kind of messy. If you need to see the pages from the comment system I will post. Thanks phpeople. Where's fugix? Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>What do you know about.......</title> <style type="text/css"> #What { text-align: center; } </style> </head> <body> <div id="What"><img src="images/what.png" width="477" height="36" alt="do you know something i don't" /></div> <?php $hostname='fd.db.fdffd.d.com'; $username='d'; $password='d!'; $dbname='d'; $usertable='d'; mysql_connect('yd.com', 'yd9', 'Ad!') or die(mysql_error()); mysql_select_db("yd9") or die(mysql_error()); $id = (int)$_GET['id']; if( $id > 0 ) { $result = mysql_query("SELECT `name`, `photopath`, `state` FROM `photo` WHERE `id` = $id") or die(mysql_error()); $row = mysql_fetch_array( $result ); echo " <img src=\"/{$row['photopath']}\" height=\"400px\" width=\"400px\" />". "<br /> "; echo $row['name']. "\n". "<br/>"; echo $row['state']. "\n"; } else { echo 'BAD PHOTO ID'; } ?> <?php $hostname='durce.com'; $username='yod9'; $password='Ad1!'; $dbname='yd'; $usertable='d'; mysql_connect('yd, 'yd9', 'Ad!') or die(mysql_error()); mysql_select_db("yd") or die(mysql_error()); $name=$_POST['name']; $comment=$_POST['comment']; $submit=$_POST['submit']; if($submit) { if($name&&$comment) { $query=mysql_query("INSERT INTO comment (id,name,comment) VALUES ('','$name','$comment')"); } else { echo "Please fill out all the fields."; } } ?> <body> <div id="comments"> <form action="image_show.php" method="POST"> <label>Name: </label><br /><input type="text" name="name" input id="name"value="<?php echo "$name" ?>" /><br /><br /> <label>Comment: </label><br /><input type="text" input id="comment"/><textarea name="comment" "cols="25" rows="7"></textarea><br /><br /><br /> <input type="submit" name="submit" value="Comment" /><br /> </form></div> <hr width="1100px" size="5px" /> </body> <?php $query=mysql_query("SELECT * FROM comment ORDER BY id DESC"); while($rows=mysql_fetch_assoc($query)) { $id=$rows['id']; $name=$rows['name']; $comment=$rows['comment']; $linkdel="<a href=\"delete.php?id=" . $rows['id'] . "\">Delete User</a>"; echo '<font color="red">Name:</font> ' . $name . '<br />' . '<br />' . '<font color="red">Comments:</font> ' . '<br />' . $comment . ' ' . ' ' . ' ' . ' ' . $linkdel . '<br />' . '<br />' . '<hr size="5px" width="500px" color="blue" />' ; } ?> </html> </body> </html> I am looking to add a comment area to pages that are created with data that is pulled off of a database. I am looking for something like the forums on this site, but not as complex. I was thinking about attempting to use a text file to hold the comments for each ID number that is in my database and then displaying those comments when the ID number is called. I'm not sure how I would go about making a function that would search if there was a text file for that ID number and if not to create one. I'm not even sure if this is the best way to go about it but I know if I put all the comments into a database it will get rather large rather quickly. Any pointers and good tutorials would be welcome. Sorry if it sounds as if this post is rambling, I think I need to step away from the computer for a bit. Thanks SOLVED: I'm having trouble with an SQL query via php in building my Viewthread.php file So on the main page you have a list of the Threads and the link I created is like the below <h2><a href='viewthread.php?id='{$row['id']}'> {$row['Title']} </a></h2> Post {$row['id']}<br> If I set the query to an ID that exists the page loads fine $query = mysqli_query($conn, "SELECT * FROM Threads where id='156'") however if I set the id to the GET command the page doesn't load (which should've got the id) $query = mysqli_query($conn, "SELECT * FROM Threads where id=$_GET['id']") I tried casting this to a variable first too $Number = "$_GET['id']" $query = mysqli_query($conn, "SELECT * FROM Threads where id='$Number'") Edited April 2 by Fishcakes This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=355013.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=322815.0 I am pretty new to PHP and am trying to create a simple (so I assumed) page to takes data from one html page(works fine) and updates a MYSQL Database. I am getting no error message, but the connect string down to the end of the body section is showing up as plain text in my browser window. I do not know how to correct this. I have tried using two different types of connect strings and have verified my names from the HTML page are the same as listed within the php page. Suggestions on what I need to look for to correct would be great. I have looked online, but so far all I am getting is how to connect, or how to create a comment, so I thought I would try here. Thank you for any assistance I may get!! - Amy - Code: [Select] <body><font color="006600"> <div style="background-color:#f9f9dd;"> <fieldset> <h1>Asset Entry Results</h1> <?php // create short variable names $tag=$_POST['tag']; $serial=$_POST['serial']; $category=$_POST['category']; $status=$_POST['status']; $branch=$_POST['branch']; $comments=$_POST['comments']; if (!$tag || !$serial || !$category || !$status || !$branch) { echo "You have not entered all the required details.<br />" ."Please go back and try again."; exit; } if (!get_magic_quotes_gpc()) { $tag = addslashes($tag); $serial = addslashes($serial); $category = addslashes($category); $status = addslashes($status); $branch = addslashes($branch); $comments = addslashes($comments); } //@ $db = new mysqli('localhost', 'id', 'pw', 'inventory'); $db = DBI->connect("dbi:mysql:inventory:localhost","id","pw") or die("couldnt connect to database"); $query = "insert into assets values ('".$serial."', '".$tag."', '".$branch."', '".$status."', '".$category."', '".$comments."')"; $result = $db->query($query); if ($result) { echo $db->affected_rows." asset inserted into Inventory."; } else { echo "An error has occurred. The item was not added."; } $db->close(); ?> </fieldset> </div> </body> Hello guys, I am a beginner in PHP, so please be gentle. I am building this website for a friend of mine. This website is made up by HTML/CSS. i.e., I have a page made with HTML & CSS. there are a few menus on the page. say Home, About Us, Contact Us, etc., Now I want to separate the page elements (like header/footer) from the page so as its easy to work on. the source is something like this Code: [Select] <HTML> <HEAD> <TITLE> My Page </TITLE> <LINK REL="STYLESHEET" HREF="./mystyle.css" TYPE="text/css" /> </HEAD> <BODY> <DIV ID="wrapper"> <DIV ID="header"> <DIV ID="logo"> <UL ID="menu"> <LI> <A HREF="./index.php"><SPAN>Home</SPAN></A> </LI> <LI> <A HREF="./login.php"><SPAN>Login</SPAN></A> </LI> <LI> <A HREF="./contactus.php"><SPAN>Contact Us</SPAN></A> </LI> </UL> <DIV ID="date"> <?php echo Date("d M Y");?> </DIV> </DIV> </DIV> <DIV ID="bodywrapper"> <DIV ID="main"> <DIV ID="content"> THIS IS A TEST! </DIV> </DIV> </DIV> <DIV ID ="clearfooter"></DIV> </DIV> <DIV ID="footer"> © <?php echo date("Y");?> </DIV> </BODY> </HTML> now I will have my content in the DIV called 'content'. now I have separated this one page into three different pages. like this: header.php Code: [Select] <HTML> <HEAD> <TITLE> My Page </TITLE> <LINK REL="STYLESHEET" HREF="./mystyle.css" TYPE="text/css" /> </HEAD> <BODY> <DIV ID="wrapper"> <DIV ID="header"> <DIV ID="logo"> <UL ID="menu"> <LI> <A HREF="./index.php"><SPAN>Home</SPAN></A> </LI> <LI> <A HREF="./login.php"><SPAN>Login</SPAN></A> </LI> <LI> <A HREF="./contactus.php"><SPAN>Contact Us</SPAN></A> </LI> </UL> <DIV ID="date"> <?php echo Date("d M Y");?> </DIV> </DIV> </DIV> footer.php Code: [Select] <DIV ID ="clearfooter"></DIV> </DIV> <DIV ID="footer"> © <?php echo date("Y");?> </DIV> </BODY> </HTML> and index.php Code: [Select] <?php include_once('header.php');?> <DIV ID="bodywrapper"> <DIV ID="main"> <DIV ID="content"> THIS IS A TEST </DIV> </DIV> </DIV> <?php include_once('footer.php');?> Is this the right way to code? I am worried because I have to keep in mind the safety of the website as I have to include a basic login module to this. is there any other style to write this? please do let me know.. thanks Hi Everyone, I have this small bit of PHP that when you are on a certain page it will add a class called selected Code: [Select] <?php $currentPage = basename($_SERVER['SCRIPT_NAME']); ?> <ul id="subnav"> <li><a href="gallery.php?category=Wedding" id="wedding" <?php if ($currentPage == 'gallery.php?category=Wedding') {echo 'class="selected"';} ?>>Wedding Hair</a></li> <li><a href="gallery.php?category=Men" id="mens-styles" <?php if ($currentPage == 'gallery.php?category=Men') {echo 'class="selected"';} ?>>Mens Styles</a></li> <li><a href="gallery.php?category=Women" id="womens-styles" <?php if ($currentPage == 'gallery.php?category=Women') {echo 'class="selected"';} ?>>Women's Styles</a></li> <li><a href="gallery.php?category=Salon" id="salon" <?php if ($currentPage == 'gallery.php?category=Salon') {echo 'class="selected"';} ?>>Salon</a></li> </ul> This is not working I think it's because of the file extenstion 'gallery.php?category=Wedding' is there any way I can get this to work with these extensions Any help would be greatly appreciated Thanks Barry I have finally gotten my registration form to work on my website but i have another question! When for example someone registers at my website and type in the wrong email address, i've set php to give out an error message like "Please enter a valid email address". But that message shows up at the verry top-left corner of my page and makes some objekts move around on my page and you can't click in the <input> fields anymore . How could i make those error messages show up above my registration form at the top of my site and maybe style them with a border and maybe make the text red and so that it doesnt change anything on my page? could i use css for that somehow or what? This is how i show the error messages right now: public function show_errors() { echo "<h3>Errors</h3>"; foreach($this->errors as $key=>$value) echo $value."<br>"; } public function valid_data() { if($this->user_exists()) $this->errors[] = 'Username already taken, choose another one!'; if(empty($this->username)) $this->errors[] = 'Please enter a valid username!'; if(empty($this->first_name)) Code: [Select] <form name="commentbox" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table border="0"> <textarea name="comment" cols="20" rows="2" onclick="document.commentbox.comment.value='';" onfocus="this.style.borderColor='yellow';" onblur="this.style.borderColor='blue';" />Comment...</textarea> </td></tr> $commentcheck = $_POST['comment']; if ($commentcheck == "Comment...") { die(' <META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=index.php\"> '); }else why does this not refresh if i comment "Comment..." it just dies and doesnt echo anything Okay... Since my last post about the website I was working on, I've figured out what I was asking. But now, I've run into another problem... Once I have a functional form on my website where a user inputs data and presses submit, how do I go about getting it to remain posted where directed? So if the user types his first and last name and presses submit, how do I get their input to remain posted on the page, much like a guestbook? I'm a newbie at this stuff, but it's a must for mine and my partner's website. Thanks again! PS... I have the code for the form all typed out and it works, it just displays the data with $echo rather than posting it permanantly. Hi, can anyone help show me or tell me where I can find out how I can create a comment box system that will get the members userid/username and post it with the comment when it has been submitted on the page. e.g the way youtube displays comments with the username and then the comment underneath. thanks, I have a comment form that I have created. It gets the id from the database and prints out the data that goes with that id, but what it also does is prints out the information into the form. I want a blank form so that the user can add a comment to the record. Any ideas? This the code for the form: Code: [Select] <form method="post" action="pv.php?id=<?php echo $row['ID']?>&action=<?php echo $form_action ?>"> <fieldset> <legend></legend> <p> <label for="cname">Date Of Birth</label> * <input id="cname" name="dateofbirth" class="required date" value="<?php echo $row['Date_Of_Birth']?>" /> (eg 1978.11.11) </p> <p> <label for="cgender">Gender</label> * <input type="radio" name="gender" value="Male" <?php if($row['Gender']=='male'){echo 'checked';}?>/> Male <input type="radio" name="gender" value="Female" <?php if($row['Gender']=='female'){echo 'checked';}?>/> Female </td> </p> <p> <label for="curl">Title</label> * <select name="title" id="title" class="required"> <option value="">Please Select</option> <option value="Mr" <?php if($row['Title']=='Mr'){echo 'selected';}?>>Mr</option> <option value="Ms" <?php if($row['Title']=='Ms'){echo 'selected';}?>>Ms</option> <option value="Mrs" <?php if($row['Title']=='Mrs'){echo 'selected';}?>>Mrs</option> <option value="Miss" <?php if($row['Title']=='Miss'){echo 'selected';}?>>Miss</option> <option value="Other" <?php if($row['Title']=='Other'){echo 'selected';}?>>Other</option> </select> </p> <p> <label for="ccomment">First Name</label> * <input type="text" name="firstname" value="<?php echo $row['First_Name']?>" maxlength="50" /> </p> <p> <label for="cemail">Last Name</label> * <input id="cemail" type="text" name="lastname" value="<?php echo $row['Last_Name']?>" maxlength="75" /> </p> <p> <label for="ccomment">Address 1</label>* <input type="text" name="address1" value="<?php echo $row['Address_Line_1']?>" maxlength="50" /> </p> <p> <label for="ccomment">Address 2</label> <input type="text" name="address2" value="<?php echo $row['Address_Line_2']?>" maxlength="50" /> </p> <p> <label for="ccomment">City</label>* <input type="text" name="city" value="<?php echo $row['City']?>" maxlength="50" /> </p> <p> <label for="ccomment">Postcode</label>* <input type="text" name="postcode" value="<?php echo $row['Postcode']?>" maxlength= "10" /> (eg LE5 5QE) </p> <p> <label for="ccomment">Contact No</label>* <input type="text" name="contactno" value="<?php echo $row['Contact_No']?>" maxlength= "12" /> (eg 077448825723) </p> <p> <label for="ccomment">Email</label>* <input type="text" name="email" value="<?php echo $row['Email']?>" maxlength= "40"/> (eg info@example.com) </p> <p> <label for="ccomment">Comment</label> <textarea rows="10" cols="30" name="note" maxlength= "500"><?php echo $row['Additional_Comment']?></textarea> </p> <p> <input class="submit" type="submit" value="Submit"/> </p> <p> <a href='pv.php'>Main Page</a> </p> </fieldset> This is the code for printing out the data on the page: Code: [Select] if($_GET['action']=='comment'){ $form_action = 'comment_ok'; $id=$_GET['id']; $result = mysql_query("SELECT * FROM project_data WHERE id='$id'"); $row = mysql_fetch_array($result); echo'<b>'; echo $row['Date_Of_Birth']; echo '  '; echo $row['Gender']; echo '  '; echo $row['Title']; echo '  '; echo $row['First_Name']; echo '  '; echo $row['Last_Name']; echo '  '; echo $row['Address_Line_1']; echo '  '; echo $row['Address_Line_2']; echo '  '; echo $row['City']; echo '  '; echo $row['Postcode']; echo '  '; echo $row['Contact_No']; echo '  '; echo $row['Email']; echo '  '; echo $row['Additional_Comment']; echo '</b>'; } and a snippet of the code I am using to send the id to the form: Code: [Select] echo "<td><a href='pv.php?action=edit&id=" . $row['ID']."'>Edit</a>  <a href='pv.php?action=delete_ok&id=" . $row['ID']."'>Delete</a>  <a href='pv.php?action=comment&id=" . $row['ID']."'>Comment</a></td>"; echo "</tr>"; Hi everyone, I have a like button on repeating regions that is used for keeping a record of votes on comments. People click it an it adds 1 to the votes column total. It works just fine. Problem is i only want to a allow a user one vote per comment only. can vote on other comments as well, but again, only one vote per day per comment. i wouldn't know where to start. Would i use a cookie or record an ip? switch ($_REQUEST['action']) { case 'likeit'; foreach($_REQUEST as $key=>$value){ $$key = $value; } $votes = $_GET['votes']; $total = ($votes + 1); $sql = mysql_query("UPDATE quotes SET votes = '$total' WHERE quoteid = '$quoteid'"); break; } I've got a comment box that connects to a MySQL database. I've got this error message here when I use the PHP include function to call on this script on my main page http://www.inkzoid.com/index.php ... <?php // calling session_start() the function which starts our authentication session session_start(); // connecting to mysql server $l = mysql_connect ( "999.999.999.999" , "inkzoid" , "999) or die("Error connecting:<BR><BR>".mysql_error()); mysql_select_db( "inkzoid" ) or die("Error getting db:<BR><BR>".mysql_error()); // defining getShouts() which is our function that gets all of our shouts function getShouts() { echo '<div align="center"> <table width="150" border="0" cellspacing="0" cellpadding="0"> <tr> <td> '; $query = mysql_query("SELECT * FROM shouts ORDER BY id DESC LIMIT 10") or die(mysql_error()); while ($row = mysql_fetch_array($query) ) { $name = stripslashes($row['Name']); $contact = stripslashes($row['Contact']); $shout = stripslashes($row['Shout']); if(empty($contact)) { echo '<p><span class="author">'.$name.'</span> - <span class="shout">'.$shout.'</span></p>'; } else { echo '<p><span class="author"><a href="'.$contact.'" target="_blank">'.$name.'</a></span> - <span class="shout">'.$shout.'</span></p>'; } // if empty contact } // while row mysqlfetcharray query echo '<br><br>'; echo ' </td> </tr> <tr> <td height="10"> </td> <form name="shout" method="post" action="shout.php"> <div align="center"> <input name="name" type="text" id="name" value="Name" size="25" maxlength="10"><br> <input name="contact" type="text" id="contact" value="http://" size="25"><br> <input name="message" type="text" id="message" value="Message" size="25"><br> <input name="shout" type="submit" id="shout" value="Shout!"> </div> </form> </td> </tr> </table> </div> '; } // function getshouts // our processing if control statement if ( isset ( $_POST['shout'] ) ) { $name = addslashes($_POST['name']); $contact = addslashes($_POST['contact']); $message = $_POST['message']; if ( ( isset($name) ) && ( isset($message) ) ) { // getting smilie list $smilies = mysql_query("SELECT * FROM smilies") or die(mysql_error()); while($get = mysql_fetch_array ($smilies)) { $alt = $get['Alt']; $smilie = $get['URL']; $message = str_replace( $get['Symbol'] , '<img src="smilies/'.$smilie.'" border="0" width="15" height="15" alt="'.$alt.'">' , $message); $themessage = addslashes($message); // replacing all smilies } mysql_query("INSERT INTO shouts (Name, Contact, Shout) VALUES ( '$name' , '$contact' , '$message' )") or die(mysql_error()); $_SESSION['has_posted'] = 'yes'; header("Location: shout.php"); // if required fields aren't empty, process into database } else { echo '<script>alert("Some fields were not filled out!");</script>'; header("Location: shout.php"); // if required fields were left empty, show an error dialog } }/* else { echo '<script>alert("Please follow the form to this page.");</script>'; header("Location: shout.php"); // if they weren't even referred from the form, show error dialog and redirect } // if isset post shout /* STARTING THE MAIN SCRIPT NOW */ // starting the table //displaying the shouts getShouts(); mysql_close($l); ?> I was told that I should put ob_start(); at the top of my page and ob_end_clear(); at the end of my page, but should that code go on the shout.php file or my index.php file (index.php is where I want my comment box to go)? I was using session_start(); but I got some error messsages about headers already being sent because I had originally used the php include function at the top of index.php to include shout.php. After reading up on it I found out that the error was caused by not having session_start(); being the first line of code. So my problem was that I wanted the comment box to be somewhere other than at the top of the page. Not sure how to move it. Anyways I'm pretty confused right now and if someone could give me some advice it would be appreciated. |