THAPA TECHNICAL

HOUSE OF WEB DEVELOPERS AND TECHNOLOGY.

CRUD Operation in PHP MySQLi with Code. Select Insert Update Delete in PHP MySQLi

CRUD Operation in PHP MySQL with Code. Select Insert Update Delete in PHP MySQLi


Welcome, all we will see how we can perform CRUD operation using PHP and MYSQL.
First of all, I already made a detailed video on it if you haven't watch it.

Below is the video link.



What is CRUD Operation and it is the most important part for any programmers?
CRUD full form is CREATE, READ, UPDATE & DELETE. So it is clear we have to first CREATE a database and a form. Which we can READ here we refer to both programmers and users those who interact with our sites. There must an option where the user can UPDATE the form details and also DELETE it.
All the features if available then that is called a perfect CRUD operation and this is what mostly backend developer deal with.

First, we will see the Database Part. So here is the con.php file. 

You have to make sure that your username and password is set or not. I mean I never set any username and password for my localhost. So that is the reason why you can see in mysqli_connect, I have written localhost and root, here the root is the username that is by default if you never set by your own. And there is no need to write password because it's empty by default but if you have one add it after the root. 

Also, make sure that your database name must me crudyoutube because below you can see I have written crudyoutube. If you want to write other names simply change the database name on PHPMyAdmin and add that here. 

<?php

$con = mysqli_connect('localhost','root');

mysqli_select_db($con,'crudyoutube');

?>

Fine, Here is the Insert.php file. Where we are actually inserting the Name and Password.

As you can see all the CDN link are already here in the code below. So no need to take from anywhere else just copy and paste it.

<?php

include 'conn.php';

if(isset($_POST['done'])){

$username = $_POST['username'];
$password = $_POST['password'];
$q = " INSERT INTO `crudtable`(`username`, `password`) VALUES ( '$username', '$password' )";

$query = mysqli_query($con,$q);
}
?>

<!DOCTYPE html>
<html>
<head>
<title></title>

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="col-lg-6 m-auto">

<form method="post">

<br><br><div class="card">

<div class="card-header bg-dark">
<h1 class="text-white text-center">  Insert Operation </h1>
</div><br>

<label> Username: </label>
<input type="text" name="username" class="form-control"> <br>

<label> Password: </label>
<input type="text" name="password" class="form-control"> <br>

<button class="btn btn-success" type="submit" name="done"> Submit </button><br>

</div>
</form>
</div>
</body>
</html>

Display.php file

This is the main display page where we will show all the interface that we want too. We want to have a nice table where the user can see their Names, Password, Delete Button and Update Button. 


<!DOCTYPE html>
<html>
<head>
<title></title>

<meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>

 <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
   <script type="text/javascript" charset="utf8" src="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>

</head>
<body>

<div class="container">
<div class="col-lg-12">
<br><br>
<h1 class="text-warning text-center" > Display Table Data </h1>
<br>
<table  id="tabledata" class=" table table-striped table-hover table-bordered">

<tr class="bg-dark text-white text-center">

<th> Id </th>
<th> Username </th>
<th> Password </th>
<th> Delete </th>
<th> Update </th>

</tr >

<?php

include 'conn.php';
$q = "select * from crudtable ";

$query = mysqli_query($con,$q);

while($res = mysqli_fetch_array($query)){
?>
<tr class="text-center">
<td> <?php echo $res['id'];  ?> </td>
<td> <?php echo $res['username'];  ?> </td>
<td> <?php echo $res['password'];  ?> </td>
<td> <button class="btn-danger btn"> <a href="delete.php?id=<?php echo $res['id']; ?>" class="text-white"> Delete </a>  </button> </td>
<td> <button class="btn-primary btn"> <a href="update.php?id=<?php echo $res['id']; ?>" class="text-white"> Update </a> </button> </td>

</tr>

<?php 
}
?>

</table>  

</div>
</div>

<script type="text/javascript">

$(document).ready(function(){
$('#tabledata').DataTable();
})

</script>

</body>
</html>

Delete.php file

Here If anytime user wants to delete any field by simply click on the delete button, the user can do it. Is it that simple? 

<?php

include 'conn.php';

$id = $_GET['id'];

$q = " DELETE FROM `crudtable` WHERE id = $id ";

mysqli_query($con, $q);

