PHP PDO CRUD Tutorial - How to Insert, Fetch, Edit & Update, Delete data using PDO in PHP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to my channel so guys in this video we are going to start with the new topic that is php pdo card okay so we will be doing a cloud application using pdo that is php data object so guys now let's get started so first we are going to insert the data into our mysql database okay so let's get started so let's go to our editor here you see guys that i have just set up the template with the bootstrap and here's the output for this at index.php that's it so now let me just design one simple card yep card card header where inside this card header add this and inside this h2 tag i want a tag add student so we will take an example of student for inserting the record where button primary and here the form name we will get create as student at dot php let's copy this and here we create one form i'm in file and let's ctrl a ctrl c copy and paste fully here and let's tell insert data into database using php pdo okay so same title will be copied and pasted here and this will be the back button let's make it to danger and here we move as index.php so guys now let's design with the card body part and let's see the output for this okay so yep let's refresh cool so design is perfect guys let's make this button to right side so move to your index dot php and add the design float end okay refresh now let's click on this you can see student add dot php here is the form same class you are going to copy and paste in your add student done and now guys you need to create the form over here so let's begin form where method we are going to accept as post and let's see the output now for this okay so now let's create the input fields so guys i have created a database where you can see that a table name student okay and inside the student where i have few fields in it where id is the auto increment and primary key okay and here is the full name email phone and course done so let's get back to the browse and now let's implement the input fields so first margin bottom three and the label i don't require for over here let's stem full name and the input field that is form hyphen control where this input field name will be full name that's it now let's copy this paste here let me just minimize and zoom it okay the next was email email and another was phone let's make this input field phone and final one course okay so let's give the course done and simple guys one button we require it let's add that button but on button where save student and let's design this button with the help of bootstrap class primary and the button type we are going to keep as submit and this button name will be as save student button okay as simple so now guys let's uh go to the action code.php and create a file so when you click on the save student it goes to code.php with the method of post and this method post will take all your input field data so yep let's create a file code.php so guys before working in this you let's check the output once refresh cool so these are our input fields so if you want to make this small card you can just go here and let's give eight here and refresh perfect so now guys let's get back and go to the code.php file so now guys first step let's open php and close php start with the if condition where inside this is set function you are going to check with the post method because you are getting the form using post method now you need to copy that button name let me just minimize okay let's copy this the button name here it comes and now guys all your input fields so one is full name is equal to dollar underscore post method of your input field name okay so what input field name is that full name email phone and course okay full name so same way we have to paste for email phone and course done so now guys we have to insert this into our database so for that guys you have to write the query so before writing the query you need to give your database connection so let's include the database include where db con dot php file so let's create the file with the name db con that is database connection database connection dot php and inside this open php and close php now let's give the server name is equal to will be localhost dollar username is equal to that is root and password is equal to null right now and the database is database name what is my database name here we find php tutorials so let's paste here done and now guys let's use try catch and inside this pdo exception so let's type video exception dollar e okay e this is exception now let's understand and whenever it goes the connection let's tell connection failed okay and get your get message so this is a default function to show all kind of errors okay done and here now let's begin with database connection so c-o-n-n is equal to connection is equal to pdo function and inside this you are going to set up with your connection guys so first your host and then your database name so that is connecting with mysql host is equal to your dollar server name okay so this is server name semicolon and now db name is equal to your database name all set so this is our host on database now guys you can give the username and then your dollar password all set so this is how we connect our database oops here we miss that semicolon and now guys you need to let's give set attribute attribute function where pdo utter error mode okay attr underscore err mode error mode where pdo you are going to error mode exceptions also you will get so let's start pdo column colon exception i mean error more exceptions that's it done guys and you can just echo here that is connected successfully if you want to see you can just see or else let's comment and keep right now now guys our database connection is set up let's go back to our db i mean code.php and let's write the code so our query is equal to where we will tell insert into table name so that is students your column fields and then your values so guys now for the column fields let me just copy that okay full name email phone and course copy and paste here done let me just minimize and let's take out the spaces give comma and just give the space and now guys let's here colon full name comma colon email colon phone command colon your course all set so guys now let's run this query dollar query underscore run is equal to where you will be preparing it so dollar con your database connection where we are going to prepare this above statement or query so let's copy this and paste here so what is this con is nothing but your database connection okay so this one so you are calling this and it's getting connected so guys after preparing this query let's bind our values into this so for that guys let's create a variable called data in an array format where you're going to bind that values so this is the variable full name we are getting from the input field full name okay and where you are going to put that is over here so just copy this with the colon okay don't miss out that colon and paste it all set so same way you're going to prepare for your email phone and course and these input fields that you're getting from your input form okay let's copy and paste here so now guys we have binded the values and now let's execute inside this so let's copy this query run and dollar query run of xe execute function inside this dollar data so why dollar data you are passing all this inside this and now you are going to execute it i mean this is your query executed is equal to if this query is executed you are going to show the message else you will tell not inserted so now guys let's use a session to show the message message is equal to where we will tell inserted successfully and let's redirect back to location location colon with index dot php exit so you need to copy this once again and paste for the not inserted okay done and if you are using session make sure you are starting the session at the top of your page session start so once it executes this guys then you are going to uh your index.php and we want to show the message of this right so let's copy this and go to index.php okay so this is index.php here so let's open php close php if colon where we will check is set if this message is set something session is set so you how you are going to check let's call the session at the top of the page so let's call the session underscore start done so our session is started and let's end the php i mean end the if condition and if and we have to show that in a h5 tag something like in the alert alert alert success and here we are going to echo dollar session of message okay so this will show our output yep let's get back to the code.php all set guys so now let's check and execute let's refresh so now here database is empty right now let's insert the data with prakash with gbl.com some phone number bca and let's save student it successfully so now let's check the database perfect so guys our data is inserted into our database so now let's try to add one more record on prakash home gmail.com where some random digit phone number bca course save here we go inserted successfully let's browse and see cool so guys our data is saved so guys in this video we have successfully learned how to insert the data into database using php pdo and now guys in this we are going to learn how to fetch the data from our sql database in html table so right now let's get started so first let's move to our editor and let's go to our index.php file so in this index.php file i want to show the output okay so here uh why it's not going this so guys in previous video like we have shown this message right so you just need to unset that let me unset it after showing the session message we need to answer that session message so just unset it that's it not to worry and refresh okay it's gone so guys now let's begin with the fetching of data card body table dot table where let's give table ordered table striped and then table head guys after table head table row table heading where id then full name email phone and finally one course that's it so now t body and over here table row so inside this table data will be fetching all our records so let's see the output over here code so here is the our output of table and let's fetch the data from our database so guys let's begin the query so first up guys we need to include our database okay so let's include the database over here include function where insert this db con dot php file so here is our database connection guys so you just need to create this code and we are including that done so let's get back to our table body and begin with the php so here let's write the query is equal to select all the fields from which table students table we have inserted the records okay so guys now let's prepare the statement state meant is equal to with the help of database connection let's prepare the above query prepare function dollar query all set so after preparing your query let's execute that query guys so dollar statement of execute and after this let's fetch the data so whatever we fetch the data let's keep in that result your executed statement executed statement of fetch all function okay now you can just check in if condition if at least one record is there means you are going to check else you will show no record found here we divide the code open and close php where table row table row table data no record font all set and here we add call span is equal to number of headings so four plus one five you can just add that five so it will show in a correct format so now guys if the result is found then you are going to loop the result and fetch the data so let's take this variable called result and here we fetch as dollar item okay so whatever item we are item or row let's take it row itself so it gives more clear idea so now guys let's close php and open php that means dividing it let's cut this and paste here all done and now you can just copy this row of open php and close php row of id so now if you want to check the output let's go and here we go so one and two record which is id is coming let's fetch the full name so let's copy and paste full name here we go with email so let's check with this two records refresh perfect so full name email the data has come guys so now uh i wanted to show you one more thing like this is the method of associated array data we are getting it okay so let me just show you pdo colon colon where fetch associate okay so this is this will give your associated data and you can check the output you are getting the same data okay there is no changes so now let's let me just fetch this data in a different format that i mean in an object so let's control x and let's keep for the reference and here we paste this and just make this to fetch underscore obj that means object wise okay and here you will get the error something like cannot use an object of type so you have to view in a different format so i mean in object format you have to view all set so let me just select this ctrl x and remove paste done so refresh cool so you are getting your data so as i just wanted to share this you can fetch an object rf fetch associated vice also so let me just get the other two fields phone and course now refresh done guys so let's get back and one more thing i wanted to show that the format of fetching instead of writing here you can just give this like statement okay so the statement whatever you are executed that statement you are going to set fetch mode okay and inside this you are going to pass your fetch mode data so let's control x and paste here and the same format for this also you can call in this okay so now you can just check the output refresh here the same so if you want to fetch in associated format then you have to change this method okay so let me just show you that to fetch in this associated type let's select ctrl d here we add paste so yep you get the data so guys these are the few methods how you can display your data okay so let's display in a fetch i mean object method itself so let's type obj and now here we refresh code so guys in this we have learnt about how to fetch the record from our database in php data object okay so now we are going to edit this data and update the data into a database using pdo okay so let's get started so let's add one column with the name edit and one simple button so let's get back and here is our table guys so this is our table head and let's add one edit button and here we go with the button option let's add a tag and mention as edit let's design this button button button [Music] primary and let's go to student hyphen edit dot php file all set so with this i am going to pass one parameter called id is equal to where the id is going to be the row of id that means fetched id so let's pass here all set so now guys let's check the output for this so refresh okay we get the edit button now let's click on this here you get your student edit dot php with the parameter id and its value one okay so same concept with goes with the second option also okay so now guys let's create the file with the name student edit.php copy and yes student edit dot php so now you have to create a form right so let's check out with the add student form so here all the same data guys so we are going to use the same student form for edit and update okay so let's go to add dot php file copy this and paste in our student edit dot php file and let's change the title edit and update data into database using php pdo edit and update data into database using phpd video so now let's click on edit cool so now guys let's fetch the record from this with the help of this id let's get back let me just minimize this so guys this is our form and first step we have to include the database connection so let's include the database connection at the top of the page here we open php and close php include function where db con dot php file so here is our database connection so you can just watch it out and let's get back to student edit so guys now let's write the query over here before the form starts okay open php close php let's check first with if condition and set with the set function dollar underscore get method okay so you are going to get what your id okay so let's copy that parameter id which you have passed from the index page this index page okay so that id you're getting and inside this id you have the value as you can see that it has one so let's store that in a variable called student underscore id is equal to with the help of get method you are going to you are going to tell id so whatever data is there it will come inside this and now guys so now guys let's type the query is equal to where we'll tell select star from the table name students where id is equal to your binded student id okay let me just keep a shortcut student id okay so you don't get confused with this and this done so now guys let's prepare this statement so you can dollar statement is equal to dollar database connection with the prepare function where dollar query all set so you are setting the query over here so after setting it we have to execute this so let's execute execute the query where dollar data so inside this data what data you are going to send let me just create it outside here we will be passing this we are going to bind it so colon student id inside that what data you are going to send this student id variable okay the parameter value done and this you are passing inside this all set so there is no confusion let me just align it in one line itself here we go and now guys let's fetch the record so this is only one record guys okay so we are going to take one and you can put also like limit one and dollar statement of only fetch data okay fetch function then let's get that data in a either object method or your fetch associate array method okay so let's switch in object wise guys fetch underscore obj object if you want to get in associate also you can just i have let me paste off that a ssoc so now let's start dollar result is equal to and in this result you will get final data let's paste that in the value attribute php open echo and close php and your dollar result object of where full name full name so this full name is nothing but your database column field name okay here done so let's check the output guys for this first let's move refresh perfect so we are getting that id one with prakash now let's check with the id two edit here we go two and here you get the cache data code so now guys you can i will just show you this what is this method of associate and if you put this fetch associated data you have to change it in a array format okay to fetch the record so there is no difference okay so you need to just change that and let me just keep in this in object format so if you are using associate then you have to view in array format okay so yep let's copy this and paste for rest of the fields and next column was email the next column is phone and the final is course save and now guys let's check the output once perfect so now guys let's create one more input field here for the id okay so name is equal to will be student underscore id and insert this value is equal to where open php echo and close the result of id okay so let's check the output so whatever here it's there that only we are storing in an input field let's make this as hidden done so now you can refresh that input field is hidden so now guys when you submit the data okay which means update student so now let's tell update student button so when you click this update student and it goes to code.php with the post method and it will take all the input field name with the values so yes guys let's go to code.php and start with the code here let's ignore that so guys now let's begin with if condition where is set function with the method post update student button and the input field names we have to get so which means we are going to get from the edit student student id full name email phone and course okay so let's come here let me just copy from below because we are going to repeat it again okay so all this full name email phone and course are from the input field and one more field is required that is student id let's copy the student id and create a variable called student id let's keep it full itself student id done guys and now you need to start with the query so now guys let's give a try catch and inside this catch let's tell pdo exception video exception dollar e and let's echo this dollar e of get message function set so now inside this let's type the query dollar query is equal to here we update the table name so my table name is students let's copy that and paste here set your table with the column names so my column name is full name i mean this column names okay full name email phone and course let's get back full name is equal to colon the full name comma email is equal to colon email let's call the database column field and here you are going to bind that colon phone course is equal to colon course okay now guys you need to give aware condition id is equal to colon your student underscore id all set so if you want to set a limit you can give limit also that means one limit one and now let's prepare this statement guys so let's tell dollar statement statement is equal to your database connection and where we are going to prepare function your dollar query i mean this above query we are preparing it after preparing it we need to execute so what you are going to while execute you need to send your i mean you have to bind all your variables here let's create a variable data and bind all the variables so this is our binding data from the sql and the data you are going to bind inside this is this values done so same concept for all the input fields your email and your variable email okay then your phone and your input field data form and final course here your variable course done guys so after this one more thing we are missing that is your student id okay do not forget the student id because it will be searching where we have to pass this id what is that student id let's copy this variable and send it done guys so now after this we are going to execute this so let's take that variable statement your prepared statement that statement you are going to execute execute function so what you are going to execute while executing you have to send all your data right your binding binded datas so let's send that dollar data so after execution let's tell this query execute is equal to okay so this is our query execution let's tell this in if condition if query is executed so success else shows like not updated so guys to show the message let me just copy the code from below let's copy this and paste here done and instead of inserted let's still update it successfully not updated and it goes back to index.php where we are using session to show the message okay so if you're using session makes your started and now guys going to index.php let's move index.php where you're showing the message here okay so if you are using session please start the session at top of your page so now guys let's test this code so yep refresh so from om prakash let's make it on dude and let's make it all the phone number two and become let's click on update here we go updated successfully and you can see that name is changed to omdu and here you find the email and okay so the phone number is not changed guys so let me just edit and let's update let's update student okay so phone number is not updating let's see why and let's go to the code dot php add update student okay here we come and data what we have passed in the phone it's email guys okay so by mistake i have left off so let's update that to phone all set so now let's try to edit and update once again so i'm going to keep all the digit as 8 update perfect so guys in this we have successfully completed with how to edit and update the data into database using php pdo okay so guys now in this we will be learning about how to delete the data using php pdo okay so guys now let's get started so now guys let's move to our editor and go to our index page at the table okay so this is our table head and let's add the delete option delete and let's create a table a button table data button here i am going to begin with the form with the method is equal to post and inside this a simple button that will be delete and this let's design this button [Music] button danger and its button type is equal to submit name is equal to delete student so now guys uh we will be fetching the value i mean id student id insert the value attribute open php and close php echo the row of id here you give your data so now guys you will go to code.php so once you click on this delete button you are going to code.php with the post method while you will carry this value also okay so let me just show you the output of this first and then we'll begin with coding okay so this is the delete button so now guys let's move to code.php with the button name delete student so go to code.php now let's begin with the coding part let's check the button is set or not which means clicked or not that is using post method delete student so guys we are going to get the student id with the help of post method so inside what you are having the value inside this delete student so let me just show you let's go to index.php here is the value which has the name of delete student okay so you are getting from that button only student id so now guys let's let's start with the try catch where you can tell the pdo exception video exception dollar e and you can just echo the message if you get any error from that so yep and here you are going to add the query guys so let's type the query is equal to delete from the table name that is students so let me just confirm it yep it's a student students where id is equal to colon your student id okay so now guys let's prepare this statement dollar statement is equal to your database connection so from where this is coming from your database okay here so we have set this and we have included that at the top db con and from there guys we are going to prepare it prepare dollar query your above query which we have written this okay so once prepared let's execute this dollar statement of execute function and inside this what you are going to execute i mean you have to bind the student id so either you create a separate variable and assign or else you can just pass that directly inside this as student id so this is also one method if you're getting confused guys let's keep it out itself like dollar data is equal to we are going to keep this and just pass the dollar data so it will be more clear to you all set so now this once executed you all need to get the result i mean that is your query execution query underscore execute is equal to and if this is success here you get else you will tell not deleted so let's copy and paste the code to show the message let's copy this and paste all done and instead of update let's make it deleted successfully not deleted and it goes back to index.php file okay so when you click on this it come back to index.php so yep guys all done guys now let's get back to check i'm going to delete the row number three let's click delete here you get deleted successfully and your data is not visible so let me just add dummy data so some dummy and let's save here you go so let me just show you that in the database here is a record now let's click to delete delete this deleted successfully let me refresh done let's check into our database here you see that data is deleted so guys we have successfully learned how to delete the data from database using php pdo okay so guys thank you for watching this video guys please subscribe like and share
Info
Channel: Funda Coder
Views: 21,540
Rating: undefined out of 5
Keywords: fundaofwebit, funda coder, how to insert data in php pdo, insert data into database using pdo in php, php do crud application, php pdo crud tutorial, pdo crd in php, php crud application, fetch data in php pdo, fetch data from database using pdo in php, updata data into database using pdo in php, delete data from database in php mysql using pdo, php pdo
Id: toU9pJeM2e0
Channel Id: undefined
Length: 46min 35sec (2795 seconds)
Published: Sat Jan 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.