PHP CRUD | Create, Read, Update, Delete, View using PHP MySql using Bootstrap 5 | PHP CRUD in 2022

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys in this video we are going to learn how to make a crowd application in php for the beginners who don't know what is crud crud is nothing but create read edit update and delete okay so guys now let's get started so guys to get started with php crowd you'll require a server where i'm using a xampp server and a browser to view the output and one editor to code so i'm using a vs code editor guys so let's now let's get started so in our php 2022 i'm going to create one index dot php file to view the student and one more file for the student create dot php file so now guys let's get back to the index page and type hello over here and check the output here refresh refresh and we got this okay so now guys let's use bootstrap to design the user interface for the php card let's get back and go to getbootstrap.com and now guys we are going to use a bootstrap 5 version over here yep and let's get started so here we get a starter template let's copy that and you can just paste that in the index page all done we don't require some of the code so just remove that skip those code so now guys first step we will be creating the cred i mean insert data right so let's go to that student create.php file and paste the bootstrap code all set and now here we will begin with the container part container row column md12 where card and card header let me just zoom it and here i'm going to add in h4 tag as student add or create and one a button for going back and let's design this button guys over here button button danger float end okay so we'll be going to index.php on the href all set and start with the card body and now let's begin with the form insert so form where here in the form you are going to set the method as post so method is equal to post all done now let's add the label where you will tell student name input field dot form hyphen control all set yep guys and now we are going to keep this in one div tag margin bottom three all set so same way we are going to create more three fields this is for name copy and paste paste paste student name email email and the name type will be email and here coming to phone number let's add phone make it to small and finally the course okay so here we had course all done guys and now we are going to create a button so margin bottom three with the button here we tell save student so button name and let's design this button with the help of bootstrap class by setting primary and now let's give the button type as submit sub mit submit and the button name as save underscore student so the button name is save student so this now when we click on this save student it goes to the action and we want to go to code.php file with the method post and this method will this method post will take all your input fields and send the data to code.php right so now guys before doing the code let's check the output okay let's go to student hyphen create dot php here we see the form let me just give a margin top five refresh here you get so now guys when we save it should save in a database so for that let's go to our database here i have created one simple database guys that is blog and let's create the table so we are going to use students data right let's add creator students table with column 5 let's go and here first column id int 11 primary go and it should be auto increment ai okay here you need to select ai okay and then coming to name your var care 255 or else you can keep 191 s max and next was email where care 191 and next is phone where care 191 and finally is course where care 191 okay done let's save perfect so my student table is created okay now guys you can get back to your form and start begin with the code so guys now let's create the code.php as we are submitting it we are going to code.php file let's create that code.php first step we need to give a database connection let's open the php and give the database connection as simple guys uh let's create one db con dot php so here we will create the database connection and we will include it so dollar co n is equal to mysqli underscore connect function where insert this localhost username root password is null and my database is blog all set if it's not connected you can give not dollar con just add die function and insert this connection failed if you want to check you can just add mysqli connect error connect error function all done guys and now let's call this encode.php so let's require dbcon dot php file we need to require it and now guys you can begin with the coding part so first step we are going to check the button is clicked or not with the help of a set function so dollar underscore post that button is coming with the method post so we are using post method okay now you can copy the button name save student it is coming from the method post so that's why i'm using dollar underscore method post okay so guys now let's get all the input fields like name email phone and course so all the details we require dollar name is equal to mysqli real escape string function and in first parameter we are going to send the connection and second you are going to send your input dollar underscore post of your name okay so what name it is this input field name email phone and course you have to take let's copy and paste name email ctrl d phone and again control d and course all set guys so we have taken the input and now let's write the query so query is equal to where you are going to insert into table name so my table name is students so here we get the students right so let's copy the student table name copy this students where value i mean column fields and the values so my column fields are nothing but you can just check it out over here id is auto increment so not to worry about that just ignore that here you are going to add and just give comma just add comma that's it and now guys inside this value you are going to add all the variables so let me bring down and dollar name so this dollar name you are not going to directly print you are going to give in single quotes okay so make sure you are giving or else your data will not be saved dollar email dollar phone dollar course all done so these four inputs and now let's run this query so dollar query underscore run is equal to with the help of mysqli query function okay so you are using mask like query to execute this above state command okay and first in this your database connection and then your query so now guys let's run this i mean once executed this query will tell inserted else it's not inserted okay so you can check with the if condition if this query is true then you will tell uh it's inserted and redirect back to which page will get back to this student create dot php page itself so guys for that getting back to page you can use header function with double quotes where i'll add capital l and tell as location and the page name student create okay and after this use the exact function to directly exit out the code so guys before uh going to the page you can add a session to show the message like inserted successfully all done and inside this you will tell message if you are using a session make sure you are starting the session at top of your page session start and now let's add the else part else here we copy the same code and paste here and tell student not created okay and then we need to show this message in the student create.php file so let's get back and if you're using a session then you have to copy this and start in your create page also at top of your page so let's begin the session over here open php close php paste session started and now you can display the message guys so for that let's go to getbootstrap.com and check with alert here you go so let's use this this missing part and here you see when we click on this into it goes so you can copy this code and paste that so you can paste in this container itself and directly paste the code so now guys let's check this output once so before that let me change this message hey and i am message let's get back here you refresh here you get a message okay so guys let's keep this message code message code in separate file and include it just include that so you can add message.php here we cut and paste all set so now guys uh before this you are going to check if any messages are set or not add a if condition over here so first you will get to check with the session dollar underscore session if any messages are set or not me ssag message okay and close the if condition open php close php you can use the another method like and if and here if you're using this type of code then you have to add a colon in your if condition all done let me zoom it more so you have to add a colon over here and then only you can end if perfect so now guys copy this session and you can just print that over here php open is equal to echo and print the message so once the message is printed now you have to unset the message like unset function and now you are going to delete the message okay once shown the second time it should not show so let's save that and now you can get back to your student create and include that file php open and php close your include function where you're going to tell message dot php file okay so this message dot php file i'm calling inside it perfect so now guys let's begin with inserting data so student name satyam here satyam phone some random phone number bca and now guys let's save click to save student created successfully and now let's check in our database let's get back refresh here you see perfect so the code was executed successfully without any errors now let me just add one more record with prakash with veda gmail.com where the phone number and bca here we create save student again student created successfully browse the second let me refresh the message has gone now let me add omrakash home at gmail.com so phone number bca save student created successfully perfect all set guys and now let's begin with fetching those records so let's get back to our index.php file and fetch all these inserted datas okay yep let's go to our index.php file and begin with the code so guys first step we are going to create the container container row column md12 and then card card header h4 where student detail here we are adding a tag and add students here we design the class just add the button button primary until float end okay and now i saw you'll be redirected to student hyphen create dot php all set and now guys let's begin with card body card hyphen body now let's check the output first refresh here you see and if you click on add student you get to add student page back now guys let's design the table table dot table table head table row table heading th so in table heading here we are going to fetch the students data so you can add student name then phone course so before the phone it was an email so let's add that email part and one table heading with action where we will use edit and delete part so now guys begin with table body and now again table row and here table data comes so guys here we are going to use all the data of students so before that let me add one class inside this bordered and table striped save now guys let's check the output here you see the table okay now let's fetch the record so first step guys we have to include our database so at top of your page you can go and open php close php require your database and require dbcon dot php file which file we have already created it db con dot now database included now guys you can start the code over here so first step open php close php dollar query is equal to select star from the table name that is students so which students here your students table students okay so all the record we are fetching it now execute this query query run with the help of mysqli underscore query function so first parameter data connection and your query all set so now guys let's check if there is any record or not by counting the row using if condition will check mysqli number of rows function if it is greater than zero then you are going to show the record else you will tell no record found oops we need to show in a echo h5 open and close and here you can mention no record found so guys now if record found then you are going to let me minimize this if the record found you are going to loop it so for use for each loop open and close here inside this if you have record you are going to take dollar query as your student data right so let's add student variable here and then you are going to echo the student of what name so i mean this name is nothing but your database column field name okay so let me refresh here you get all the records so now guys let's uh implement in the table so let's comment this ctrl forward slash and divide the php okay to use html so here we cut this table row and directly paste inside this php and now guys ctrl x and now here we are going to open php echo and close php directly print it that's it so same process with the id and email phone and one course and let's remove this not required now guys you can check your output so refresh here you see all the records perfect and now guys at the action we need one edit and delete button yep let's add the td once again and a tag with the edit and one more with the delete so let's design it button button danger button sm now you can paste for this also and here you can tell as success save refresh here you get all the edit and delete button so now guys uh let's uh view the record also so you can just tell view data so here you add info okay perfect so now guys let me just add one more record and show you like varun govind varun gmail.com some random phone number and bca let's save student student created successfully let's get back go back and here you see what i'm going this has come so guys now let's begin with edit data okay so when we click on this edit button it should view the detail so guys we are going to use the same add form for editing the data also yep let's get back now first step we will go to our edit button so at this edit button let's tell student hyphen edit dot php with the parameter id is equal to php open and echo close and here you can add the student id so let's take the student id and pass that and now let's click on that edit button refresh here you click on edit button on id 2 here you see you get the id 2 perfect so now guys let's create the page for this now create a new page student edit dot php and the same form for the create part okay so let's copy all the things and here you paste all set so now guys um let me change the title first student edit here you add make it as edit and back will be the same button and all the inputs going to be same okay and now button name we are going to change it so instead of save student let's make it to update student update student update let me bring it down all fine guys now let's check the output first here you refresh you get the form so now guys with the help of this id you are going to fetch all the records and display in this input fields yep let's get back to the card body and before this form starts you are going to check the parameter id is present or not so guys now to check the this id is present or not you can use a get method like dollar underscore get method id if it is set or not so you can use the asset function also here if a set of id is present then you are going in and let's write the query to get the data with the help of id select all the fields from students table so this students is nothing but the database name here use a where condition id is equal to single quotes okay so here we are going to tell student underscore id so this student id we will create a variable and pass with this get method so paste here all done so guys let me just echo and show you this first let's comment the below query and echo and show you the output refresh here you find that id number two if you change this id it will be one and if you just miss out this it is nothing going to show okay done so let's add that id number two as we have clicked on it now let's execute this query so guys before sending this to database you need to protect it so you can use real escape string okay function where database connection and the get method id so it protects your sql injection so now guys if you are using a database connection you need to require it so let's include the database connection require dbcon.php file this database connection file i have included it and now let's get back below and let's execute this query so dollar query run is equal to with the help of mysqli query function where you are going to tell dollar connection and your query perfect and now guys you need to check if any record exist or not in this so we will use the mysqli number of rows function to check the queries present or not perfect so if there doesn't exist any record on that id you can show no such id found done guys and now if the record found you are going to get the data so like it's a student data student is equal to with the help of mysqli fetch array so are a function here you are going to pass your dollar query run executed query perfect so once you found all the students record is going to be present here so let me just show you the records before we check the output in the input field print r refresh here you see that data has come so id number two is with prakash and is gmail everything it's coming perfect so now guys uh let me remove this and now you can divide this open close php and open php let's divide this and here inside this we are going to add the form tag let me minimize this form first ctrl x the code push the form and paste below so guys no confusion it's very clear it's inside your if condition that's it and now you can echo your data let's copy this variable student and here in the value open and echo student of name all set copy this paste here for the email another phone course and now check the output refresh here you get the data let's get back and i'm going to edit with id number four edit here you see what data comes and now guys let's update this okay here we get back update student it's going to the form code dot php with the method post by taking all the input fields so here we copy the button name update student and move to code.php let's go at the top and add if condition so if check with the set function whether the button is clicked or not which is coming in the method post and now guys get all the input fields so we can just copy that same and paste here input fields all the input fields are going to be same on the update student also and now guys you can write the query is equal to inside double quotes where update set the table name students and the column fields so my column field is name is equal to inside single quotes you can dollar name comma next is email is equal to single quotes dollar email and the phone so this phone is nothing but your database column field okay this name email phone is your database column field names okay and the data variable is from the input phone you are going to paste here and then finally the course course is equal to inside single quotes you are going to update the course record i mean input course and now guys main part at the where condition id id is equal to single quotes here you are going to tell dollar student underscore id so from where are you getting this id so you need to get from your form itself all set so you can just copy this part and tell student id so you need to send with the method post let's go back to our edit student and add the form below itself create a input name student id and give the value inside this value echo and you get this id write student id just pass this id either you directly pass it or else you can get from this also student okay student of id so anyhow it depends on you let's see the output first for this refresh here you get id number four which is present in your url perfect so we don't need to show this make it hidden refresh gone and now guys we are going to get the student id to the code.php file with the method post on update student here you get now you got the student id and you are going to update the record all set let me bring this down done guys and now let's execute this query dollar query underscore run with the help of mysqli query function where you are going to give the database connection first and second you are going to execute your query so this query you will be running it and database is already included over here all set now guys you can check if the condition is successfully executed we're going to check in if else condition else here we need to paste the code let's copy from below paste paste so guys let's change the message updated successfully and get back to where edit dot php or on the index page on this main page okay on this page we'll get back so guys we will get back to main page itself at index.php once we updated let's paste here student not updated okay so now guys for this message we have already started the session and we are going to index.php so let's move to index.php and display the message so guys we have already written the code for displaying the message let's use that over here open php close php user include function at here message.php okay so message.php message.php displayed and if you are using message make sure your session is started session underscore start function that's it so now let's refresh let me add a margin top four okay so now guys let's edit so i'm going to edit on prakash to om sharma update okay mysql i exception you have an error okay i think the query is jumbled over here let's get back to the code.php you have to paste that student update students table and set all these fields okay let's get back let's refresh once paste and now let me update student here you see student updated successfully and here you see the record on prakash om sharma it is so now guys i'm going to change this whole detail let's add vnode so winod and here i change this phone number to 2 and mca let's update student here you see student updated and all the record is updated over here so guys we have successfully completed with update students 2 and now let's begin with view student so same concept goes like edit part itself okay so you can just copy and paste the same code so let's copy and paste it same file you are not going to change anything just rename this file and make it as view student.php okay and now you can get back to your index page and go to the view button here at this view button you are going to copy the same code and paste here and instead of edit dot php let's make it to view dot php and passing the id all done let's go to view.php and make some changes so now you have copied the same edit code and pasted in the view code and we don't require message guys so just remove that message and we don't require session start if required you can keep or else not required but in this case it's not required and now guys let's move below and change the header student view details and coming back to the input field so instead of input you can directly use a p tag or a h4 tag to display the data so guys i am going to use a p tag over here and if you require the same input design you can just copy this class okay copy this class and paste that and take this value and print that that's it so now guys you can just remove all the input fields and we don't require a id over here remove it let's paste here email and just remove the input copy and paste for the phone remove it for the student course and we don't require a button remove that button save now guys you can remove this form also we don't require any time remove the form save and now you can check the output refresh uh this is edit let's get back and click on view button so i'm going to view this window data view and here you see all the details so guys it's looking like an input box because i have used the class called form control if you just remove let me remove and show you this the details have come over here okay perfect so let's save so this is how we view the details and get back and now guys at last we are going to delete the record yep let's move to index.php at the delete button so guys there are many method to delete the record like on the form submit or confirm and delete the record so first we will be learning about form submit delete okay so let's use the form tag and here we tell we go to code.php and we want to go with the method post so method which is post and in and now let's copy this button and paste here and now guys i am going to change this from a tag to button tag so bu tton button and make this href to type where submit and give the name for this button delete student and this button value will be value value open and close and the button value dollar student of id so here you are going to send the student id only in the button itself you are not going to create separate input field to send that id directly will send it from the student button itself that's it guys and now you can just add one class over here to not break i mean why i'm adding this class let me show you refresh here you see that delete button has come below so for that case i will add just d in line so d inline means display inline refresh here you see it's coming in one line perfect so now guys you can get back let's copy this with the taking the value to code.php with the method post let's go to code.php at the top now add if condition here you check with the asset function dollar underscore post of your delete button and inside this delete button only we have a student id so student id is equal to with the method post we have to get okay so here you get the two net id we need to secure this id i mean filter that all unwanted data that comes under sql injection so add the connection and your method post so now guys let's use the query to delete the record delete from the table name students where id is equal to this so this in the case like student id so this variable you will take it and delete that record now let's execute this line so let's run this query with the help of mysqli query function so mask like query function where you will send the database connection and then your above query perfect so now guys you need to check whether this query is executed or not with the help of if condition you can just check this query run if it is success then you are going to show else you are going to display not deleted so now guys let me just copy the code from below to display and redirect back so to display the message student deleted successfully here we copy and paste again student not deleted okay all done and we are going back to index.php so user session obviously you have to start it now guys let's uh get back to our index and we have already written the code to display the student message so here is our message and session is already started perfect so now guys let's uh test this output also refresh and here let me add one dummy data dummy dummy gmail.com so some random phone number bca save student student created successfully let's get back and now guys let me show you the record in database so here we have the dummy details right so now i'm going to delete this dummy detail let's click on delete here you see student deleted successfully we have got the message and let's check in that database too perfect so guys we have successfully completed with this delete part so now guys let's get back to our code and one more thing i'm going to teach you guys about like not repeating this contents like if you are using this right header and the body script it's included in every create edit view pages right so let's not repeat those things so for that you can just use this or else you can move out directly so let me create one includes folder and inside this includes folder i am going to create one header dot php and a footer dot php part that's it so now guys i am going to index page here you move just take this part out and paste in your header.php and this header.php will be included in your index okay as user include function and include that header so let's go to includes folder and call your header dot php so same condition goes with the footer part let me just paste here and tell footer.php and let's remove this and paste inside the footer.php part that's it so now guys you can see no difference okay so if you refresh here you get all the designs too okay so this is one good practice to keep your code clean now let's copy this header.php and change in your create2 so let me just show you one output let's go to add student here you see the form design and if you just remove this and now if you see like there is no design so let's add that header dot php which we have pasted that code okay to include it and here you add your footer.php now refresh here you get same design okay that's it guys so you can paste for the edit and back to footer part rooter.php and same for the view all set guys so now you can refresh and check with your view working fine edit working fine so guys we have completed the crud application using php mysql with bootstrap so thank you for watching this video please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 185,579
Rating: undefined out of 5
Keywords: fundaofwebit, php crud using bootstarp 5, php crud, php crud tutorial, php crud source code, php crud using mysql, php mysql crud, crud in php mysql using bootstrap, bootstrap 5 crud operation in php, php crud in bootstrap 5, insert data in php, php crud edit update data, php crud with ajax, php crud operation using mysqli, crud operation in php mysql, php crud operation, php tutorial for beginners full, php project, php bootstrap tutorial
Id: ExW0bYNMTlo
Channel Id: undefined
Length: 47min 55sec (2875 seconds)
Published: Fri Apr 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.