Jump to content

comment delete help


mattm1712

Recommended Posts

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>";

 

}

 

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>";

    }

}

?>

 

Link to comment
Share on other sites

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!";
    }
   
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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']";

Link to comment
Share on other sites

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']";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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