Delete ID from a MySql Database, Geustbook script help.

Discussion in 'PHP' started by jacobbannier, Nov 11, 2008.

  1. #1
    Hey, I am trying to code a simple guestbook, and am trying to add a delete comment feature.

    So far I have:
    viewguestbook.php
    <?php
    
    include('connect.php');
    
    mysql_select_db('guestbook');
    
    $sql=("SELECT * from guestbook ORDER by datetime DESC");
    
    $result=mysql_query($sql);
    
    while($rows=mysql_fetch_array($result)){?>
    
    
    <table border="1">
    	
    <tr>
    	<td>Comment ID:</td><td width=200><?php echo $rows['id'];?></td>
    </tr>
    <tr>
    	<td>Name:</td><td width=200><?php echo $rows['name']; ?></td>
    </tr>
    <tr>
    	<td>Email</td><td width=200><?php echo $rows['email']; ?></td>
    </tr>
    <tr>
    	<td>Comment:</td><td width=200><?php echo $rows['comment']; ?></td>
    </tr>
    <tr>
    	<td>Date Posted:</td><td width=200><?php echo $rows['datetime']; ?></td>
    </tr>
    <tr><td><a href="deleteac.php?id=<? echo $rows['id']; ?>">Delete Comment</a></td></tr>
    
    </table>
    
    
    <?PHP } ?>
    PHP:
    deleteac.php
    <?php
    
    include('connect.php');
    mysql_select_db('guestbook');
    
    $id=$_GET['id'];
    
    $sql="DELETE FROM guestbook WHERE id='$id'";
    $result=mysql_query($sql);
    
    	
    	if($result){
    		
    		echo 'Deleted!';
    
    }
    
    mysql_close();
    
    ?>
    PHP:
    Any help would be appreciated, thanks! :D
     
    jacobbannier, Nov 11, 2008 IP
  2. refined

    refined Peon

    Messages:
    476
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What's wrong?
     
    refined, Nov 11, 2008 IP
  3. shineDarkly

    shineDarkly Banned

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    at first glance the code looks good, what error are you encountering?
     
    shineDarkly, Nov 11, 2008 IP
  4. joxtechnology

    joxtechnology Peon

    Messages:
    146
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    what the problem here dude? how can we help we can't see any error in your code.
     
    joxtechnology, Nov 11, 2008 IP
  5. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thats your problem. Using a variable like that tells PHP to use it as a string and not parse the value when the query gets executed. Change it to this:

    
    $sql = "DELETE FROM guestbook WHERE id=\"$id\"";
    
    PHP:
    OR if you must use single quotes:

    
    $sql = "DELETE FROM guestbook WHERE id='" . $id . "'";
    
    PHP:
     
    Christian Little, Nov 11, 2008 IP
  6. CodeShop

    CodeShop Active Member

    Messages:
    137
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #6
    You have to verify that the page is getting the values properly.

    To verify the names and values by get or post method user var_dump($_REQUEST); die(); at the top of your script.

    If you will see that the id and its value is passing properly and page is getting the id element then try to use the following code to declare the variable.

    if(@$_GET['id']){

    $id_new=$_GET['id'];

    }

    Then execute and place id variable in the query.
    $query="DELETE FROM guestbook WHERE id='".$id_new."'";

    I hope it will help you a lot.

    Thnx
     
    CodeShop, Nov 11, 2008 IP
  7. jacobbannier

    jacobbannier Active Member

    Messages:
    1,155
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    90
    #7
    I worked out how to do it.

    It turns out that the delete comment link was wrong.
    It was:
    
    <tr><td><a href="deleteac.php?id=<? echo $rows['id']; ?>">Delete Comment</a></td></tr>
    
    PHP:
    This didn't work as the <? didn't open PHP tags.

    
    <tr><td><a href="deleteac.php?id=<?php echo $rows['id']; ?>">Delete Comment</a></td></tr>
    
    PHP:
    This Worked :)


    Thanks for all your help.
     
    jacobbannier, Nov 12, 2008 IP