Jump to content

PHP Delete onClick

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
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>"; 


#2
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
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;
		?>



#3
Guest_Jaan_*

Guest_Jaan_*
  • Guests
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);

}