Hi, I want to be able to delete a record in a MySQL table, by just clicking on a delete link or by using a checkbox system for multiple rows. How would I go about doing this?
i have this code so far, would I have to put the code for the delete in a separate PHP file?
Code:while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['userID'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['Surname'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . "<a href='delete.php'>Delete</a>" . "</td>";
echo "</tr>";
}
echo "</table>";
Hey there, I have a simlar piece of code that you wish, but I use it to delete members and entries on my CMS system, here it is;
Code:<?php
function get_content($id = '') {
//Gets the ID number from the SQL Database
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM cms";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
//Makes members names into links
echo "<h3>" . $row['title'] . " <br> <a href='?delete=" . $row['id'] . "'>Delete Post</a></h3><br>";
}
} //Ends Our Class
//Displays the posts
?>
<?php
function delete_content($id = '') {
if (!$id) {
return false;
}else {
//Gets the ID number from the SQL Database
$id = mysql_real_escape_string($id);
$sql = "DELETE FROM cms WHERE id = '$id'";
$res = mysql_query($sql) or die(mysql_error());
echo "<h3>Content Deleted</h3>";
}
} //Ends Our Class
?>
<?php
if(isset($_GET['delete'])):
delete_content($_GET['delete']);
else:
//If a post hasent been selected, display everything
get_content();
endif;
?>
Just do something like this:
And then in delete.php file just remove a record that has this "userID" that you will take from your url like this:Code:while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['userID'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['Surname'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . "<a href='delete.php?id=".$row['userID']."'>Delete</a>" . "</td>";
echo "</tr>";
}
echo "</table>";
Code:
if($_GET['id'] != ""){
$userID = $_GET['id'];
$sql = "DELETE FROM table_name WHERE userID='".$userID."'";
$query = mysql_query($sql);
}
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks