Deletion question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Deletion question

Post by jefffan24 »

I have a script running to make a deletion link and it works but when you click it, it deletes the record but it also keeps the link until you refresh the page.

So like lets say it was like

Bob | *link*Delete?*link*

You click the delete link, the record is no longer in the database but the line above still shows until you refresh the page. Is there anyway to stop this from happening? Also here is my code if it helps ignore all the if statements I know switch/case would have been better I'm just too lazy to go change them right now.

Code: Select all

 
$sql = "SELECT * FROM drivers ORDER BY Name";
    $result = mysql_query($sql);
    
    $id = $_GET[ID];
    $delete = mysql_query("DELETE FROM drivers WHERE ID = $id");
        
    while($list = mysql_fetch_assoc($result)){
        
        echo $list['Name'];
        if($list['Link'] != null){
        echo ' | <a href="http://trukz.com/driver_display.asp?ID=' . $list['Link'] . '" target="_blank">Link to Driver Page</a> | ';
        }
        else if($list['Link'] == null){
            echo ' | No Link | ';
        }
        if(!$_COOKIE['InfoSec'] == 1){
            if($list['Company'] == "NA"){
                echo 'Haulin Glass Expediting<br /><br />';
            }
            if($list['Company'] == "EU"){
                echo 'Haulin Glass Europe<br /><br />';
            }
            if($list['Company'] == "AU"){
                echo 'Haulin Glass Australia<br /><br />';
            }
        }
        if($_COOKIE['InfoSec'] == 1){
            if($list['Company'] == "NA"){
                echo 'Haulin Glass Expediting | <a href="drivers.php?ID=' . $list['ID'] . '">Delete?</a><br /><br />';
            }
            if($list['Company'] == "EU"){
                echo 'Haulin Glass Europe | <a href="drivers.php?ID=' . $list['ID'] . '">Delete?</a><br /><br />';
            }
            if($list['Company'] == "AU"){
                echo 'Haulin Glass Australia | <a href="drivers.php?ID=' . $list['ID'] . '">Delete?</a><br /><br />';
            }
        }
    }
 
 
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Deletion question

Post by JakeJ »

Put inside a frame and just refresh the frame or use ajax.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Deletion question

Post by social_experiment »

This code does something similar to what you want but it's one page being used over and over. You have a page that displays all the records with a delete link next to them, as per your example. In the code i created i used that same page to also delete the record. When you access the page, there is no query string set. Once you click on the delete link the page is opened (same page) this time with a querystring that carries the name 'ID' and value of the record id out of the database. The script checks whether or not the querystring has been set, if not the page displays all records. If the value is set, the script deletes that selected record, then again displays all records from the database.

Code: Select all

<?php
    if (!isset($_GET['ID']) || $_GET['ID'] == '') {
        mysql_connect('???', '???', '???');
        mysql_select_db('???');
        
        $query = mysql_query("SELECT * FROM table");
        
        while ($pointer = mysql_fetch_assoc($query)) {
            echo $pointer['username']." <a href=\"Untitled-2.php?ID=".$pointer['id']."\">Delete ".$pointer['id']."</a>";
            echo '<br />';
        }
    }
    else {
        mysql_connect('???', '???', '???');
        mysql_select_db('???');
        
        $deleteQuery = mysql_query("DELETE FROM table WHERE id = '".mysql_real_escape_string($_GET['ID'])."' ");        
        
        $query = mysql_query("SELECT * FROM table");
        
        while ($pointer = mysql_fetch_assoc($query)) {
            echo $pointer['username']." <a href=\"Untitled-2.php?ID=".$pointer['id']."\">Delete ".$pointer['id']."</a>";
            echo '<br />';
        }
    }
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: Deletion question

Post by jefffan24 »

Thank you so much social experiemnt that works perfectly :D
Post Reply