Loading video player...
Hello everybody. Welcome back to my
training channel. Today we are going to
complete our Python
CRUD API part three part three of the
tutorial series. In the last two parts
we created the API first API for
fetching the data displaying it and we
did the insert form in the second part.
Now we have to add the facility for
editing the data from the index file and
deleting the data. So for that what
we'll be doing is we'll be creating the
fast API route for edit and delete and
in front end we'll be giving the edit
form and the links in the crowd
operation in the index file from where
we can edit and delete the data. So
let's dive into the coding and start the
editing part of API.
So this was the last API which we
created.
The last time which we did was this API.
And now what we'll be doing is
let me zoom it first. Okay.
We did two functionality in your API.
One is your fetching of products with
the URL/ products. Next is the post at
the same URL. Now for editing what we
have to do is we have to get the data by
id by id of the product. So that will be
filled in the form. So for that we'll be
creating a root over here
that is app dot the method will be get
because this will be fetching first. So
for fetching the URL will be something
like this slash products
and then slash and we give in curly
braces
product
id.
So this is the thing which has to be
added to get the product by product ID.
We give the URL / products and this will
be replaced by the actual product ID 1 2
3 4 whatever will be passed from the
index file over here. So this is the
root which is created. Thereafter we
define the function. So for that we give
def
get
product
by
id.
So we create this particular function
over here and in that you have to pass
the parameter. So that parameter will be
the same that is defined over here that
is product id
and we define the type as integer. So
this particular function will take the
product ID as integer as a parameter.
The same steps again we have to do the
connection first. So we define con
equals to get connection that will be
same in each and every step. After that
we define a cursor. So that is cur
equals to con dot
cursor.
In cursor we give as dictionary equals
to true.
So this is specified. After that what we
have to do is we have to create the
query to get the data. So that is cur
do.execute. In execute we specify the
query as select
star from
products
where
we have to specify where clause over
here because I want to fetch the
particular product ID data. So over here
we say where id equals to again we give
the parameter. So for giving the
parameter we just giving as percentage s
that is our parameter and the value of
parameter has to be passed after it. So
we give it in curly braces. Oh sorry we
give it in round brackets product ID.
So this is the parameter which is
passed. So this query will run and take
the product ID from the URL and it will
be passed over here. So that's the
query. After that we create the object
of product. I want to return the single
product. So product equals to cur dot
fetch
we have a method fetch one. So one will
only fetch a single record from the
database which is giving by the output
of this query. So this is done. After
that we close the cursor. So cur dot
close
we have to close the connection then. So
that is con do.lo close
and the query is return and if the data
is not found then we have to return some
error. So for that we give a if
statement that is if not
product
then what has to be done? So you return
error over here return we specify in
curly bracket message that data is not
found or whatever. So we specify error
and n error we specify as
product not found.
If everything goes well then we have to
return the entire product. So for that
we give the return statement at the end
return and then we have to return the
product. So we give it in curly bracket
and I define as product.
This is the array which will be used in
the front end product and colon the
product.
So this is getting a single product. So
this will allow us to get the single
product
from the data which is passed in the URL
by clicking on edit. After that the next
part for your editing portion is update.
So for update also we have to create a
root. So we specify app dot. Now this
will be a next route which is known as
put. Put allows us to update the data.
Post will create the new data. Put will
create the data but it will be in update
mode. So for that
we pass the same URL products. And over
here it will be same as the upper one.
We have to specify the product ID over
here. So that is the same thing which
you done at the top
product ID. So that is passed over here.
After that we have to define the
function. So def and we define the
function as update
product.
So this is my function name. And in that
function also we have to pass the
parameter that is product ID. So we
specify product
ID
colon integer. And over here we have to
take two data that is the product ID
also as well as the data which is came
from the form which we did in our insert
section. In insert we did this
particular thing in post we took the
form data. Same way we have to give the
form data over here also. We specify
comma and then we specify two fields
that is name
colon str that is string equals to from
that is form
and we specify three dots over here.
Spelling is wrong. Type mistake
form dot dot dot that is the data. Next
will be price. So it is same I specify
price over here in that price we have to
specify float as we did before. So float
and equals to we take the data from the
form itself that is form and in that
form we specify again dot dot dot.
So this will take the data from
the form which is submitted. So this is
done. Thereafter same thing we have to
do the connection. So we give con over
here
we specify get connection that is done
over here. Thereafter we specify cursor.
So we specify cur equals to con
dot cursor.
So that will create the cursor after
that cur do.execute and in that we have
to give the statement for updating. So
that update statement in XQL is update
products
and we have to set the fields. So the
statement is set and set name equals to
a particular value. So that is again the
parameter. So percentage s then we
specify a comma after that second field
that is price.
In price also we have to specify
percentage s that is our parameter
and where clause because we want to
update only one field. If you don't
specify where it will update all the
records so what we do is update product
set name equals to a parameter price
equals to a parameter where id equals to
a parameter
and now we have to pass the parameter
three values over here. So for that I
specify comma after that and in comma we
specify again a bracket and that bracket
will be let me complete the string over
here.
Okay and then we specify three values
name
pricea
product ID.
So three things we'll be passing over
here. So this is the update statement
which will allows us to update the
record inside your database. Update
products set name equals to parameter
price equals to parameter where id
equals to parameter that is a database
field and we pass the three values name
price and product ID. So this is
written. After that we have to close the
cursor. So for that before that we need
to do one thing which is done in our
insert that is commit to change the
record. So for that we do is car dot
commit
sorry con dot commit
that is done over here we commit the
data commit function so it will update
the record then we close the cursor
cursor dot close
there after we close the connection that
is con dot close
and we have to set a return message over
here also. So we say return in return
what we do is we give the status
in status we specify success. So I give
a status as colon and then we say s u c
e s success
and in success we can give a message
over here. So that is success comma and
we give a message
colon and we specify product
updated successfully.
So this is the part for your updating
the entire record inside your database.
So this thing is done. After that
we have to do for edit. So for edit what
we have to do is we have to specify the
edit function which as we did in our
edit uh and update we'll specifying a
function for updating the record. So for
updating the record that is deleting the
record over here what we'll be doing is
we give as at the rate app dot now the
method will be deleted.
So in delete also we have to give the
same path.
So that is
method will be post because it will be
delete. We'll be passing the delete
method below. So post and then we'll say
products.
So specify products
and over here again in curly bracket
we'll be specifying the product ID
but for that we just pass the record as
delete. So this is the path / product/de
and then we have to delete on the id
itself. So for that we define a function
defaf delete
give the name as delete products or
delete product and then we pass the id
over here. So that id type should be
integer
and we take the value from the form. So
we specify over here from
form
and in that form we specify again dot
dot dot
specify the function. So over here we
have to fire the query for delete. So
the path for delete will be app.post
because it is just delete function. We
don't want to put the value. In our
update we specified it as put. But in
delete we just have to post the data. We
have to just pass the ID and it should
be delete from the database. So delete
product integer id and we take the value
from the form. So for that again we do
the connection
con equals to get connection. The same
method over here get connection we
created cursor. So cur equals to con dot
cursor
con dot cursor and we do cur do.execute.
In execute we will be specifying the
delete query. So delete will be delete
from products
where we specify again the parameter id
equals to percentage s.
So this is specify we have to pass the
value for s. So we give comma over here
in comma we specify id.
So this query has been written over
here. Again we are making changes to the
database. So we have to commit. So again
concomit
we have to close the cursor. So that is
cur dot close.
We have to close the connection
on dot close and we have to specify the
message as usual. So we return the data.
In return we specify status.
In status
we give it as success again.
And in message we specify
message as
deleted.
So these are the three methods which we
have written new inside your API for
editing and deleting. First was getting
the data by ID. So for that we specify
as app.t get product/ product ID. We
define the function. We pass the integer
product ID over here. We get the
connection. We create the cursor. We run
the query as select star from product
where product ID that is ID equals to
the product ID. And we specify as cursor
dot fetch one. So that will fetch the
record once that is first record which
has been executed by this particular
query. It will only fetch one record. We
close the connection and the cursor and
we give the output as return the
product. So this will return me the
single product. Now to update that that
is after this has been fetched in the
form the user will click on update in
the UI and then that will be fetching in
this particular function. We have to
write a function for updating. So for
that products and product ID again we
write the function as update product
over here just one more change instead
of just passing the product ID we have
to pass the entire record. So for that
we did in the post method while creating
the same thing has to be done over here.
We specify name that is from dot dot dot
and the price and then connection cursor
and we fire the update query. Update
product set name equals to parameter
price equals to parameter where id
equals to parameter. Pass the three
values and then we commit it. Same thing
for delete. We just have to create the
path as delete. We don't need to pass
product ID over here. There is a mistake
over here. Just change it product ID.
Okay,
we don't have to pass the product ID
over here. We just create the path. We
define the function inside that we pass
the integer as from the value. So that
will be passed from the front end. We
get the connection. We fire the query
delete from products where id equals to
this and we commit and close. So this
part is complete. Just try try to run if
it is fetching any error or something.
The path is started over here and I give
us products.
So that record has been fetched. We have
the ID as one. We pass the ID as one.
So this is showing you that your single
query which we wrote right now is
firing. We can't update it directly over
without the form because this is taking
value from the form. So we can just
check this first part of getting by ID.
I just pass the URL with the product ID
and it is getting me the record that
particular record is being fetched. So
this portion of your API is complete for
the CRUD operation. Now for that we have
to create a form for editing inside your
front end and then just uh clicking for
delete. So two things has to be done
inside your
front end. So let's open the front end
folder
studio code. We open the folder as PHP
front end.
This was our front end portion. This is
the index file
which was fetching to the data. Now what
we have to do is first and foremost we
create the edit file. So for that we
just create a file named as edit
So for here we give the starting point
PHP.
Now this is edit page. So we have to do
each and everything which we done in our
create form. That will be a simple
repeatation of what we did. So first and
foremost we give the URL as API_base.
Whatever variable you want, you can keep
it. Then we take the base URL. So that
base URL is this
just products. So that will be the base
URL. We specify the base URL. Then we
have ID over here because I want to
fetch on ID. So dollar ID we specify in
PHP we used to get it using dollar_get
from the URL. So get will be passed over
here. I'll say ID over here. And then we
use a turnary operator and specify null.
So if it is not getting then it will be
assigned a value of null. Over here we
check it that if it is fetched. So if we
specify not dollar id if it is not set
then what has to be done? So for that we
can give a error over here. So we just
say die that is a pattern for your PHP
in you have that do or die that is
function ID in your PHP
invalid product ID
and this is done. If everything is good
then we have to fetch it. So we specify
the variable as dollar ch in that ch we
say curl in it.
We specify curl in it over here. So that
curl object is initialized. Then we have
to set the option for curl. So that is
curl set
set option. And we pass the curl object
that is ch. The option which we want to
pass is curl
optore
url. First and foremost you have to
specify the URL. So for that URL you
have to give it in
dollar API
base that is our top variable which we
defined and then we have to pass the
product ID. So we pass over here /d
dollar id. So this will be the URL set
options of curl. Then you again set
option that is curl
set opt. Second option to set in curl is
dollar ch which is write the object curl
underscore
opt
return
transfer because we are getting the data
over here and we specify true. So that
is done over here. Thereafter we take
the response.
So we create object that is a variable.
We specify response
equals to curl_execute
and we execute the curl object that is
ch. So this is done. This is written
over here. After that we close the
object taking the response. So you just
say curl close. So fire this close.
Don't forget to close the curl because
if it is active your server will be in
load. So call close it whenever it is
done. Our work is done. After that we
want to take the data from the response.
So we create a variable as data equals
to the data will be in JSON. As you saw
over here the data is JSON as usual. So
we take it from this JSON dot decode. So
it is JSON decode
a function to decode the JSON data from
the response. So we take it from the
response and we specify true over here.
So our data is in JSON format in dollar
data and we want the product data. So we
specify one more variable dollar product
that is single product equals to dollar
data
square bracket the data which we passed
with the name that is we are passing it
in this particular format product. So we
can grab it in this particular sequence
only the same name has to be passed. So
it will be an array. So we specify
product over here
product
or we specify null. So whether the
product is return or it will be null and
we again check if not product. If
product is not set dollar product
if not product then we have to give an
error. So for that again the die
statement die and say product not found
form.
This is done.
After that
uh come out of the loop that is a
statement. If statement we specify if
dollar server
and we specify the request method. So we
say request
method
equals to equals to equals to single
quote and post because in your form the
method will be post remember in your API
we give put but over here you just have
get and post. So just specify post over
here this is for the submission when the
user will click on the edit submission
form. So this is a checking that if it
is clicked the button is clicked then
only go inside and then we say dollar
update
we have to create the fields update
fields
create a variable update fields equals
to square bracket because I want
multiple data that is ID name and price.
So same way as we did in our insert we
have to specify it and we have to
associate it. This is associative array.
We say dollar post and we give the name
of the text box. The textbox name will
be name which will be created in the
below. This is done. We specify comma.
Second value we have to take. Second
value is price. So that is price over
here.
And over here also I specify it as
price.
So this is the array which will be
updated. Now again for this we have to
create the call object. This was for
fetching the data and this is for
inserting the data. Our curl object is
already closed. So we can create it
again over here. I open it that is ch
equals to curl in it. I initialize the
object.
Over here we have to specify curl
options. So that is curl set opt.
setting the option dollar ch again the
variable we specify the same URL we just
copy paste this line so it will be
faster
this is done uh return also we need so
we take the return line also but in
between we have to change the option so
for that what we'll be doing is curl set
opd and the option is for putting that
particular request and putting the
actual data for updating. So for that we
just specify as skull
custom request
R E Q sa
and we specify the method as put because
it is put we have to specify this as
call custom
request and that is put over here. So
that is done over here. Thereafter again
a call object we have to specify as post
fields that we did in in the insert form
the same thing. So over here request as
post fields
and that variable is dollar post fields
which we created at above. So we specify
as
dollar update fields which we specified.
So over here you have to specify four
options URL the custom request method
put the post fields that is the update
fields data and return transfer. So that
is done over here. After that we take
the result in result
equals to
dollar data
result we just need to execute. So what
we do is result equals to curl_execute
that is exec execute and the object is
dollar ch. So this will execute the
update request and we close call it is
call
close.
So one call request over here for
fetching the data. Second curl request
is for posting the updated data. So
check the method as post. We create the
array. We set four options and we
execute using this. Now the second part
of this particular is we have to create
the form over here. So for that we have
to go inside below this PHP thing and we
do is just say
dot type I specify.
or I just type HTML
file and it will be returned. So that is
done over here. Thereafter we have to
create the form in the body section. So
we give in body section. First we
specify H1. In H1 we can just say edit
product. So it is visible on the form.
After that what we do is we create a
form.
In form we have to specify the method as
post.
post capital or small it will work. And
in form we have to create the label.
In label we just say just type it
product name
and create the input field.
input type equals to text.
Type equals to text. Name equals to
first field is for name.
So we specify name over here.
We have to specify value over here which
will be fetched from the database. So
that is value equals to now the value is
coming in the array named as product.
Over here we fetched it inside your
product. So we can directly write it
over here to get the data in value we
can specify it as
double quotes we start PHP associate
tag.
So in that you can type it HTML
special characters I pass the function
and inside that I say dollar product
that is our array and inside that array
the field name that is the JSON return
name over here. So this will be done.
After that we have to close the bracket.
So this is done and we specify required
equals to true.
So this is first text box. I copy it. I
paste it. I just have to change the
names over here. So we can say price.
Input type equals to this. Specify as
price.
You can take the input type as number
also over here.
So it will take only number input value
equals to HTML special characters and
then you can specify it over here as
price
and we specify required over here. After
that we just have to specify a button.
So we take the button over here
and in button we give it as type
equals to
submit
then we type as update product.
So this much logic is done inside your
edit form. Now what we have to do is we
have to call this edit php inside your
index. So in that index what we'll be
doing is we'll be typing
a URL that is inside your table we have
to increment a field over here. So we
say th that is table head and we give it
as edit
and over here we can increase the TD
that is table data inside which we will
be specifying a href that is the URL to
fetch that record. So we give as a href
equals to our file is edit dot php. So
edit dot PHP
we specify question mark id equals to
and we have to pass it same way as we
did and printed the price and
everything. So we take this ID over here
we are printing the ID also we copy it
and inside this we specify as this
we have to specify it in PHP.
So the question mark equals to and over
here we just have to pass record. So
this is done and we can type edit in
between. So this is for edit. Same way
we can do it for delete also.
So for delete also we pass this
variable.
Let's type delete over here.
The file which we'll be creating will be
delete dot PHP.
And one more thing has to be passed in
your delete functionality. I just want
to give option to the user that is on
click.
On click I want to specify
return confirm.
I just ask the question to the person
that you want to exactly delete that
record or not. So we can just say delete
the item
and we specify this delete over here. We
give a termination over here.
So this is your delete thing. So two
things we added in our index dotphp that
is edit link and delete link. We haven't
created the delete function ID yet but
okay let's complete the deleted
functionality also then we can jump on
to testing the actual thing. So we
create a file as delete dotphp
that will be again a PHP file but this
is a shorter code because we just need
to take the ID and from that ID we have
to delete that particular record. So we
just test it if is set
dollar get id
and we specify id over here
single quote
because it will be a variable if is set
dollar_get id inside that we have to
again give the api delete url so for
that we create a variable as dollar api
type delete
equals to
take the same URL because I want it over
here
and instead of this uh two the URL will
be delete which we created in the API.
So that API delete URL has been created.
Then we specify dollar data a variable
and from that variable you just say
square bracket single quote dollar id
associate it with dollar get dollar_get
and in that get we specify id.
So this is associating this particular
variable data should be binded to this
particular ID single id data should be
binded. So we created again the curl
object. We just say curl_init
and we want to initialize it using this
data.
So specify the variable over here dollar
data
dollar api delete. So this is done.
Thereafter we say curl underscore set
option
we set opt and
dollar ch a new thing over here we did
it as set option and underscore array we
have to pass the ch object that is a
curl object over here and then we
specify a square bracket
in that we specify as curl
opt
post
and post will be true. So we give it as
true over here.
Second thing we'll be passing is skull
opt
post files that is post fields equals to
again we specify as true over here
no post will be data. So we specify the
data variable which we created and again
what we'll be doing is
the transfer that is return transfer. So
call opt
return transfer
we specify array s.
So we specified options in an array
format. We specify three things. One is
call op post equals to post field equals
to data. The data will be from that
particular id. So that id data will be
passing over here. And after this we
have to execute it. So we say
curl_execute
and dollar ch. So that will be executing
the curl object. Thereafter curl dot
close. So we specify close and dollar ch
inside it. So it will close that thing.
After deleting what we have to do is we
have to redirect the user to the same
URL. So for that uh we have it in PHP as
header location. So we can just say
header
in header I specify location
and our file is index dot PHP. So it
will redirect and then I exit over here.
So I type the code as exit. So what we
did in delete is we created the delete
URL in a variable. We took the data from
id get the id which is in the URL. We
started the curl object on the API curl
set option array. We pass the array
object post is true. Then we said post
fields. So that will be the data itself.
The ID itself and return transfer. Then
we executed and we return it to index
dot php. So this is the front end part.
So we did back end part as well as the
front end part. It is completed. Now we
can check if it is working successfully.
Let's test it. So for that
so let's
so let's test the application now.
This is our index. We added two links
that is edit and delete. So when you
click on edit
you can observe that you are fetching
the record by ID. The ID which is passed
over here three it goes to this API and
returns you the data. The form is
filled. Now you can check the edit
functionality.
It change the price to 45,000
and I click on update. It is updated. So
this is your edit working perfectly.
Same way we can check the delete
functionality. I click on delete. It
asks me whether you want to delete the
item and you can just click on okay.
The record is deleted. So this is we did
successfully the CRUD operation using
your fast API in your Python and
implementing the front end in your PHP
or PHP.
Hope the tutorial is useful.
For more tutorials, you can subscribe
and like our channel and get updated
with information technology tutorials.
For any clarifications or any doubts for
any of the topics on my channel, you can
always type in your comment box and I'll
surely reply you back. So, keep
learning. Thank you for watching.
Welcome to Part 3 of our Python FastAPI + PHP Integration series! In this video, we’ll complete our project by implementing Update and Delete operations — creating a fully functional CRUD (Create, Read, Update, Delete) system. You’ll learn how to: Create PUT and DELETE endpoints in FastAPI Connect PHP buttons or links to these API routes Update and delete data dynamically using cURL in PHP Refresh the data view automatically after actions By the end of this video, you’ll have a complete full-stack web app powered by Python (FastAPI) + MySQL backend and PHP frontend — perfect for showcasing backend integration skills. #fastapi #phpcurl #phpintegration #python #restapi #crud #mysql #connect #fullstackpython fastapi crud mysql fastapi php integration python fastapi full crud php curl put delete example python php api integration crud app using fastapi and php fastapi update delete example fastapi mysql full project python fastapi rest api mysql fastapi tutorial for beginners python full stack project fastapi php tutorial part 3 php fastapi crud example python backend php frontend build full stack project python php