header('location:display.php');

?>

Update.php file

Here If anytime user wants to update any field by simply click on the update button, the user can do it. Is it that simple? You have to simply copy it and paste. This is the last part and I hope my video and source code helps you. 
<?php

include 'conn.php';

if(isset($_POST['done'])){

$id = $_GET['id'];
$username = $_POST['username'];
$password = $_POST['password'];
$q = " update crudtable set id=$id, username='$username', password='$password' where id=$id  ";

$query = mysqli_query($con,$q);

header('location:display.php');
}

?>

<!DOCTYPE html>
<html>
<head>
<title></title>

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="col-lg-6 m-auto">

<form method="post">

<br><br><div class="card">

<div class="card-header bg-dark">
<h1 class="text-white text-center">  Update Operation </h1>
</div><br>

<label> Username: </label>
<input type="text" name="username" class="form-control"> <br>

<label> Password: </label>
<input type="text" name="password" class="form-control"> <br>

<button class="btn btn-success" type="submit" name="done"> Submit </button><br>

</div>
</form>
</div>
</body>
</html>

109 comments:

  1. hi how can you show the one you edit?

    value=" ">

    value=" ">

    but it doesn'y work. please help.

    ReplyDelete
  2. can you give Source Code for me brother

    ReplyDelete
  3. i am practicing but i have got an error .I can't solve it .can u help me

    ReplyDelete
  4. can you help me, the coding is okay but it doesnt do what it suppose to do

    ReplyDelete

  5. Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in E:\xampp\htdocs\crud\display.php on line 42............
    i m getting this error sir please help...please

    ReplyDelete
  6. source code file download please

    ReplyDelete
  7. all is well but when i click on update button and show the page of update then when i edit the data and click on submit button then it go to the insert page and cannot update the data

    ReplyDelete
  8. display code hase some error 47 line error

    ReplyDelete
  9. while i am inserting data it doesnot insert data in sql table even i put echo iin the

    php code it will not reflect please help

    ReplyDelete
  10. Very nice Thapa Ji...its really helpfull for me..

    ReplyDelete
  11. can you please add the search query with CRUD and add more details what your writing more detail for learning

    ReplyDelete
  12. Great article with excellent idea! Thank you for such a valuable article. I really appreciate for this great information.
    php software developers

    ReplyDelete
  13. This is a really too good post. This article gives truly quality and helpful information. I’m definitely going to look into it. Really very useful topic info is provided here. Thank you so much buddy and Keep up the good work.badbunnymerch

    ReplyDelete
  14. Thank you brother. I want to make such a database but I couldn't find any tutorials that you give us.

    ReplyDelete
  15. It's high in antioxidants and cleans pollutants out of your hair, so you won't be caught in a hair follicle drug test. You should use it in conjunction with Zydot Ultra Clean, which is recommended as a companion product on the company's website. Of these, the hair follicle drug test is the toughest to crack as this hair testing can detect the presence of THC metabolites in the body for the longest period — at least three months after last consumption. That’s because an average person’s hair grows about half an inch per month. Testclear’s Drug Test Kit is one of the most reliable home marijuana detox kits by a long shot. The test uses your urine to identify remnants of THC in your system. Visit: https://www.urineworld.com/

    ReplyDelete
  16. In this contemporary world, it has become responsible to get to each and everything with present day advances; subsequently, gaming peripherals are the best gaming gadgets which solace the gamers who love to play the best games with simple and open gadget to play their abilities. 먹튀사이트
    토토사이트

    ReplyDelete
  17. I have read your excellent post. This is a great job. I have enjoyed reading your post first time. I want to say thanks for this post. Thank you. 토토커뮤니티순위

    ReplyDelete
  18. Please continue to post good comments. l come every day. Please. 메이저안전사이트

    ReplyDelete
  19. Please continue to post good comments. l come every day. Please. 토토추천사이트

    ReplyDelete
  20. They were active listeners and viewed meetings article. 안전토토사이트 This post is really the best on this valuable topic

    ReplyDelete
  21. I like your blog. i ma happy to read your blog its very informative and your blog is really good and impressive 메이저사이트

    ReplyDelete
  22. I read this article. I think You put a great deal of exertion to make this article. I like your


    fuct

    ReplyDelete
  23. it's really nice and meanful. it's really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull information. juicewrldmerch999.store

    ReplyDelete
  24. Your texts on this subject are correct, see how I wrote this site is really very good. playboyhoodie

    ReplyDelete
  25. I am very glad to read this article. I appreciate your work and inspiration. I also recommended my friends at
    mensslippersmaker to explore this domain.

    ReplyDelete
  26. I feel extremely cheerful to have seen your post. I found the most beautiful and fascinating one.mens marvel dressing gown I am really extremely glad to visit your post.

    ReplyDelete
  27. I enjoyed reading your blog and it contains a lot of information. sheepskinslippershub ​is also related to fashion in which you will find beautiful and charming sleepers made of sheepskin.

    ReplyDelete

  28. I read this article. I think You put a great deal of exertion to make this article.shoptechnoblade.com I like your

    .

    ReplyDelete
  29. Nice and interesting post Shop Now, I appreciate your hard work, keep uploading more, Thank you for sharing valuable information.

    ReplyDelete
  30. I love this! such a great message. it’s a very nice blog.
    ทางเข้าเล่น igoal

    ReplyDelete
  31. I read this article. I think you put a great deal of exertion to make this article. ChelseaBootsMaker

    ReplyDelete
  32. Great website you have got here.shop Here
    Keep up the good work and thanks for sharing your blog site it really help a lot.

    ReplyDelete
  33. This amazing hearings completely acceptable. Most of simple facts are ready through great number connected with practical knowledge realistic expertise. https://chukkabootsmaker.com/ Now i am confident the item all over again completely.

    ReplyDelete
    Replies

    1. Its blog give information from another people very well I am impress its information an information is very nice.
      Click Here

      Delete
  34. Hiya, I’m really glad I have found this information. Nowadays bloggers publish only about gossip and net stuff and this is actually frustrating. A good site with interesting content, this is what I need. Thank you for making this website, and I will be visiting again. Do you do newsletters? I Can’t fin
    https://www.travisscottshop.net/

    ReplyDelete
  35. Thanks for writing such useful content, I like your style of writing and frequently read your content. ดาวน์โหลด spade gaming

    ReplyDelete
  36. Thanks for taking the time to discuss this, I feel strongly that love and read more on this topic. If possible, such as gain knowledge, would you mind updating your blog with additional information? It is very useful for me. dreammerchofficial

    ReplyDelete
  37. I enjoyed reading your blog and it contains a lot of information. MensWalletMaker ​is related to fashion in which you will find beautiful and charming sleepers made by sheepskin.

    ReplyDelete
  38. Nice blog and absolutely outstanding. You can do something much better but i still say this perfect.Keep trying for the best.
    drewhouseofficial

    ReplyDelete
  39. Its an interesting and useful information. commedesgarconshop.net We all get highly motivation from this.

    ReplyDelete
  40. Its an interesting and useful information.
    Click Here We all get highly motivation from this.

    ReplyDelete
  41. I read this article. I think You put a great deal of exertion to make this article.
    quackitymerch.net I like your

    ReplyDelete
  42. Thanks for sharing such beautiful information with us. We hope you will share some more information about it. We really enjoy your blog & content.charleschoice

    ReplyDelete
    Replies
    1. I feel extremely cheerful to have seen your post. ranboomerchshop I found the most beautiful and fascinating one. I am really extremely glad to visit your post.

      Delete
  43. It’s actually a nice and helpful piece of info. I’m glad that you shared this useful info with us. Please keep us up to date like this. Thanks for sharing.HatsBuddy

    ReplyDelete
  44. I enjoyed reading your blog and it contains a lot of information. Visit our store MensWalletMaker for a selection of the best wallet.

    ReplyDelete
  45. This is a really too good post. This article gives truly quality and helpful information. I’m definitely going to look into it. Really very useful topic info is provided here. Thank you so much buddy and Keep up the good work.
    https://essentialshoodies.co.uk/

    ReplyDelete
  46. Peshawari chappal and Kaptaan Chappal are trending now a days. We deliver nation-wide. We offer different designs of Peshawari chappal to look decent and stylish, an ultimate desire! Scroll through our latest collection of peshawari chappal and order it at your finger tips.

    ReplyDelete
  47. Thanks for sharing such beautiful information with us. We hope you will share some more information about louisleather. We really enjoy your blog & content.

    ReplyDelete
  48. I read this article. I think you put a great deal of exertion to make this article.Designerbeltsmaker

    ReplyDelete
  49. I'm not tired of reading at all. Everything you have written is so elaborate. I really like it. Thanks for the great article. ดาวน์โหลด bet game

    ReplyDelete
  50. I read this article. I think you put a great deal of exertion to make this article.DenimJacketsMaker

    ReplyDelete
  51. Keep up the exceptional piece of work, I read generally a couple of posts on this website page and I imagine that chromeheartsoutfit your blog is genuinely stunning and holds gatherings of amazing data.

    ReplyDelete
  52. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for.RoyalRoot

    ReplyDelete
    Replies
    1. This Site give information from another people karljacobsmerch.com very well I am impress its information an information is very nice.

      Delete
  53. This is a very nice blog and learned more knowledge to read this post thanks for sharing this informative post.silverchainmaker

    ReplyDelete
  54. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for mensbraceletsmaker.

    ReplyDelete
  55. Visit chrome hearts to get high quality hoodies, tees and jackets at best price and get fastest shipping services across world

    ReplyDelete
  56. I feel extremely cheerful to have seen your post. I found the most beautiful and fascinating one. I am really extremely glad to visit your post.
    the weeknd merch shop

    ReplyDelete
  57. Denim Jacket stylish designs and luxurious materials, denimjacketmaker is a favorite among celebrities and everyday people alike.

    ReplyDelete
  58. Very important and wonderful post here. This post is very helpful for every visitor. I hope you will soon share your next post about this discussion.logachiThanks for sharing and keep sharing

    ReplyDelete
  59. It’s actually a nice and helpful piece of info. I’m glad that you shared this useful info with us. Please keep us up to date like this. Thanks for sharing.Edcationalsolutionpk

    ReplyDelete
  60. Visit chrome hearts to get high-quality hoodies, tees, and jackets at the best price and get the fastest shipping services across the world

    ReplyDelete
  61. Thanks for the information you have shared.
    If you are in search of a mens clothing store from where you can buy best printed shirts for men
    trendy shirts for men,
    mens co-ord sets,
    shirts cut piece online.

    ReplyDelete

  62. I feel extremely cheerful to have seen your post. I found the most beautiful and fascinating one. I am really extremely glad to visit your post.
    thrasherhoodies

    ReplyDelete
  63. https://www.chromeheartofficial.shop/
    Chrome Hearts Hoodies Limited Stock & all Colours of Hoodies are Available in our Chrome Hearts store. Free Shipping & Fastest delivery worldwide

    ReplyDelete
  64. Chrome Hearts Hoodies Limited Stock & all Colors of Hoodies are Available in our Chrome Hearts store. Free Shipping & Fastest delivery worldwide.
    chromehearthoodie

    ReplyDelete
  65. Surprise to read your article. Its very informative and interesting. Hope you will write more amazing.
    Discover the complete Chrome Hearts Sweatshirts , Sweatshirts collection on the official website Free worldwide shipping Fastest delivery.
    Chrome Hearts Sweatshirts

    ReplyDelete
  66. click hereThe information given in this blog is very nice.

    ReplyDelete
  67. Nikehoodie
    nikehoodsgreat website you have got here.

    ReplyDelete

  68. Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man,Keep it up.
    Ice Cream Cone Sleeves

    ReplyDelete
  69. This is a very nice blog and learned more knowledge to read this post thanks for sharing this informative post.
    https://ericemanuelshop.com/eric-emanuel-shorts/

    ReplyDelete
  70. I read this article. octobersveryownshop.com I think you put a great deal of exertion to make this article.

    ReplyDelete
  71. it's really nice and meanful. it's really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull informatiocharlidameliomerch.shop

    ReplyDelete
  72. Very important and wonderful post here. This post is very helpful for every visitor. I hope you will soon share your next post about this discussionshop here

    ReplyDelete
  73. Continue to send in more valuable and astonishing information on your blog so that we enjoy it.superhoodshop.com

    ReplyDelete
  74. I never stop myself to express something about your nice work. yeezygapshop You're working really hard.

    ReplyDelete
  75. I read this article.pufferjacketshop I think You put a great deal of exertion to make this article.

    ReplyDelete
  76. shopgallerydept Buy best gallery dept clothing from our official store.

    ReplyDelete