PHP REST API CRUD 2: Create API to Insert/Store data in php mysql | Insert Data using API in PHP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys we are continuing our rest API in PHP crowd okay so guys as we have seen that in previous video we have created the API to fetch the records from our mySQL database okay so now guys in this video we are going to start with how to insert the record okay or store the record in our database with the help of API so guys now let's get started vs code editor and create a create.php file with the name create.php perfect so now guys you can import your basic headers so to create the API so let me just copy from the read.php file let's copy this and paste in your create.php file so guys let me just show you this the headers over here this is my origin and content type method and headers so all these headers are done and now we are including the function.php so this is a function.php and checking the method request or get or post put any methods okay whatever method we get we are going to check so we will be checking with the post method so we are whenever we store the data we use the post method so first step change your method over here so let's type it as post and then you can check in a if condition here you tell request method is double equal to post so if it is pushed then you're coming in else you are going to show the data called method not allowed so let us copy from the Red Dot PHP instead of typing it let me copy it and paste here done so for not five that means any other method if you use get method with this API so that time it will tell get method not allowed and then it sends the header HTTP that is format 5 method perfect so now guys once the data is correct you are in a method post that time you are going to get your input data whatever the field data is so let's take as input data only so input data equals to you will get all the raw data if you're using Ajax or any other method to store the data without the form so at that time you can use this file get contents PHP input okay Json decode function and inside that file underscore get underscore contents okay function so whatever we did I get here we are going to take that PHP colon forward slash forward slash input and tell that should be true okay so now you get your data here inside this input whenever you do the normal any data like if you send from the notepad or anyway through Ajax also you can Echo and see it let me just Echo and show you okay so now guys let's get back to the Google Chrome and move here let me copy in a new tab and instead of read.php Let's Tell create dot PHP hit enter you will see the method that is get method not allowed let me give the space where you can mention here okay now if you refresh you see that get method is not allowed so we need a post request so for that let us copy this URL guys and go to our Postman software just get with the new request here and I'm going to use a post method and just send the request you will find empty data okay so here you see nothing is returned so now if you go to your body and this is none and this is one form data and this is raw data okay so let me click on this form first and this is one Raw so here we are going to open and close curly brace and first change this text to Json we are going to enter the data in Json format only okay so first step let me give here name and send the data that is like with raw okay so I will send this data so to access let me copy this name variable and paste here you have to create a array format so before getting this let me show you what output comes over here you will find an array type of data okay so let's send it so here you see your array to string conversion comes so this is our when you make it to true it sends the data in Array format okay if you make this as false or don't give this true it will accept as object okay so now I'll give a name here and again get back now send the request you will find this name perfect so here you see that withdraw details have come over here okay so now guys this was one was for the normal data like when you send data without the form tag okay so now let's check this in if condition when this input data when you're not sending with the help of a raw data so at that time what you have to do empty function and paste here so when this input data is empty that time you are going to check using the post method okay so for that let me show you Echo dollar underscore post okay method here let me comment this let's uncomment and keep it a else condition so let's move this inside the else condition and here inside this name so what is this name this name is nothing but what we are sending here the data okay now you can get back so guys this else part is not going to work now why because we are not going to send from the raw data let me remove everything and go to the form data first and then type the input column name and then the data here with form data okay so I'm going to change this and now if you send the request you'll find the waveform data details perfect so it's coming inside this condition okay and now you can create a variable called stored or stored customer okay equals to let's go to one function customer okay and here you are going to send the post variable let me copy this and send here let's remove it so the normal post that form tag post it will go here and let's copy and paste here and this will be for your input data whatever raw data comes like Ajax other things okay and now you can control X and paste this condition after your effects done and you need to Echo this part so yeah done guys and now let's copy this function name and go to the function.php file and create the function with the name store customer let's go yep so here I'm going to create a function name function store function where call your database connection let me copy and paste here so my connection is called so first step guys let's create a variable called name equals to where we are going to validate and inside this first you will send the connection and then your input query so how are you getting the input query so we are going to tell customer input okay so this will be our customer input from the function we'll get let me go back to a create.php here you see whatever data we are passing we are accessing that data from this function okay so we are getting that input so instead of direct here we will call the input names whatever we provide so one will be our name and another is going to be email and phone so same details change the variables here you provide email done and now guys to validate you can give that in a if else condition if again else if again else if okay and here you can check with the empty empty empty function and inside that again trim function where you can trim the starting and ending space values okay where you will pass this dollar name and same way guys you are going to paste for all other conditions here okay it's going to be email and phone done and here you are going to return the input validation error so let's create one simple function like error 422 or input validation so for my understanding 422 is all the input request validations okay so here I send the data as enter your name copy and paste and again and enter your email enter your phone done so now let's copy this and create one simple function so function open and close and the message you get open and close then dollar data equal to again inside the array format okay let me just copy it let me go to read.php copy and paste here paste it and now guys this is input validation so please name as 422 okay and you can mention the message as unprocessable entity and instead of typing here in the message I want to show the exact data what message I have sent so let's copy this message from the function and you send that okay and you are going to Echo and just use exit done so now you get whatever you show that will throw this messages and now guys finally once it's done you can use a else condition here once all this data is correct you're going to store the data so first step let's write the query here like insert into table name so my table name is customers let me copy it so here customers customers and the column names and the values so my column names let me control B here the first input name second column email and third is phone and the values we are getting in the variable okay so inside the single quotes guys dollar name and then again inside single quotes dollar email and again inside single quotes dollar phone and now let's execute this query so let's tell a result equal to with the mysqli query function we are going to execute the above query so first your connection and then your query done so query will be executed and result is going to be stored here so now guys you can check in a if condition if this result is true or false if the query is false 500 error let me just copy from Below okay let's copy this internal server error and paste here in the else part done and now guys once the query is successful you can show the success message let me copy it and paste Here and Now guys let's change this status to two not one two naught one means something is inserted or created the record okay and here also HTTP headers we can send it and tell that it's created okay and send the message also customer created successfully as simple guys done and yep now guys let's begin with the testing so first step let's move so we have two methods to insert One is using the form and one is using the raw data yep let's get back and first validation we are going to do with the get request okay so if I insert the data with the help of get request let me send the request you'll follow that method not allowed get method not allowed that is this if condition in the create.php file this is the condition over here okay 405 method not allowed and now guys let's change this condition to post method and we are just going to throw the data okay we are not going to insert any form let me delete this let me delete this here so I have no form here no form data you can see and inside the role so it's not there let's just submit it okay so guys here you see you have got the message that is 422 your name okay but we are getting this warnings we don't want to show this warnings so for that you can get back to your create.php and just add one simple code that is error reporting so error reporting of zero okay so if you give this basic errors doesn't show let us send here and see the request perfect done guys and now let's begin with the form data first so first name these names are nothing but the column field names okay I mean the input names what we are accepting here in the function this name email and phone okay so those are the details name email and phone here you can give the values Ram Ram gmail.com and the phone number some random digit phone number all set and now let me just remove this at gmail email ID and let's send it so we see that we are getting enter your email so validation works fine and now if you give correct data it's going to be stored so let's send the request so here you see guys customer created successfully and now let me get back one step here on the get method on read.php let me fetch the data and see the newly added record has been displayed or not so here you see guide that third data we have added it's shown let me show you in the database also so right now it's to record let's refresh perfect so we see that third record also has been created okay done and now guys let's get back to our create API and let me insert again new record with the help of raw data okay which is used in Ajax and all okay so we can delete the column fields so I have cleared the form data let's go to Raw data and open your curly brace and make sure you are selecting here Json okay so first input it's going to be name and the name value and again the second it's going to be email Adil at gmail.com oops I have to mention them in a double quotes and then guys finally one phone so enter random digit phone number which is should be in double quotes done don't add a comma at last you'll get an error okay so let's remove that so now guys let's post the request okay so you get again customer created successfully which is two not one created okay now get back and check let's fetch the record and see the data is there or not let's see below perfect so here you see guys that it has been created okay so guys we have successfully learned how to create a API to store the data into your mySQL database in PHP okay so guys we have seen that in part one how to fetch the data by creating the API and store the data by creating an API in PHP so guys now let's see next video with the new topic that is like retrieve a single data edit the single data update the single data okay and also delete it so guys in this video that's it thank you for watching this video guys please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 21,358
Rating: undefined out of 5
Keywords: fundaofwebit, how to create rest api in php, how to create api and insert data from database in php, create api in php to store data from mysql database, rest api in php, php rest api store json data, php laravel rest api tutorial, php curl api tutorial, create an api in php to insert / store data using php mysql, php api to save or create data, php restful api tutorial, rest api tutorial in php, php api tutorial in hindi, php crud for rest api
Id: h4VzePleFk8
Channel Id: undefined
Length: 18min 58sec (1138 seconds)
Published: Sun Nov 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.