mattm1712 Posted March 15, 2010 Share Posted March 15, 2010 hi i am trying to design a forum based site i have made a login and comment bits but i cant seem to sort out deleting the comment it deletes all commnents from that user so i have to start again can any1 help this is my comment script, <?php if(isset($_POST['submit'])) { $comment=$_POST['comment']; if ($comment&&$comment!="") { include 'connect.inc'; mysql_query("INSERT INTO comments VALUES('','$comment','$name')"); mysql_close(); } } ?> <form action='index.php?variable=P2' name='register' method='post' class='form'> <input type='text' name='comment'> <input type='submit' name='submit'> </form> <?php include 'connect.inc'; $query = mysql_query("SELECT * FROM comments;" ); while ($row=mysql_fetch_assoc($query)) { echo $row[comments]." posted by ".$row[submittedby]."<br>"; } ?> Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 15, 2010 Share Posted March 15, 2010 hi i am trying to design a forum based site i have made a login and comment bits but i cant seem to sort out deleting the comment it deletes all commnents from that user so i have to start again can any1 help this is my comment script, <?php if(isset($_POST['submit'])) { $comment=$_POST['comment']; if ($comment&&$comment!="") { include 'connect.inc'; mysql_query("INSERT INTO comments VALUES('','$comment','$name')"); mysql_close(); } } ?> <form action='index.php?variable=P2' name='register' method='post' class='form'> <input type='text' name='comment'> <input type='submit' name='submit'> </form> <?php include 'connect.inc'; $query = mysql_query("SELECT * FROM comments;" ); while ($row=mysql_fetch_assoc($query)) { echo $row[comments]." posted by ".$row[submittedby]."<br>"; } ?> where is the delete script? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 15, 2010 Share Posted March 15, 2010 That code is for inserting a comment. You say you're having problems with deleting comments. Post your code that deletes comments. Quote Link to comment Share on other sites More sharing options...
mattm1712 Posted March 15, 2010 Author Share Posted March 15, 2010 sorry in still learning php if thats y u are wonding about my script been messed up this is my delete <? if($_GET["cmd"]=="delete") { include 'connect.inc'; $sql = "DELETE FROM comments WHERE comment=$comment"; $result = mysql_query($sql); echo "Row deleted!"; } ?> <?php $dbusername="web123-matt"; $dbpassword="matt"; $dbdatabase="web123-matt"; mysql_connect(localhost,$dbusername,$dbpassword); mysql_select_db("comments"); if(!isset($cmd)) { $result = mysql_query("select * from comment order by comment"); while($r=mysql_fetch_array($result)) { $title=$r["title"]; $comment=$r["comment"]; echo "<a href='delete.php?cmd=delete&name=$name'>$comment - Delete</a>"; echo "<br>"; } } ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 15, 2010 Share Posted March 15, 2010 In your comments table you should have setup a field called something the lines of comment_id (or id for short) which is an auto_increment field. That way each row that gets inserted into the comments table will have a unique id associated with it. That way you can easily delete the comment based on the id for that row. Here is how I'd do it $dbusername="web123-matt"; $dbpassword="matt"; $dbdatabase="web123-matt"; mysql_connect(localhost,$dbusername,$dbpassword); mysql_select_db("comments"); if(!isset($_GET['cmd'])) { $result = mysql_query("select id, title, comment FROM comments ORDER BY id"); while($r = mysql_fetch_assoc($result)) { $id = $r['id']; $title = $r['title']; $comment = $r['comment']; echo "<a href='delete.php?cmd=delete&id=$id'>$comment - Delete</a>"; echo "<br>"; } } elseif(isset($_GET['cmd'])) { if($_GET["cmd"]=="delete" && (isset($_GET['id']) && is_numeric($_GET['id']))) { include 'connect.inc'; $sql = "DELETE FROM comments WHERE id=$id"; $result = mysql_query($sql); echo "Row deleted!"; } } Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 15, 2010 Share Posted March 15, 2010 That is a bad way to delete the comment if I am reading your code right. You are using the entire comment string to figure out which comment needs to be deleted? Although that might not be the problem you should have like a comment_id field in your comment table that whenever you need to delete a comment you can reference so that you know you are deleting only 1 comment. Quote Link to comment Share on other sites More sharing options...
mattm1712 Posted March 15, 2010 Author Share Posted March 15, 2010 hi im having a problem with the line while($r = mysql_fetch_assoc($result)) it says Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 15, 2010 Share Posted March 15, 2010 hi im having a problem with the line while($r = mysql_fetch_assoc($result)) it says Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in Did you add an id field? You have to change the SQL query to whatever applies. Are you familiar with the proper syntax for the queries? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 15, 2010 Share Posted March 15, 2010 If you're using my code suggestion you'll need to adapt it to your needs. Mine is only an example it may (or not) work for you. Make sure you have a field called id within your comments table that is set to auto_increment. To add the id field to your comments table run the following SQL query ALTER TABLE comments ADD COLUMN id INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY Quote Link to comment Share on other sites More sharing options...
mattm1712 Posted March 15, 2010 Author Share Posted March 15, 2010 yeah i have i have id comments submittedby this is the code <?php $dbusername="web123-matt"; $dbpassword="matt"; $dbdatabase="web123-matt"; mysql_connect(localhost,$dbusername,$dbpassword); mysql_select_db("comments"); if(!isset($_GET['cmd'])) { $result = mysql_query("select id, comments, submittedby FROM comments ORDER BY id"); while($r = mysql_fetch_assoc($result)) { $id = $r['id']; $comments = $r['comments']; $submittedby = $r['submittedby']; echo "<a href='delete.php?cmd=delete&id=$id'>$submittedby - Delete</a>"; echo "<br>"; } } elseif(isset($_GET['cmd'])) { if($_GET["cmd"]=="delete" && (isset($_GET['id']) && is_numeric($_GET['id']))) { include 'connect.inc'; $sql = "DELETE FROM comments WHERE id=$id"; $result = mysql_query($sql); echo "Row deleted!"; } } ?> but its works fine but when i go back to the comments its still there and hasnt been deleted Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 16, 2010 Share Posted March 16, 2010 You are using a variable that hasn't been set. If you call the page with $_GET['cmd'] set then it will go to the delete part. If you call the page without $_GET['cmd'] set then it will display the comments. Looks like $_GET['id'] is the id of the comment to be deleted so this line $sql = "DELETE FROM comments WHERE id=$id"; should most likely be this line $sql = "DELETE FROM comments WHERE id=$_GET['id']"; Quote Link to comment Share on other sites More sharing options...
mattm1712 Posted March 16, 2010 Author Share Posted March 16, 2010 hi i have tried that but no luck cheers tho Quote Link to comment Share on other sites More sharing options...
mattm1712 Posted March 16, 2010 Author Share Posted March 16, 2010 this is the error im getting Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/sites/moseleyengineering.co.uk/public_html/matt/delete.inc on line 28 which is $sql = "DELETE FROM comments WHERE id=$_GET['id']"; Quote Link to comment 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.