Click to See Complete Forum and Search --> : Deleting MySql records


Stevo
08-16-2004, 12:39 PM
Alright I have this script and I want to add a delete feature to it. The table has a username and password for each record and I want to delete the record using the username as the ID. Is this possible? If so, how?

Here's the script:
<?php

$db = mysql_connect($host, $dbuname, $dbpass);

mysql_select_db($dbname,$db);

echo "<table>";

echo "<tr>";

echo "<td><strong>Username:</strong></td>";

echo "<td><strong>Password:</strong></td>";

echo "<td><strong>Delete</strong></td>";

echo "</tr>";

$result = mysql_query("SELECT * FROM users ORDER BY username ASC",$db);

while ($myrow = mysql_fetch_array($result)) {

$username = $myrow["username"];

$password = $myrow["password"];

echo "<tr>";

echo "<td>$username</td><td>$password</td><td>Delete</td>";

echo "</tr>";

}

echo "</table>\n";

?>


Help would be appreciated.:)

Naemo
08-16-2004, 03:30 PM
hi

you could create a new page called delete.php. create a link on the page you have just created and pass the username as a query string:



delete.php?username=charlie


Then the mysql code is:


"DELETE FROM contacts WHERE username='$username'"



Hope that helps

Stevo
08-16-2004, 04:04 PM
That might work but is it possible to keep it in one file?

Naemo
08-16-2004, 04:14 PM
Yeah just include something like this:




if($action=="delete" && isset($del_user)){
# Connect to mysql....
$query="DELETE FROM contacts WHERE username='$del_user'";
mysql_query($query);
echo "Entry deleted";
}



and then you'll need a link which you could generate e.g.


<a href="filename.php?action=delete&del_user=username">Delete</a>



hope that helps

Stevo
08-16-2004, 07:46 PM
Perfect! Thanks a lot.:)