PHP PDO CRUD using bindparam(): Insert, Read, Edit, Update, Delete data using bindParam() in PHP PDO

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 learn with the php pdo using bind param function guys okay so we'll be creating a thread application so guys we have already seen about that php pdo current but in this series we are going to learn about php pdu using bind panom function crowd application okay so guys basically we are going to insert the data edit update and delete the data using the bind panel function in this php video card so guys now let's get started with it so as we have already created with the database and it's table named as students over here and it has few fields like full name email phone and course and with the id primary key and one integer with the auto increment value so guys now let's create the student form as per this database requirement so guys now let's get started so let's move to our editor and here guys i'm using a bootstrap 5 cdn link to design this user interface and just get started with a simple create button okay column 12 card card header h4 tag php video using bind param function okay grad and let's give a tag where add student class button primary let's create the file name as same student at dot php and let's make this to float end so yep guys now let's give this card body over here all done let's see the output for this refresh cool so nice design guys and now let's click on this add student here you move to student add dot php file let's create a file and create the form student at dot php so basically we require all the design let's control a and copy all the code and paste here so we don't require few things just remove that margin top four here we tell add data let me create this as a back button so let's keep this as index.php page and here guys let's begin with the form design let's type form method is equal to post simple margin bottom a label not required before let's give full name and the input field form hyphen control and this input field name as full name so yep guys let's copy this let's make this as email email id email copy and paste email so now phone number let's make this text and this will be the phone column now final is the course okay all set now guys you need to create one button let's create the button as simple save student let me zoom it and minimize this so yep let's design this button button primary and this button type is equal to submit and the button name will be save student save it guys and now let's check the output perfect so now guys uh let's begin with inserting the data so when you click on submit button it goes to action let's go to code.php file with the method post and take all the input fields so let's create the file name code dot php all set open php close php so guys first you need to include your database connection include where dbcon.php file create the database okay so let me just copy the database connection code where here is the code let's copy this and paste here done so if you want this link guys we have you just go to fund of your bitey and just move to pdo tutorials so you'll find your latest video tutorials updates over here okay so let's get back so now guys here you set up your database name my username is root password is null and database name is php tutorials okay so let me just copy this and paste here all set guys so now guys let's get back to the code dot php and begin with the if condition by checking by using a set function where you're going to check dollar underscore post method of what when you click the submit button save student so let's copy this and go to code dot php save here and now guys let's call the variables so as you can see that full name using post method you are going to get your input field data as given here input field name attribute as full name so same concept goes with the second email then phone and final his course all done so now guys let's execute with the try catch where you can give the pdo exception video exception dollar e and just echo this echo your my error type add and just echo this dollar e get message function so now you can write the query over here so first step let's uh write the query so dollar query is equal to insert into the table name so my table name is students let me just browse this here we get the table name as students where the input field i mean database column field here you need to copy this id i'm not copying it all done and now you can just go on with values question mark space question mark comma space question mark comma space question mark comma space so guys this is the one two three four values okay so now let's prepare this statement dollar statement is equal to your database connection with prepare function dollar your above query query all set so now guys you need to bind all your variables and then execute it so let's bind that let's copy this and bind using pattern function bind param function so first it goes to this group let's add that one comma and then your variable at this first variable what you want to store the full name so let's copy this full name and paste here so same concept with the four fields it goes to second third and fourth so second one field is email so let's take the variable email and paste here the third is phone and the fourth is course guys all done after this you need to execute your statement exe execute function that's it so this is how we use uh our bind panel function in php pdo so now guys let's create the variable as query execution or execute is equal to done i mean the spelling misspelled query execute and check in an if condition okay let's set the message dollar underscore session where we will give a message variable and you can mention added successfully after this let's give the header location where location colon index.php let's use the index function i mean exit function and now you can give an else condition where you have to say that not added okay or something went wrong as per your requirement so yep guys and now you if you are using a session you need to start the session at your top of your page session start function so you're going to index.php with the session you need to start the session in your index.php page also let's paste that all done and let's echo the message so now let's move below and here we will use a if condition open and close php f open and close curly brace so here we are going to check with the set function dollar underscore session of message so if this message is set you are going to echo in a h4 tag like let's concatenate and paste here and let's add a class alert alert success all done after showing the message let's unset it what your you have set the session as a message right so let's unset this done so now guys this is a simple method to show the message and yep let's get back so let's now let's insert the data and let's check it's saving or not so first with prakash veda gmail.com some random phone number bca let's add student here we see added successfully and let's refresh the message has gone now let's check in our database browse cool so you can see that guys our data is stored so let's add once again here we go with mohan kumar mohan gmail.com some phone number bcsa student here we added successfully let's browse perfect so guys in this we have successfully completed with the how to insert the data using pdo with the bind param function okay so yep guys okay so now guys we have seen that how to insert the data and now let's fetch the data which we have stored in our database so now guys let's begin and finish this soon so we have already designed the user interface and yep guys let's continue with creating the table let's give a table let me just zoom it yep and let's design this table using bootstrap class table table hyphen bordered all done let's create the table head that is table row and one table heading which means id next will be full name then email phone and password all done so now guys let's continue with table body tbody then table row and the table data so here we will be fetching all the record so guys now let's begin open php and close php let's write the query query is equal to select star from the table name so table name is students let's copy this paste here let's execute that statement execute the statement dollar database connection of prepare function then your query inside that so this database connection over here let's include that in our index page so you just need to include function where db con dot php file so what database is included now you can execute this dollar column so now guys let's execute this prepared statement e x e c u t e execute function now let's create the result is equal to dollar statement your executed statement from that statement you are going to use fetch function to retrieve the data so by default you will get the fetching format in pdo called fetch our associated data let me just give that over here fetch associated data and now guys let's check with the f condition dollar result if a record is there you are going to show else you will tell no record font something like let's open and divide the php and create the table row table data where you can find this five table headings let's add the call span is equal to five and let's tell no record found perfect so now guys you can if result is found just look that so let's loop using for each loop open and close where dollar result as dollar row so here let's close php open php and yep guys let's begin with table row table data where we will tell php open is equal to and close dollar row of your id same concept full name full name email phone and of course so yep let's uh let me remove this extra thing and now guys let's check the output okay so it's telling it's not the correct method warning it's a warning guys over here illegal string offset oops i think we have should give over here all okay it's just written in one data now let's refresh perfect so it was just searching for the ide okay so now guys we have successfully fetched the data and yep guys so now guys we are going to start with how to edit the data with the bind param function and update those data using bind pattern function so guys now let's get started so let's move to this table design and just add one edit button so we can edit the data using the parameter called id okay so yep guys let's get started so here is our fetched table row data and here is the table heading so first oops it's not a password it's of course as we have pasted in previous video wrongly okay so let's create one edit and the table data with the a tag that will be button called as edit and give a class with button button info and now guys over here let's create the student hyphen edit dot php file question mark id is equal to your fetched row data id okay is equal to as you have echoed over here dollar row of id you just need to echo this also as simple guys and now you can refresh here let's click here we get the output this is our file name and the parameter id with the value 1 so let's get back and create the file name student edit.php copy and create guys done so now guys for to design the form we are going to use the same form which we have used in add student okay so let's copy that add student form so let's get back ctrl c let me just minimize it little here we paste so yep as simple let's edit and update data using video with bind param function in php so this is the form now let's change the button name first update student and the name attribute data as update student okay so let's save and let's see the output edit here perfect so we are on the edit form guys so now let's fetch the data using this parameter id first step let's uh go above the form and write the php code let's begin with the if condition where we will check using a set function dollar underscore get method where if any id is present or not first in the form if id is present you are going to tell yes then come inside else you have to show echo in a h5 tag that no such note id font as per your message area you can just display that so if the id spawn let's store that in a variable called student id so which comes under using get method where id so guys some guys have asked that do we need to use mysql real escape string for protection of sql injection so it's not required guys over here using when you're using the pdf thing okay so because we are going to that bind it so not mandatory yep guys and let's type the query so basically we are fetching the record select all the fields that means start from the table name so my table name is student i mean students where the id column field id and then you are going to give question mark over here okay and we just want to fetch only one id so you can just add a limit also which gives to one so now let's let's execute the above statement so dollar statement is equal to dollar connection of your prepare function with the dollar query so after preparing this query guys now we have to bind the id okay let's use the dollar statement here we use bind param function where you are going to pass at first which just you have to analyze that it's a first record and then pass the data in it so that is student id the student id you have to pass it where and one more thing guys in this you can just initialize that it's a id or what thing data it is so if you're required you can give or else you can it's not mandatory too also okay so let me add a pdo colon colon where parameter will be integer param underscore int here it is so this is also one type of validating that id strongly and now you can execute this statement statement execute done and now guys you need to fetch the data so let's tell your data is equal to dollar statement of fetch so here we are not going to use fetch all as we have seen in previously we are not using fetch all we have to just use fetch because we are going to get single data and that data let's get in a format of associated array fetch associate if you require you can get an object also so which we have learnt in previous series that is php pdo crud link in the description guys so now finally let's check out with this dollar data where you will be sending this value inside your variable okay i mean input fields so let's type the value open it's php and close dollar data of what the column field database column field name full name okay so now guys let's check the output here we refresh okay undefined variable con that means connection this connection guys it's searching for this connection why because we have not included that so let's include the database connection first we missed out connection db con dot php file all set so yep now let's get back refresh here we go so we have got the data so now guys let me just try to remove this id and just give a student edit here you get undefined variable data okay so what for this you just need to add that in a if else condition i'm in this form so let's divide this just need to close php open php and inside this your form will be so let me just control x for this form and paste that code below all set guys so now you not need to worry and now if you try to remove it and just keep student you can see that no id found okay so as per your request you can just set this yep now let's add the remaining values in its input field video and course so let's paste that phone email all done cool guys so our data has come let me just check out with the second perfect so there is no issue now guys let's begin with updating this code yep so when you click on update student it goes to code.php with the method post as default by taking its input field values so guys now let's move with the code.php and create a update student code so let's move to code.php here and yep let's use a if condition to check if the button is clicked or not so the button is clicked using post method and now guys when you update the data you have to get all the input field names so let's copy from below itself which we have seen in inserting time done so now let's use a try catch statement right here you need to pass the pdo exception so pdo exception dollar e and you can just echo that dollar e of get message if you get any kind of error you will be moved over here so now guys a simple let's begin with the query is equal to update the table name so my table name students set the column fields so column field is full name is equal to colon your full name comma then email is equal to colon your email id then your phone is equal to colon your phone number and course is equal to your course okay so these first values are nothing but your database column field names okay so your table column field name and the remaining are you are going to bind them so guys previously we have seen that we have just used a question mark so here we are while updating it's this method okay so course where id is equal to on what id you want to update this statement so you can just add the student id so let's keep in small where limit let's keep it one so update one record at one limit one okay so now guys yes let's prepare this statement so dollar statement is equal to dollar connection prepare where dollar query dollar query and here your database connection makes your database included done so once prepared guys now let's bind all the data into this sql query so download statement your prepared statement with that you are going to bind param so first and your value colon the full name okay so here you are taking this data and pasting here and inside this you are going to get your variable full name that's it all set so same condition moves with the email id and the email from the input field the phone number and the input field value a course and your variable called course so here and now guys one more thing we required that is the id so this id let's paste here id and student id let's take this variable and create at the top and get from the input method student id okay so let's check out in our student edit form and here we have no such input called student id let's create that just add a value is equal to and tell dollar data of student underscore id now let's see guys first the id is visible or not inside this input field let's refresh okay so it has not come let's make it id it's not actually that student id okay because that field is not found so it gave another and let's create a name with the student id so let's copy this input field name and let me make it hidden so yes guys let's copy this and go to your code.php at update student and here you have to paste that so now you will get your student id and that bind that function i mean by the parents so now guys after this let's execute the query query execute is equal to your dollar statement where you will tell execute function that's it so once executed you are going to show in a files condition that updated or not updated let me just copy the code from below to show the success message so yep paste here all set guys updated successfully and not updated all done you are using session make sure session start and going back to index.php file so now guys let's test it okay refresh so instead of mohan kumar let's add just mohan and update the phone number also let's click on update student updated successfully and you can see that from mohamed kumar to mohan it has added so let me just change this uh first record very i'm in data so from veda to home and from with at gmail.com gmail.com update student here we go so all the data is changed guys so guys we have successfully completed with how to update the data using bind param function in php pdo so guys now we are going to learn about how to delete the data using the byte panel function in php pdo okay so guys now let's move to this table design and just add one delete button over here and delete its record okay so yep guys let's get started so first uh let's move to our table design and add a delete option so let me copy and paste and here you are going to tell delete and create a table data with the form so we are going to use a form to delete the record user button delete and let's assign the class let's copy this and paste here and name it as danger and the button type will be keeping as submit and let's give this button name as delete student and its value this button value we are going to fetch the table row of id okay and we are going to code.php with the method is equal to post all done so once you delete student code.php method post value of row of id so yes here we move is that function dollar underscore post method of student delete and then let's get the id so which we have already passed that id in a button itself that is delete student button as simple so you can just check out over here so inside this button only we have sent the value so let's get that all done let's give the try catch catch function the pdo exception dollar e dollar e of get method i mean message let's echo this so yep dollar query is equal to delete from the table name students where id is equal to question mark and let's give a limit one okay so you have to delete one at a time one row so now guys let's prepare this query as statement is equal to your database connection with the repair function dollar query once executed let's bind the variables dollar statement of bind paren function where first value comma dollar student id student id and inside this let's specify that it's integer itself so pdo colon colon your pattern is integer value all done execute this query execute is equal to dollar statement of execute function and now you can check with the if else condition to it's deleted or not paste here let's tell deleted successfully not deleted okay done and now guys let's check the output here we refresh and yep guys let me just add one dummy data so dummy and yes save student add it successfully and now guys let me just delete perfect deleted successfully let me just delete the second record so you can find deleted successfully okay here we refresh and you will see that it's deleted so as we have successfully completed about the php video using bind pattern function card application so guys in this video that's it thank you for watching this video please subscribe like and share
Info
Channel: Funda Coder
Views: 1,891
Rating: undefined out of 5
Keywords: fundaofwebit, funda coder
Id: drAgS91uMU0
Channel Id: undefined
Length: 39min 39sec (2379 seconds)
Published: Sun Jan 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.