Jump to content

Recommended Posts

I am working on a program and want to be able to delete a record from the list i have on screen, I use the code shown below, and want to allow where it says delete to get it to delete that record.

[code]

  <?php
    $host = "$lang_dbhost";
    $user = "$lang_dbuser";
    $pass = "$lang_dbpass";
    $db = "$lang_dbase";

    // open connection
    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

    // select database
    mysql_select_db($db) or die ("Unable to select database!");



    $query = "SELECT *
FROM staff";

    // execute query
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

?>


<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
  <?php
if (mysql_num_rows($result) > 0) {
echo"<table border = 1>";
echo"<tr>";
echo"<td><b><center>$lang_userid</td><td><b><center>$lang_username</b></td><td><b><center>$lang_fullname</b></td>";
while($row = mysql_fetch_row($result)){
echo"<tr>";
        echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td><a href=\"./staff_pass.php?search=".$row[0]."\">update</a></td>";
echo "<td><a href=\"./client-results.php?search=".$row[0]."\">delete</a></td>";
  }
  }

?>
</table>
</form>
[/code]

Thank you in advance

BigRossCo
Link to comment
https://forums.phpfreaks.com/topic/31343-solved-deleting-records/
Share on other sites

The following should go at the beginning of your script (after connecting to database)

<?php

if (isset($_GET['delete'])) {
$qry = "DELETE FROM staff WHERE staffID = ".$_GET['delete'];
$deleted = mysql_query($qry);
}

?>

Your delete link should be scripted like this:

<?php

echo "<td><a href=\"".$_SERVER['PHP_SELF']."?delete=".$row[0]."\">delete</a></td>";

?>
The following should go at the beginning of your script (after connecting to database)

[code]
<?php

if (isset($_GET['delete'])) {
$qry = "DELETE FROM staff WHERE staffID = ".$_GET['delete'];
$deleted = mysql_query($qry);
}

?>
[/code]

Your delete link should be scripted like this:

[code]
<?php

echo "<td><a href=\"".$_SERVER['PHP_SELF']."?delete=".$row[0]."\">delete</a></td>";

?>
[/code]
Thanks for getting back but for some reason I cant seem to get it to work, when I click on the link it changes the URL to staff_admin.php?delete=4

But it dosent seem to delete it, must be because I have placed it in the wrong place or something?

[code]

<?php
    $host = "$lang_dbhost";
    $user = "$lang_dbuser";
    $pass = "$lang_dbpass";
    $db = "$lang_dbase";

    // open connection
    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

    // select database
    mysql_select_db($db) or die ("Unable to select database!");



    $query = "SELECT *
FROM staff";

    // execute query
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());


?>



<?php

if (isset($_GET['delete'])) {
$qry = "DELETE FROM staff WHERE staffID = ".$_GET['delete'];
$deleted = mysql_query($qry);
}

?>


<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
  <?php


if (mysql_num_rows($result) > 0) {
echo"<table border = 1>";
echo"<tr>";
echo"<td><b><center>$lang_userid</td><td><b><center>$lang_username</b></td><td><b><center>$lang_fullname</b></td>";
while($row = mysql_fetch_row($result)){
echo"<tr>";
        echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td><a href=\"./staff_pass.php?search=".$row[0]."\">$lang_update</a></td>";
echo "<td><a href=\"".$_SERVER['PHP_SELF']."?delete=".$row[0]."\">delete</a></td>";
  }
  }

?>


[/code]
You have put the delete query after the select query, it is therefor selecting the records to display before deleting the requested one.  I would imagine if you refresh the display the record will have been deleted.  You need to move the delete query to before the select query.
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.