How to Delete Record From Table Using PHP MySQL



Allowing users to deactivate/delete their account, to delete post/article and even friend and unfriend system however require the PHP MYSQL delete query if you’re dealing with PHP MYSQL database.








To delete post, user account all require this same delete query. So in this tutorial I’ll be showing you how to delete post from table using MySQL delete query.

php mysql delete query


some useful tutorials:




Reading on I hope you already have your database, table created and some pretty records inserted in it then next is for us to get our hands dirty with the coding process.
Everything is going to be put in same web page.

So I have a DATABASE name -> delete and a TABLE name -> record.

My table has three rows: post_id, post_title and post_content.



post_id          post_title      post_content
1                      coding           coding is important




Now below is the entire code for deleting record from table:

<!DOCTYPE html>
<htmL>
<head>
<title>mysql delete query</title>
</head>
<body>
<form action="" class="signup-form-class">
<input type="submit" name="delete-submit" value="delete" />
</form>
<?php
$mydb = new mysqli("localhost", "root", "", "delete");
if(isset($_POST['delete-submit']))
{
$post_id = 1;
$postid = $post_id;
$su = $mydb->prepare("DELETE FROM record WHERE post_id='{$postid}'");
$su->execute();
If($su)
{
Echo “post deleted!”;
}
}
?>
</body>
</html>

An html form with no input field, the form has one delete button.

Read all about html form






I put both the delete button and the delete MYSQL query in the same page. As you can see, there is no METHOD attribute in the form simply because I’m not posting anything into the database.

If the delete query is successful, then you’ll get “post deleted!” report message.

Remember I did Demo about the table, which has one record, post_id =1, post_title=coding and post_content=coding is important.

I did not fetch record from the table, so I assigned the post_id value which is 1 to a variable which I name ($post_id = 1;) and assigned the variable $post_id into $postid ($postid = $post_id;).

Subscribe, follow us more are on the way.

Comments

  1. Replies
    1. Thanks! Please subscribe to the blog for more updates.

      Delete

Post a Comment

Popular posts from this blog

Guide to add blog URL to Google search console

Best steps to master JavaScript programming language