need some help, how to delete specific results

Discussion in 'PHP' started by Funk-woo10, Nov 19, 2007.

  1. #1
    Hi,

    Im using this to display the content of the sql table.

    <?php
    include ("config.php");
    $result = mysql_query("SELECT * FROM footer");
    while($row = mysql_fetch_array($result))
      {
      echo "<a href='{$row['url']}'>{$row['anchor']}</a><br /><br />";
      }
    
    
    ?>
    PHP:
    This works fine but I need to know how to delete a specific result it out puts.

    I.e have a delete button next to each result !

    Hope that is clear.
     
    Funk-woo10, Nov 19, 2007 IP
  2. Indian_Webmaster

    Indian_Webmaster Banned

    Messages:
    1,289
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Let me tell you a simple Logic:
    put a hyperlink on a delete button and pass that row['url'] and row['anchor'] to another page say deletefooter.php.

    Now on deletefooter.php page,
    fire following query.
    Hope this helps..
     
    Indian_Webmaster, Nov 19, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    nico_swd, Nov 19, 2007 IP
  4. Funk-woo10

    Funk-woo10 Peon

    Messages:
    1,108
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Im having trouble taking the results to the delete page !
     
    Funk-woo10, Nov 19, 2007 IP
  5. rustem

    rustem Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try this:

    
    <?php
    include ("config.php");
    if(isset($_GET['delete_anchor'])){
    	$sql = mysql_query('DELETE FROM `footer` WHERE `anchor` = "'.$_GET['anchor'].'" AND `url` = "'.$_GET['url'].'"');
    	if($sql) echo 'Record deleted.<br/><br/>';
    	else echo '<font color="red">Could not delete a record.</font><br/><br/>';
    }
    $result = mysql_query("SELECT * FROM footer");
    while($row = mysql_fetch_array($result))  {
    	echo "<a href='{$row['url']}'>{$row['anchor']}</a>"; 
    	$url = '?delete_anchor='.$row['anchor'].'&delete_url='.$row['url'];
    	$message = 'Are you sure?';
    	echo '<input type="button" onclick="javascript:confirm_delete(\''.$url.'\',\''.$mess.'\')" value="Delete"/><br /><br />';
    }
    ?>
    PHP:
    Also put the JavaScript code to ask for confirmation before deleting:

    
    <script>
    function confirm_delete(url,message){
    	if(confirm(message)){
    		window.location.href = url;
    	}
    }
    </script>
    
    Code (markup):
    I always have an AUTO_INCREMENT field `id` in each table. It's unique, so can use it in such cases - only one parameter needs to be passed.

    And BTW, note that I personally prefere single quotes in strings - that way code is more readable IMHO as you can better destinguish variables from strings.
     
    rustem, Nov 19, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
  7. Funk-woo10

    Funk-woo10 Peon

    Messages:
    1,108
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Funk-woo10, Nov 19, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Click the links for examples. The first link explains also why you should use these functions.
     
    nico_swd, Nov 19, 2007 IP
  9. Funk-woo10

    Funk-woo10 Peon

    Messages:
    1,108
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks. It kinda works but doesnt delete the resource, also there is no message on the javascript warning.


     
    Funk-woo10, Nov 19, 2007 IP