PHP REST API From Scratch [3] - Update & Delete
Key Takeaways
PHP REST API development using MySQL, focusing on update and delete operations, as well as introducing category model development
Full Transcript
hey guys welcome to part three of building a REST API with PHP from scratch no frameworks or anything so in the last video we made the read single so that we could get a single post we also did the create so we can make a post request and we could add a post through through our rest client alright so now we're gonna do update so we want to be inside of our post model and what I'm gonna do is copy the create method because there's a lot of stuff that is the same so I'm gonna create that I'm arm sorry I'm gonna copy that whole method and paste it in and we'll change this to update post and it'll change this to update now as far as the query goes we're going to instead of insert into we want to say update update this table and we can do this we can do the same thing here set all this stuff but we need to say what posts were updating so right after the category ID we're gonna say where and we're gonna say where ID equals question mark alright actually we could use a named parameter here so we could do : ID like that it's up to you if you want to use named parameters or positional so let's see we prepare the statement we're still we're gonna clean the data just like we did before but we also want the ID so we're going to let's actually just copy that down and let's do ID so this ID equals this ID like that and then we want to bind the ID as well so right here and we're binding it here - this ID then we want to execute it we want this this stuff will be the same so if something goes wrong it'll print out the error and that's it so pretty easy pretty similar to the create now we need to create in our API post folder we need to create a file called update dot PHP alright and for update I'm gonna copy from C create dot PHP hole grab down to let's see actually it's let's just copy the whole thing and go to update usually creates and updates are very similar now one thing I do want to change is from post I want to make it a put request okay so we'll go down here we're gonna get the stuff that is submitted but we also need to set the ID to be updated okay since this isn't a create it's an ID I mean I'm sorry it's an update we need to know the ID so for that we'll say post ID is going to be equal to whatever the data ID is okay and then we're going to set the properties here and let's just change this to update and we're going to call the update method and then we'll say post updated or post not updated and that should do it it should be as easy as that so we'll save and let's try it out so what we're gonna do is we're gonna say post update dot PHP and then we're gonna go to let's see actually want to change this to a put request and headers should still have the application Jason as the content type and then in the body you know let's add ID so let's say we want to update for and we'll change the title to updated post and we'll say this is an updated post and that should be good so let's let's try it out we'll send and we get post updated now this is ID 4 so let's check it out by going to read underscore single dot PHP and we want to check ID of 4 and we want to make that a get request and there we go title is updated post and body is this is an updated post so we've successfully done the update so now let's do the delete which is actually pretty easy we're going to first go to our model so you can see the the the process that that we that we're doing here so we'll go to the model and let's see we'll just type it out this is easy enough so make sure we go under the update we're still within the class we'll say delete post so public function delete so our query query is going to be delete from we want to concatenate this table and then we just want to say where ID is equal to and I'll just use a named parameter here so we're ID is equal to colon ID and then we want a prepared statement so I'll just grab that from one of these like that and let's clean the ID so I'm gonna grab this remember this is this is a delete so we're not the only thing that it's going to take in is the ID then we need to bind the ID so I'll just copy from right here should probably comment this stuff alright and then we just want to execute the query and just do this in return true if it executes return false if not like that and that's it so we'll save now we'll go to our post folder and create a file called delete dot PHP and in this file I'm just gonna copy let's see let's copy the update and paste that in and we're going to change the allowed method to be a delete this is all going to be the same this is going to get the posted data which will just be the ID we want to set the ID to the data ID we don't need any of this because we're not adding this stuff and then let's say delete post so we want to call post delete that we just created and we'll change this and this to delete it alright so we'll save and let's go back to postman and let's delete will delete seven the one that we created so we'll say API post delete dot PHP make sure that it's a delete request and then for the headers you want to make sure it's jason and then the body all we need is the ID that we want to delete so we'll say delete seven and send and post deleted now let's go to post read dot PHP make a get request and now if we look one two three four five six and no seven so seven has been deleted alright so that's it guys we now have a full crud rest api for our blog post now what I would like you guys to do if you're interested is to finish this with the categories and if if you are confused on how to start that out then I'll help you with that but then I think that you should try to do it on your own and just take the same principles that we've used so far and it'll be even easier because categories only really have a name they have an ID in the name so what we'll do just to start you guys off is create a folder and API called category and we also want to create a model called category dot php' and the reason I'm not doing this is one it gives you guys a chance to do it on your own and in another it's it'll be very repetitive I don't want to do the same exact thing for categories but we could even copy let's see no let's not copy let's we'll just type this in real quick so PHP would create a class of category and we want our DB stuff so we want a connection we want a table and in this case it's going to be categories then you want your properties so we'll do public ID public name public created at ok then we want our constructor and I can actually copy some of this stuff it's so from the post model we'll just grab the constructor and then what I'll do is I'll do that I'll do the first one I'll do the read and then if you guys want you can continue and do the rest of them ok so just like with post where we had the read here we can actually I'll just type it out this one this is much easier since we're not doing any joins or anything so public function read and actually let's put a comment here so we'll say get categories so we want to create our query so our query is going to be select and then what do we want to select we want the ID the name and created actually just the ID in the name but then we want to do from and then the table so what I'll do is concatenate this table and let's see from this table and then we'll just say order by created at descending okay so that should do it for the query now right underneath that we want to prepare the statement so we'll say statement equals this connection prepare pass in the query execute it so s s TM T execute and then just return the statement alright so we'll save that now in Category inside the api folder we'll create a file called read dot PHP and let's see in this file we're gonna do pretty much the same thing we did in the read in the post except some things will be changed so I'm just gonna copy this whole thing and we want the headers to be the same we don't want the post model this time though we want the category model okay connect to the database we're not going to instantiate a blog post we're going to stanch EI a category object so we want to change this to category and change this to category it's still going to take in the database then we want our category read query so this will be category read we're gonna get our results count we're gonna see if there's check if any cat categories and then we're gonna create I will say cat array I'm just going to call this cat array okay and again we want it put in the data then we're going to loop through we're gonna extract the row and change this to say cat item and then it doesn't have all this stuff categories all we really want is the ID and the name oh you know what I think I messed one thing up in the model real quick if we just go back over to the model since I ordered by created at we have to select that as well alright so back to the category read so we have the cat item now we want to push on to the category array data the category item and then we want to output the jason of cabaret and then down here we'll say no categories message will be no categories found alright so let's try it I did that kind of fast so hopefully I didn't mess anything up so we can now go to API category slash read dot PHP get request send and there we go so we have an adjacent object with a value a key of data and then we have the array of categories with the ID and the name so it's up to you guys if you want to keep going and you want to do a whole crud thing for the categories that might be some good practice but other than that that's going to be it there is the the github repository in the description I'm not sure if I'm gonna finish the categories and put that in the repository right this minute but I probably will eventually or maybe someone can make a pull request with it just make sure it's clean please but that's it guys thanks for watching and I hope you enjoyed it and I'll see you next time
Original Description
In this part we will add the ability to update and delete a post and also start on the category model for you guys to finish up on your own
Code:
https://github.com/bradtraversy/php_rest_myblog
💖 Become a Patron: Show support & get perks!
http://www.patreon.com/traversymedia
Website & Udemy Courses
http://www.traversymedia.com
Follow Traversy Media:
http://www.facebook.com/traversymedia
http://www.twitter.com/traversymedia
http://www.instagram.com/traversymedia
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Traversy Media · Traversy Media · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Changing Your DNS/Nameservers
Traversy Media
Create a MySQL database in cPanel
Traversy Media
Install & Uninstall Joomla Extensions
Traversy Media
Adding and linking an article in Joomla
Traversy Media
Create a Joomla Blog
Traversy Media
Import & Export A MySQL Database
Traversy Media
Use A Custom Font On Your Website Using CSS
Traversy Media
Connect Joomla Site With Dreamweaver
Traversy Media
Remove Phoca Gallery 3.2.3 Footer Text
Traversy Media
Drupal 7 Security Update 7.19 to 7.20
Traversy Media
Add An Addon Domain In Cpanel
Traversy Media
Pull A Heroku Rails App and Database
Traversy Media
Create a Custom Joomla 2.5 Module - Part 1
Traversy Media
Create a Custom Joomla 2.5 Module - Part 2
Traversy Media
Create a Custom Joomla 2.5 Module - Part 3
Traversy Media
Joomla SEO Tutorial - sh404sef Configuration
Traversy Media
Font Dragr
Traversy Media
Convert an HTML Template to Joomla 2.5/3.0 - Part One
Traversy Media
Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Traversy Media
Rockettheme Rocketlauncher Joomla Site in Under 10 Minutes
Traversy Media
JQuery FAQ Slider Tutorial
Traversy Media
301 Redirect With htaccess File
Traversy Media
Convert HTML to Wordpress Theme - Part 1
Traversy Media
Convert HTML to Wordpress Theme - Part 2
Traversy Media
Easy JQuery Widgets
Traversy Media
Codeigniter App Part 1 - Creating the Database
Traversy Media
Codeigniter App Part 2 - Installation and Configuration
Traversy Media
Codeigniter App Part 6 - Login/Register System
Traversy Media
Codeigniter App Part 7 - Models List CRUD
Traversy Media
Codeigniter App Part 8 - Models Task CRUD
Traversy Media
Node.js Part 1 - Install NodeJS on Windows
Traversy Media
Node.js Part 3 - Building a Static Page Server
Traversy Media
Node.js Part 4 - NPM
Traversy Media
Node.js Part 2 - Install MongoDB in Windows
Traversy Media
Create a Joomla Quickstart with Custom Sample Data
Traversy Media
Install MongoDB in Ubuntu
Traversy Media
HTML5 Web Storage
Traversy Media
Create a Joomla Bootstrap Template From Scratch
Traversy Media
Ubuntu Server 14.04 Setup Part 1 - Installation
Traversy Media
Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Traversy Media
Create A Wordpress Widget - Part 1
Traversy Media
Create A Wordpress Widget - Part 2
Traversy Media
Create A Wordpress Widget - Part 3
Traversy Media
Create A Wordpress Widget - Part 4
Traversy Media
Get Started With Sass on Windows
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 1
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 6
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 4
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 5
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 3
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 2
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 7
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 10
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 8
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 11
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 9
Traversy Media
Build An Audio Player Using HTML5 & jQuery - Part 1
Traversy Media
Build An Audio Player Using HTML5 & jQuery - Part 2
Traversy Media
Youtube Data API v3 & jQuery To List Channel Videos
Traversy Media
Using Bootstrap With Ruby on Rails
Traversy Media
More on: API Design
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Common Next.js Errors (and How I Solved Them)
Dev.to · gary killen
Applying Scalability in Backend (CodeBuddy)
Medium · LLM
Why Every Backend Developer Should Learn Nginx Before Going to Production
Medium · DevOps
Connecting Frontend to Backend: A Backend Engineer’s Reality Check
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI