Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP Delete onClick


  • Please log in to reply
2 replies to this topic

#1 Alex_j

Alex_j

    CC Regular

  • Just Joined
  • PipPipPip
  • 29 posts

Posted 12 March 2010 - 06:21 AM

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?

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>";

  • 0

#2 Bioshox

Bioshox

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 210 posts

Posted 12 March 2010 - 06:37 AM

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;


<?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;
?>


  • 0

#3 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 13 March 2010 - 01:32 PM

Just do something like this:

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>";
And then in delete.php file just remove a record that has this "userID" that you will take from your url like this:



if($_GET['id'] != ""){

$userID = $_GET['id'];

$sql = "DELETE FROM table_name WHERE userID='".$userID."'";
$query = mysql_query($sql);

}

  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download