HOME / BLOG / BLOG-POST

How to delete data from database table in wordpress?

Delete data from database table in wordpress

<?php
if(isset($_POST["del"])){
global $wpdb;
$table_name = $wpdb->prefix . "my_plugintable";
 
 $idval = $_POST["idval"];
 

 
  $wpdb->query( $wpdb->prepare( "DELETE FROM $table_name WHERE ID = %d", $idval));
 
}

 get_header();

?>

<table border="2" >
<tr>
<th width="50px">ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Phone</th>
<th>Message</th>
<th>Delete</th>
</tr>
<?php
global $wpdb;
$table_name = $wpdb->prefix . "my_plugintable";
$result = $wpdb->get_results ( "SELECT * FROM $table_name" );
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->id;?></td>
<td><?php echo $print->firstname;?></td>
<td><?php echo $print->lastname;?></td>
<td><?php echo $print->email;?></td>
<td><?php echo $print->phone;?></td>
<td><?php echo $print->message;?></td>
<td><form method="post" action=""><input type="hidden" value="<?php echo $print->id;?>" name="idval"> <input type="submit" name="del" value="X"></form></td>
</tr>

<?php
}
?>
</table>

 


about authors

Sandip Das

Web Developer

Once a legend said “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler.



POST COMMENTS

Leave a Reply

Your email address will not be published. Required fields are marked *