bigrossco Posted December 20, 2006 Share Posted December 20, 2006 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"> <?phpif (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 advanceBigRossCo Quote Link to comment https://forums.phpfreaks.com/topic/31343-solved-deleting-records/ Share on other sites More sharing options...
chiprivers Posted December 20, 2006 Share Posted December 20, 2006 The following should go at the beginning of your script (after connecting to database)<?phpif (isset($_GET['delete'])) {$qry = "DELETE FROM staff WHERE staffID = ".$_GET['delete'];$deleted = mysql_query($qry);}?>Your delete link should be scripted like this:<?phpecho "<td><a href=\"".$_SERVER['PHP_SELF']."?delete=".$row[0]."\">delete</a></td>";?> Quote Link to comment https://forums.phpfreaks.com/topic/31343-solved-deleting-records/#findComment-145051 Share on other sites More sharing options...
chiprivers Posted December 20, 2006 Share Posted December 20, 2006 The following should go at the beginning of your script (after connecting to database)[code]<?phpif (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]<?phpecho "<td><a href=\"".$_SERVER['PHP_SELF']."?delete=".$row[0]."\">delete</a></td>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31343-solved-deleting-records/#findComment-145052 Share on other sites More sharing options...
bigrossco Posted December 20, 2006 Author Share Posted December 20, 2006 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());?><?phpif (isset($_GET['delete'])) {$qry = "DELETE FROM staff WHERE staffID = ".$_GET['delete'];$deleted = mysql_query($qry);}?><form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <?phpif (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] Quote Link to comment https://forums.phpfreaks.com/topic/31343-solved-deleting-records/#findComment-145066 Share on other sites More sharing options...
chiprivers Posted December 20, 2006 Share Posted December 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/31343-solved-deleting-records/#findComment-145068 Share on other sites More sharing options...
bigrossco Posted December 20, 2006 Author Share Posted December 20, 2006 tried and still would not work so done it another way, I made the delete go to another pager, where the user gets a confermation if they want to delete then they would click on YES after YES is selected the user is then deleted!Ross Quote Link to comment https://forums.phpfreaks.com/topic/31343-solved-deleting-records/#findComment-145079 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.