6. Food Order Website with PHP and MySQL (Deleting and Updating Category)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone i am busy tapa and welcome back to my channel today i'm back with a new episode of our food order website with php and mysql and in this episode we will work on deleting and updating categories so without further ado let's start our tutorial [Music] so i have already started my jam server and started running apache and mysql so this is our database or psd myadmin and this is our category table and inside category we have three data we have title image name feature and active now let's open our admin panel and we will log in and we will add the feature to delete and update category so i'll access my room panel so localhost less food order slash admin so this will take us to our admin panel and to login or access alright panel we need to login with username and password so hope you remember your username and password my username and password is admin and admin and login successful and we can go to category and we have added image title and feature and active now we need to work on update and delete uh category function and first we will work on delete category and then work on update category because uh deal category uh will take less time than update category so we'll first work on this category so let's go back to our code editor so i'll open my visual studio code and this is our manage category.php file so inside our admin folder we have our pages so i have opened just manage category so you can open manage category.psp and let's go back to our button so our button is here so here in our anchor tag we need to give the link instead of a hrf so i'll delete this hash signed or pound sign and then php start psp in echo site you are l and then we need to go to admin admin slash and then deleted dash category dot php so we will be creating delete category dot php file so inside or along with manage category and all of these things we will create delete category page so i'll click on new file and here we'll create delete dash category dlt category dot php that's it so here we have our dedicated category.psp file so we have successfully created a delete.category delete that's category.psp file and also edit link in our category button so i'll save it pressing ctrl ns let's go back to delete dash category.psp here we will just process the data and delete the category from database as well as remove the image from our images folder from this folder we will also be removing the images if available and then we will just redirect it to manage this category page we don't need to show any data in this page so we won't be using any any uh or we won't be using html page to display the value so i here i will create just psp tag php and then php end for now i'll just echo or print let's say delete page semicolon and save it by pressing ctrl ns let's go back to our browser and i'll refresh the browser and here i will check whether our link works or not so i'll click under category and our delete page is displayed successfully delete with equally repeat so this means we have successfully uh linked our delete page with our delete button if we are seeing any error live object not found for for error then your link is not working so you need to you might need to check the link in our delete button but if it's working fine then to delete the data uh or to delete the category we need to pass id so here i'll pass id so after delete category dot php uh we will add question mark and we'll call id is equals to and here we'll pass id so php open psp close and then eq dollar id that's it so this id will be obtained from here we have already obtained inside our while dollar row so here we have our id so we are displaying this id uh in our link or url and this method of passing sorry not here here so this method of passing the value using url is a gate method and from this id we can delete this category but we also need to remove the image from images folder when we are deleting the category for that we will also pass image name so after id is equals to psp echo and psp in here i will add ampersand and and will pass i'll call where i have alcohol i will create a variable called image name so i image image underscore name is equals to uh i will call it php echo dollar image name uh this image name this value will be obtained from here so here we have here we have image name so we are accessing or we are passing this value in our url from here so in our delete button or in our delete button url we are passing two values one is id and another one is image name so i'll save it and go back to browser and refresh it so if i hover over the delete button at the bottom you can see the image name as well so whenever we have image name there will be image name whenever we do not have image name it will be empty and we are also getting id value so we are passing id and image category uh we are passing id and image name through our url which is get method okay so we need to get this value in our delete page delete category.psp here we will get the value and we will process to delete our category okay so first at first we need to check whether the value are being passed or not so we will check whether we are passing the value through this button or not because sometimes some hacker or some user tries to get to the delete page without passing the value so here we will check check whether uh the id and image underscore name value is set or not so here we'll be checking both of them id and image name because uh in our url we have set uh both id as well as image name so to check we'll create if i assess it if set um if you set and then dollar underscore get id and here we will add and either you can use and this way or you you can use double and ampersand like this but for now i'll use and and it's set dollar underscore get image name image image underscore name then we will here we will get the value and delete else redirect redirect to manage category page category page and to redirect uh to our manage category page we need to use site url constant and our site url constant is inside our config folder so we need to get include our constraints file as well so at the top i will include constants files so here include constraints file so let's include it i am include and here we are inside our admin folder so we need to get out of this admin folder and get inside code free folder and include the constraint.php so to get out double fully stuff slash and then config slash c-o-n-s-t-a-n-t-s dot php that's it so we successfully encrypted our construct.psp now we can redirect to our manage category page so to redirect we'll use header and then location and then we will pass uh l site url and then uh we need to go inside admin slash uh we need to go to manage yeah sorry m m a n is manage this g o r category dot php that's it so site url admin dash managed as category.psp so here let's echo get value and delete so this is how we will check whether we have passed uh id an image name or not so i'll save it and go back to our manager scan manage category page i'll refresh it and whenever we click on this button so i'll click complete category dot php page or delete category button so here we can kc get value and delete so we are inside if condition so both of them are set id as well as image name you can see here as well so we have id as well as image name so i'll go back and click on when image is not at it i will click on this category uh even we even when we do not have image name we can get to our if section which is getting value and relating but what if we try to uh just go to delete prg page instead of without passing so what will happen if we try to uh go to delete category page without passing like our image name and id so i will try to go inside category.plc page without passing id an image name sometimes some user will try to do this and this is also some kind of hack uh hack so we need to prevent this as well so whenever i go try to go inside lead category.php file in without passing id and image name i should be redirected to manage category and it successfully redirected us to our manage category page so this is what our eps condition is doing if we pass id as well as so this and means both should be passed we should we must pass id as well as image name and then only we can like a process delete functionality but if we fail to pass either of them either id or image name then it will redirect us to our manage category page so we have added one sec security in our manage category page hope you understand if you have any confusion in this code then please let me know in the comment below i will try to explain you more in as simple as in as simple as a possible way so let's uh right go to delete the category as well as image from the folder so first thing we need to do is get the value so i will get the id as well as image name so dollar id is equals to dollar underscore get and then id and then we will get image underscore name is equals to dollar underscore gate and then image underscore name okay so that's it and after getting the value first thing we will do it uh remove the image if we have image and if image is not available then we won't remove the image but if the image is available then we will remove the image then only we will delete the data from database okay so first remove the physical phy sscl physical image file if available then only we will delete data from database and then redirect to manage category page get a go repeat with message that's it so first thing we need to do is remove the physical image file if available that means we need to check whether this image name has uh image file or not for that we'll use if condition if dollar image name then we'll use is not is equals to empty which means image is available available so remove it so if this variable of if uh image name is available then we will remove the image name else we won't do anything so to remove uh that image name we need the path of the image so i'll create a variable called dollar path and path will be let's say here we need to give the path right now we are inside admin folder so to go inside the category images category folder we need to get out of this admin folder to get out of this addon folder we use double full stop slash and then images and then category and then slash and at the end we need to concatenate our image name okay so dollar image underscore name that's it sorry controls it so that's it so we have our path as well and then we need to remove the image image so to remove we will use a function called online so uh let's create a variable for this as well so i'll create dollar rdm ov remove is equals to and we'll use unlink so this function say this is a built-in function this function will uh remove our file from this folder and to remove the file we need to give path so we have created here the path of our image so i'll give dollar path and this remove variable will have a boolean value which is true or false so if the image is successfully removed from this folder then this will have true value if it's it failed to remove the image from folder then it will have false so if dollar remove is equal is equal to false then here we will so let's add a comment here if fail to remove image then add and either message and stop the process okay so if we fail to remove image from the folder then we won't delete the data from database so here we will stop it first uh what we'll do it we will set the session message so set the session message uh then redirect to like uh manage category page and then we will stop the process to start the process we will use die function that's it this will stop the process and here let's say the session message so i'll call it as dollar underscore session and it will be re mov a remove and let's set the message so here i will create a div with our sorry div div div end and between this we will start the message and let's pass the class as well class is equals to error and here we'll pass the message so it will be fair to remove category image that's it and then we need to redirect uh to our manage category page to redirect we'll use header and then location location and here we'll pass site url and then hdmi admin slash manage this category dot php that's it so this is it so what we will do it is if image name is available that means if image name is not blank then we will remove the image from the folder so here we have state the path of the image with our image name and but if we fail to remove the image from folder then we just set a session message and set a session message failed here failed and set assistant message and then redirect to manage this category.php page and then stop the process okay so let's display this uh session message in our manage category page so i'll go to manage category.psp page and at the top along with our other session messages like add we have add message here we'll check for remove session message so let's check if is set dollar underscore session remove then we'll display echo dollar underscore system remove and also onset onset dollar underscore session remove that's it so this will display the message whenever we fail to remove image from our folder then after removing the image if imagename is available then we will delete the data from database for that we need to create a sql query so i'll create dollar sql is equals to uh d e delete uh from tbl underscore category so this is our tvl category or category table little frontal category where id is equals to dollar id okay so that's it we have obtained the id at the top here we have dollar id so we will pass this dollar id in where and this will read our data from database okay after this query so let's add a comment sql query delete data from database and we need to execute the query so execute the query so dollar is let's create a variable called raise a result mysqli underscore query and here we need to pass connection dollar connection and then we'll pass dollar sql that's it and after executing the query so we will check whether uh our data is deleted from database or not so check whether the data is rooted from data base uh not for that we will use if condition if dollar raise is equals to which goes to true then we will set success message and redirect else set fail message and redirect okay so we are doing that here so i will remove this message and let's set the status session message so dollar under school system i will call it as delete and let's add division the ifv and this will have class of success since this this is a success message so category deleted successfully and let's redirect it so to redirect we'll use redirect to manage category so here i'll add header and then location i will add constant site url and then admin slash managed as category dot php that's it and we also need to set this uh field message so i'll just copy it and then redirect so i'll copy it and paste it here and let's this is not right this is a fail message so it will be error let's say fail to delete category category and that's it let's save it and let's display this deletion in our manage category page so here let's display delete session as well so first we need to check whether session is set or not dollar underscores system de led delete and then we will display to display echo dollar underscore system delete and then we also need to onset dollar underscore season and delete that's it let's save it and let's check whether our delete functionality works or not for that i will go to browser and refresh the manage category page and i will read the last one test to image so i'll click on delete category and for before deleting this uh image let's go back to our code editor and inside our category folder you can see we have two images so if i delete this one there should be only one image okay so i will delete or i click on delete category button category deleted successfully and the data is gone from this table as well so let's go back and here in our category folder we only have one image okay so we successfully deleted the data as well as remove the physical image file from our category folder so now let's try to delete uh one category without image so i'll try to delete this one this do not have image so i'll click under category and again category is successfully deleted and we have one image that is this one so this is guys we have successfully added the functionality to delete the the category as well as remove the physical image file from our folder so in the next section uh we will be working with updating category okay until now if you have any confused or any error on deleting the data in database and removing the image then please let me know in the comment i'd be more than happy to help you and also guys please share and subscribe uh to my channel i need to support please do that for me so that i can make more tutorial for free and for for you and for free okay so let's go to our next section which is updating the category so let's go back to our code editor which is our visual studio code now i will i will remove or i will close delete category.php file and here we need to create another page for update category like same as add category page we will create update category.psg file okay so i'll click on new folder and let's call it update dash category dot php and here we will be uh displaying the data of selected category but for now i'll just display some message so that we can check whether our update category page is linked correctly or not so let's call it echo update category page save it control ns let's go back to manage category page and same as on our grid category button let's add the link in our update category so here remove the hash upon sign and then php open php close echo site url and then we need to go inside admin folder and then update that get a gory dot php okay so let's save it let's go back to our browser i refresh the browser and here i'll click an update category button and here update category page is displaying that means uh this function echo function is working correctly that means we have successfully uh connected our update category page with our update category button but if you are getting update not found or resource not found either then your link is not added correct please that means you need to focus on adding link okay so after that let's display the design same as add category so this will our update category page will be same as add category but we will have uh value will but value will be displayed uh in our uh this input type either on radio or text box as well as we will display the image if image is available okay so let's go back to our update category page i'll remove it and first thing i'll do it include our parcels like menu and footer so php and then include include power seals slash menu dot php and let's save it let's go back to browser and i will go to category page click on update category now we can see menu that means menu is added correctly let's add footer as well so i'll select it copy it and at the bottom i will paste footer so this will be footer.php save it go back to browser refresh again now you can see footer as well and between we need to add main content uh to display our category details so div with main content class and then uh inside this we will have a wrapper sorry ah wrapper and inside this wrapper we will have h1 tag s1 s1 will be update category that's it let's save it go back to our browser and refresh now you can see update category here now after that we will add a table to display uh or update the details of category okay so after that i'll add several line breaks and here i will add a couple of br tag as well so br and again br and then i will create a table table with a class of tbl30 if you remember we have created class for form like this of with tbl 30 class and inside this we will have tr and td tr first row and then inside here we'll have td so first thing we will display is i think category title so i will open category in new tab and here let's click on add category so we will make a similar like this one so title select image feature active okay so first one is title and here in another td uh here i will create a input tag so input and type will be title and its name will also be sorry type is text and the name is title so let's go name is equals to title and here we need to pass value okay so in a moment we will get the value of selected category and display here okay after title what we will display is we will display current image if available so i will create a new row let's call it td and here i'll call it current image and i'll create another column td and here we will display so to display the image first we need is to get the image name so for now i'll leave it as like i will set the message like image will be displayed here okay so after we obtain the image name we will display the image here for now just a message and then let's create another tr tr to select the new image so td we'll call it as new image and then here we will have another column to add button to select the new image so it will be input and its type will be file and its name will be so not name space name is equals to image and if you remember uh to be able to upload the image we need to create a form with uh ink type multipart slash form data and here also i forgot to create a form so before table we need to create a form and action will be blank so this closing form i will add at the end of table so i'll cut it from here so cut it from here and at the end of table after table ends i'll add the form in here okay so form starts here formats here so let's put let's push them inside so form is here form is here and inside the form we have table okay so action and its method will be method will be post and here we need to pass ink type to be able to upload our file or image okay ink type is equals to multi part slash form dash data okay so that's it now we have uh image uh we have section to uh select the new image now after that we have is so if we go back to our browser we are featured and active okay so here i will create a new column td and it will be featured and inside it let's create td and here we need to create input type radio so input type will be radio and its name will be name is equals to featured and its value will be yes first one will be yes and second one will be no so i'll just copy it and paste it here a name will be same because they are on they are for same thing either yes or no so they are both of them are for feature so name will be same so feature feature and value will be different no and this will also be no okay that's it now another one is active i'll create another one tr and td and it will be active and let's create another column td and inside it we will create radio bottom so i'll just copy both of them and paste it here and let's put it inside let's push inside and we'll change the name so this is active now this is active active these are feature feature and these are active active now and at the end we need to uh we need to create a button to submit this form okay so this will be input type submit and name will be submit and its value will be update category and we also need to give give the class class is equals to vtn dash secondary okay secondary and so we need to put this input type inside a td so here we need to create td and inside this td we will put this input type submit our update category button so i'll just cut it from here and i'll paste it inside here okay so that's it we need to push it inside rtd so that's it let's save it and let's go back to our browser and let's update our list refresh our update category page so i refresh it and here you can see all the details like title current image new image featured active and update category button now we have successfully designed update category form but we need to populate with data like we already have data like uh title feature active and image so we need to populate this data in our object category form okay so let's work on that and to display all of this data we need to pass uh the id of selected category so let's go back to our manage category dot php file and pass the id in our update category page okay so in our update category button here we will pass id value okay so what id is equals to php php end and here will pass id it says echo dollar id okay so that's it let's save it and let's go back to our update category page and at the top before form and after br here i will create a php tag and here we will get all the value of that selected id okay so psp open php clouds and here first we will need to check whether uh the id is set or not so check whether t whether the id is set or not so if is set dollar under score sorry dollar underscore gate and then id then only we will get the id and all other detail else if id is not set else redirect to manage category okay so to redirect we'll use header and then location and then add site url constant and we'll go to our admin slash manage dash category dot php psp okay so that's it uh let's save it and here i will just echo getting the data so let's save it let's add semicolon and let's save it and let's go back to our browser and let's go back to our category page i'll create i'll close this tab so we are manage category page and whenever we click the update category button we are already getting the id in our url you can see down here so if i click on this here you can see the message getting the data but if i do not pass this id value like if i just remove the id and i just give the update category.plc page then it it must redirect us to manage category so it it detects it to manage category so that means whenever we pass the value then only it will go to our object update category page but some way some users try to hack without passing the value then it will redirect us to manage manage category page okay so you can also add the session message and display the message like unauthorized access message from some message like that that is a homework for you if you want to add you can add it but for me i i do not feel like adding that message so if you want you can add it okay so let's work on update category so if you are passing the id then here we need to get the id so to get the id i will create id variable dollar id is equals to dollar underscore get id okay and here we need to get sql query to get all other details okay so create sql query to get all other details so dollar sql is equals to select i see alicity select all from t bl underscore category where id is equals to dollar id okay so this will get all the details of our selected category uh based on this category id okay so that's it then we need to execute the query so let's execute the query execute the query so dollar res is equals to my sqli underscore uh query and then we need to pass dollar con extend and then dollar as ql okay so that's it and after executing the query we need to count the rows okay so we need to count the rows so count the rules to check whether the id is valid or not okay for that let's say dollar count is equals to uh my sql underscore uh num underscore rules and here we need to pass dollar res and so if uh the number of rows is equals to 1 because there will only be one row of data with this id so there cannot be multiple id uh so let's check if dollar count is because it is equals to 1 then only we will uh get all the data so this is on another step for validation else uh redirect to manage uh category with system message this time i will add a message so dollar underscore session uh let's call it as no category category found so it's a session variable name will be no category found and let's say a message i will call it a div i'll create a div in the div div and here semicolon and we'll give a class of error because this is the error message is equals to error sorry not double quote single quote error and let's add a message message will be category not found and then we need to redirect to our manage category dot page so header and then location and here we need to concatenate site url and then here we will go inside admin slash manage this category dot psp file let's save it and let's display or let's check whether the session is set or not and display in our man manage display it in our manage category dot php page okay so i'll go to manage category page at the top along with delete and other sessions here we will display so if it's set uh dollar system no category found then we will eco uh this one and also onset our session that's it so this is it i will save it by pressing ctrl and s and let's go back to our browsers and i will explain what does our that if condition does so i'll refresh it and if we go uh like regular way if i click on update category then this will uh try to display all the available details of selected category but sometimes if a user tries to add some random number which is not available in our database then it will redirect us to manage category page okay so if we try to go like that then it will say category not found and redirect us to manage category page okay so this is another validation that we added in our food order website okay so this is also one security feature now let's work on uh getting all the data and displaying in our form okay so let's go back to our code let's go back to update category here we have checked successfully now let's get the data for that i will create a dollar rule and here uh we'll add mysqli underscore fetch underscore so so this will get all the data from our database and save it in a row in array format so here i will get dollar i'll pass dollar risk uh from database execution and then we will get individual data okay so let's get the individual data first i will need this dollar ti tle is equals to dollar roof and then title and then dollar uh image underscore let's call image name as current image so i'll call it as current underscore image is equals to dollar row and then image name and the column for current image is image name because in database we have saved the image name as image underscore name column so we are accessing from database so we need to give database table column name here i am just creating available with current image variable name okay and then we need to get featured and active so dollar featured is equals to dollar row and then featured and then last one is active so dollar active is equals to dollar row active so that's it let's semicolon here and save it ctrl s and now we can display this data in our form okay so i'll go to form so here we have our title the first form our first input field is title so in value here i will display title value so php eco dollar title that's it let's save it by pressing ctrl and s let's go back to browser and i'll refresh it and here you can see the value okay so here i'll go to category and i will add a random category so let's call it uh test category sorry because we have not added the image so i'll refresh again and here let's call it test and yes yes so i'll click on add category so fail to upload upload the image i think we have a bug in our add category section so i will fix it so let's open the add category as well so we also uh must be able to add the category without uploading the image so i'll go back uh to our i'll close this for now so we are here so category at category so i'll i'll go back to our code editor and i'll open add category so let's call add category and where is our image uploading section so if it settle underscore file underscore image name and to upload the image we need to check if the image name is uh if image name is available then only we will upload the image okay so here we need to check so we have image name so if we have image name then only we will upload so i will add a coat here so here upload the image on the if image is selected for that i will check image name if image name is available uh that means image is selected if image name is not available then that means image is not selected okay so here i will call it as dollar image name is equals to sorry not image name if i want to add if dollar image name is not is equals to empty then only we upload the image so i will cut this ending curly bracket or braces i will cut it from here and i will add after this if statement okay so after this if i will add i'll paste it here ctrl and v so here we need to push it inside and we will push all of this inside this if section okay so that's it so that's it okay so let's save it so what this image was what this if condition will do it if we have image or if we have selected image then only will upload uh the image but if the image name is not selected then we will not upload the image okay so that's it so let's go back to our add category section i will refresh it and i will give the name title and i will select featured and active but i will not select the file and click on add category and our category is added successfully and without the image so again i will add a category with image this time so [Music] category with image let's choose file desktop i'll go to desktop and here i'll select this one open and let's select as add category and category added successfully and our image is also uploaded okay so we have successfully fixed the bug as well okay so if we check our folder cat for images we have two images okay so we have two images and we have two images in our folder so we have successfully fixed our bug in our add category section as well now let's continue with our update category so i'll select this one and we have successfully displayed a test image let's go back to our update category and we have displayed the title or title for our category successfully now we need to display uh the image if image name is available okay so we have given the image name as current image so if correct image is not empty then we will display the image else we will display the error message okay so here we need to add php code psp and then here we will add if condition same as in our add category section i will check dollar so see if current image is not is equals to empty then we will display the image display display the image else display message so here i will add echo and inside this echo i will create a division d i sorry div division end and here i'll pass class of error class is equals to sorry error and here let's uh pass a message called image not added that's it so this will work now here if the image is available then we'll display the image okay for that i will break the php and start the php again and between this i will write a html tag to display the image which is img and here in src i'll just remove alt tag and in src uh i will pass the path to display the current image so path for path will display site url site url and then we need to go inside our images folder inside our category and then image name okay so images folders last category folder and then again we need to pass our image name for that we need to open php and then display the image okay for our image our variable is current image so current image has the name of our image which is image name and that's it let's save it go back to our browser and i'll refresh it so here we have our image and image is displayed i will resize the image i will give the width so here i will give width width is equals to let's give it 150 pixel save it go back to our browser and let's refresh it now our image is displayed perfectly so let's go back to our category and select the image uh select the category without the image so i will select this one and here we can see the message in red color image not added okay so that's it we have successfully displayed our title as well as current image if available now let's display uh featured or active status as well okay so let's go back to our code editor and here we have our select image button we do not need to do anything with this but for our uh featured and active we need to add a condition whether the value is selected or not so here inside input we will open psp and close php here we will check if dollar feature is equals to is equals to this one is yes so if ether is equal to is equals to yes then ah then we will select this echo selected and i'll break this line so that we can see the code clearly so this is the code so i'll copy it again and here if ah feature is equal to is equals to no because uh this has a no value so if feature is no then this will be selected okay so let's save it okay look get back to our browser i'll refresh the browser sorry we do not have value i think this is not selected eco let's call it checked checked and checked oh now we can see the button is checked okay so if i go to category manager or manage category so featured is all of those i have s so if i click on update category so this is yes so similarly we also need to display the status in active so in active i'll go here i'll just copy this one copy it and here in input here uh input oh i forgot to end the php so php if this is now active if dollar active if dollar active is equal to is equals to yes then we'll display checked so this value this checked uh is our html property so this will check we this will put tick mark in our radio button so if the value is yes then this will be checked if the value is no then we need to check bottom one okay so we can check this one so if active is no so save it so if active is no this will be checked if active is yes this will be checked okay let's go back to our browser let's rephrase sorry not single where the single way is which one is so here type o i missed to close our php close the psp don't forget uh this type of silly mistakes okay but sometimes we do mistake because we are humans let's go back to our browser i refresh it and here you can see both of the button is selected okay so that's it we have successfully displayed all the details of the selected category in our update category form now we need to uh process uh the form whenever we click on our update category button okay so let's work on that and guys if you have any confusion or any error till now then please let me know in the comment below i'd be more than happy to help you and also don't forget to subscribe and share this video or any tutorial you like this will help my channel to grow now uh the next thing we need to do is check whether the submit button or update category button is clicked or not and we will process the form only if our update category button is clicked okay so let's go to our form at the end of the form here i will start php and check whether the button is clicked or not okay so php php close and then inside it what i will do is i will check with if statement so uh if you set a dollar underscore gate and then submit then i will just echo clicked and we don't need to do anything with else condition i just want to check whether the submit button is clicked or not so i will save it go back to our browser refresh the browser and click on update category button so we are not getting any value that means we have some error okay so name is submit this is not get guys i don't know what is happening with me this is post because we are passing value uh through our form so this is post method so here we need to get the value with post method so echo clicked and also so if i click on update category button now we can see clicked message so then we will process our form okay so let's say first thing we need to do is get all all the values from our form from so here uh first thing i will get is title dollar title is equals to dollar underscore post title title title and then let's get so title and we will also get the value for current image because whenever image is not selected we need to set the current image uh in our database so we also get we need to get both value current image name as well as new new image name uh to get the current image name we need to pass the value in i in hidden input type so let's go back to our form and before our submit button here i'll create input type hidden so input type will be hidden and name will be name will be current image current underscore image and value will be psp echo echo dollar current image that's it and we also need to pass id so let's pass id as well in our input hidden type so input type will be hidden and the name will be is equals to id uh we can either pass the uh id or we can leave it as it is because we can get it from our top section here since both of them are on same page but this is the best practice uh to do so we just pass the id with our hidden input type and value will be value is equals to uh sorry value is equals to php and then echo uh dollar id that's it so we have passed current image as well as id uh in hidden type so along with id i will get uh both of them so dollar id is equals to dollar underscore post id and here i will get current image so dollar current image is goes to dollar underscore post and then current underscore image okay so that's it and then uh then we will get a feature as well as active and i will get the new image after those okay so let's get uh current or not current now let's get feature as well as active and for that we need to check so if uh it's set uh if it's set by the way we don't need to check if it's yet because uh update forms are already checked with previous value so both of them are checked uh with previous value we only need to check on add category okay so we can get as our previous one so i'll get featured is equals to dollar underscore post featured affiliate you already featured and also let's get dollar active dollar sc active is equals to dollar underscore uh post and then active that's it for now then we will work on uh uploading updating new image here we work on updating updating new image i if select it and then we will finally update the database update the database and then redirect to manage category with message okay so for now we will leave updating a new image uh first let's update the database and then we will work with updating the new image okay so i'll go to step three i will skip the step two for now so here i will create sql dollar sql i'll call it sql 2 because i have already created sql at the top here we have already created another sql so i do not take risks or to conflict the sql query so i'll create dot sql tool and i will create the query which is update sorry up update uh tbl underscore category and set and here we will set uh the value and set first we will say title say title tit title is equals to uh dollar title and then we will set featured an active let's say featured and active so this would be dollar featured and then active t if active is equals to uh dollar active okay uh and we will also update the image uh after we like update the image we will also rename the image name in our database but for now we will not rename our image name and after active we need to add where keyword to only update the selected category so where id is equals to dollar id okay so that's it and do not put comma after the last column and that's it so this is our sql query then we need to execute the query so xg cute the query so i'll create variable dollar raise and i will call it address 2 and here mysql sorry my sqli underscore query dollar database connection and dollar sql two we don't need to create as uh dollar connection two because database connection is same for our project so sql to then check and redirect our uh message redirect with message okay so here first check check uh whether query velocity a query xg qt or not so if uh dollar raised to is equals to is equals to true then we will redirect with sesame that means category updated else else fail to update get a guri okay so here i will state the message so dollar underscore session update and here i will call it as i'll create a div let's create a div and div end div end and div and here we'll give class is equals to success success and here i will give message so category updated successfully successfully and then we need to redirect so to redirect we'll use header and then location and then site url and then admin and we need to redirect to our manage category manager cte ngo our manage category dot php php okay so we set the session message and we redirect it to manage category page so i will copy it and paste it here as well and here we'll set failed message so this is update and class will be error and let's say failed to update category that's it and we are ready we are redirecting to uh manage category dot php okay so let's save it and let's go to manage category.psp and here uh let's display our update session message as well so if is set dollar underscore session and then update then echo dollar underscore session update and then onset onset dollar underscore session update that's it okay so let's save it let's go back to our browser let's refresh it and we can't now uh and we can uh change the category name so from test image i'll call it as food and right now we have not added the functionality to change the image so i will just change the title feature and active so i will change feature status as no and active status as no and let's click on update category so here category updated successfully and here you can see our status also change featured no active no and title also food okay so that's it we have successfully added the functionality to update title featured and active now let's uh add a functionality to update the image as well so let's go back to our code and let's go to update category and here so when our image is selected so here we need to check whether uh image is selected or not so i'll uh here i will check check whether the image is selected or not [Music] so now let's uh check whether uh image is selected or not so first we will check with if set if is set dollar underscore files and our name of input type file is image and we need to check uh with name property so if we have image or not then we need to get so if it set dollar underscore files then we will get the image image details else else we need to set we will create image name for changing in our database so image name will be current image okay so if image is selected then we will set the image name as new image name but if image is not selected then our image name will be our current image okay now let's get the image detail so first thing we need to get is image name so dollar image name is equals to dollar underscore files and then i image image and then name and then we will check whether image is available or not check whether the image is available or not so if dollar image name is not is equals to blank sorry controls it if imagename is not is equals to blank that means image is available uh image available uh else else our image name will be current image okay so i'll copy this and i'll paste it here so sometimes what happens is uh we we may like sorry expected so we have error in some ways so where is the here also here so if don't underscore files here we need our square bracket close so save it so go back to browser and go back to update category now sometimes what happens is uh we may click on choose file so when we click on choose file then this dollar underscore file value is set but if we do not select any image and click on open or click on cancel then this is already set but image is not selected so that means we are inside this section but image is not selected which means image name is empty okay so in this case we need to check whether we have image or not if image is available then we will like upload the new image remove the previous image but if image is not available ever available then we will set image name value as current image okay so hope you understand if you have any confusion please leave the comment below okay and after this what we need to do is uh first we will upload the new image and we need to remove the current image okay so if image is selected then we need to upload the new image and remove the current image okay for that we need to create a source path as well as destination path so and we also need to rename our image for that i will just copy those code from add category so let's go to add category and here what we have done is we have obtained the extension of selected image like dot jpg or dot gif and then we renamed our image and then we gave source part destination path and uploaded the image okay so i will copy the code from here to here so let's copy the code upload so ctrl c to copy from like rename to upload okay so just go copy from there i'll go to update category and in this upload section i will paste the code okay so i pasted it so here so i'll call it as a section a and i will call this as section b okay so here what we have done is uh obtained the extension of our selected image and here we rename the image and here we get the source path or selected or the path of our selected file and here we give the destination where our file need to be saved and here we pass the file name and this upload variable and this is our function uh php function that uploads our file in our category folder okay so and this will hold either true or false value so if image is operated successfully then this value will be true but if image failed to upload then this value will be false and here we check the condition if we fail to upload the image we nee which means if dollar upload value is false then we display the message upload session message and fail to upload image and this time we need to go to let's go to manage category okay so and we also need to display this session message so dollar underscore session upload so i'll go to manage category and let's add that session value as well so if i assess it is it dollar underscore season upload then we will echo dollar underscore session and then upload and onset dollar underscore session upload upload upload that's it let's save it let's go back to update category so guys i just copied the p i just copied the code from add category and paste it in update category section because our tutorial is already long and i just wanted to reuse the code but if you want me to type all the code in another tutorial then please let me know in the comment okay so and if you also have any confusion or if you have any error then please let me know i will be i would be more than happy to help you guys okay and after the image is uploaded uh we also need to remove our current image okay because our new image is already updated we do not need our previous image okay or we don't need our current image so i will go here and let's remove our current image so create dollar remove the mov remove is equals to on link and here we need to give our path okay so our path will be like let's create a path so i will create a path to remove the image so uh dollar path let's see dollar remove underscore path and let's give the path so our path will be so we need to go to our sorry we need to go to our we need to get out of our admin folder then we need to get inside images and then we need to get inside our category and then we need to give our image name so here i will give image name dollar dollar image not image dollar current image we do not need to we don't give the image name instead we give current underscore image so this will remove our current image okay so dollar current underscore image so uh this line of code will remove our image and this won't remove this is the path so here in our dollar remove here we need to keep path so dollar remove underscore path and this code will remove uh the image current image from our category folder okay so here we'll check check whether the uh image is removed uh not and if uh fail to remove then display message and stop the process okay uh css process okay so if dollar remove i will check if remove is equals to false which means fail to remove the image filled to remove the image so failed to remap remove image so here i'll create a system dollar underscore session field remove uh is equals to let's create a division division and then div and say in semicolon let's create a class this class is equals to this is error error and let's call the message let's say the messaging message failed to remove current image that's it then we will redirect it so i'll add header location and then site url will redirect to admins less manage desk category dot php okay so that's it and we will stop the process so this code this die will stop the process so we start the process so that's it so let's save it and let's go back to our browser and let's test this feature so i'll go to category and i will select uh the first one so let's select the food so if this is food so let's select this one i want to say the image so update category let's choose file and let's call it as this image i will select this image open and let's click on update category okay category successfully updated but our image is not changed so we have some errors so the error is i found the error guys uh the error is we have not updated the database so we have changed everything but we have not updated the database so here we need to update database so after title let's say dollar not dollar image underscore name is equals to dollar image name and that's it so our image name is changed and save it and do not forget to add the comma after image name okay so let's save it go back to browser let's refresh the category so i'll refresh the category and i'll click on update category you know select the file so select the new image update category uh i think we have some e either or we have some issue so if we rephrase our image is not uploaded image is not uploaded so let's go back to our update category move uploaded file images for underscore category.ext uh image name okay guys i forgot to check while removing the current image we only remove the current image uh if the image is available remove the current image if available okay avai already ali okay so here let's check with if uh dollar current underscore image is not is equals to empty okay so if current image is not empty that means if current image is available then only we remove the image so i'll cut all of this code got it from here ctrl x and paste it inside here ctrl v and just push them inside that's it so if current image is available then only we will remove the image else we won't remove the image okay so let's go back to our browser so let's go back to our browser and let's check again so i'll refresh the page update category choose the file select the image open and update category still our code is not working i think our removing the current image is not working so let's check our manage category.php so here we have not displayed the image so let's let's display this field to remove image as well so i'll copy it i'll go to manage category and here let's display the message if we fail to upload the upload or remove the image okay so if here if it said underscore session failed remove then let's display the message okay and also on set the message so i will save it and let's go back to our manage category and here uh filtering current image so that means we are failing to remove the image i'll again update it and click and select the image update the category and fail to remove the image failed to remove the current image so we have problem on removing our current image okay so i will remove this right now i will try with the last one so let's select the image update category and image is successfully updated i don't know why but this first one is not working uh this might be because while uploading this first category uh we did not rename the image that might be the problem so for now i will delete this image from database or i will delete this category from database so i will delete uh this first one so i'll it the number two okay and so this deletes our first from our database so we have we do not have that data but we have other two so this is also image and now let's try to change the image for number two so i'll update two and i will change the image for this and i will select this one and our update category now our image is updating successfully category updated successfully let's try to add image for the first one so i'll create an update category image not added select image i will choose this one and update category and image is updating successfully so the problem was that we added the first category before renaming the image so that was the problem now everything is working fine so that's it but while testing i have added a lot of categories so i will delete all of those images and only uh remain these two image okay so i'll go to database i will refresh the database and i will see the name so i will only store two image category 20 and 73 and i'll remove all of those so category 20 and 73 okay so i'll remove this one uh delete the move to recycle them and i think i will have to control i will select control only only 20 and 73 733 so i'll remove all of those select and right click and delete so did it delete uh move to disable okay so we only have uh two images okay so i'll refresh the category again i'll freeze the category again so we have two images i'll add one category uh let's say category 2 and i'll select yes yes choose file and for now i will add let's say i will add which one should i add so i will select this one not this one let's select i will select okay this one is fine i'll select this one add a category so i have an image here category added successfully now i will try to update the featured and featured status so both of them are yes i will try so id is equal to six update category what happened now so this category objects categor.psp id 6 not found what is update category i think i deleted i accidentally tilted update category page so i'll go to recycle bin let's go to recycle bin uh where is my recycle bin i'll go to recycle bin along with all of those in i think i delete update category so i am opening so here is my update category so i will restore now i will go back to our browser and i will try to update so now i have my update category page so guys sometimes these things happens i forgot to i accidentally created update category if i had deleted for recycling then i had to do all of this again fortunately i had this on recycle bin now i will select no select no and update category now feature and activate status to no image remains same and i can also change i will try to change the category to title as well so this will be let's say new category and update category so new category no no let's try to replace this image as well so i'll select update category select new image for now i'll select this one or this one we can select either of this i'll check this one for now okay so select update category and images images change successfully as well so this is guys we have successfully updated uh the category as well as uh replaced the previous image and added the new image successfully and if we check our category folder so let's go to our editor and categorical folder we have three images and we have three categories so that means we are removing the physical image file as well so this is guys thanks so much for watching till the last and if you have confusion or if you have any error till now then please let me know in the comment below and i am extremely sorry uh for having uh a bug while uh uploading the image due to our first category that was added before renaming the image i hope you do not have any either till now but if you have any errors then please let me know in the comment i will try to fix your error as soon as possible and if you like this video then don't forget to like share and subscribe i'll see you a new episode till then stay safe take care see you soon bye
Info
Channel: Vijay Thapa
Views: 20,187
Rating: undefined out of 5
Keywords: vijay thapa, software development, web development, programming courses, free courses, php, mysql, php for beginners, php tutorial, php project step by step, php and mysql projects, php and mysql tutorial for beginners, food ordering system, food order website, food order website with php and mysql, how to create food order website, update in php, delete in php, update image in php mysql, web development full course, backend web development with php, learn php, php course
Id: 9wlbIb3gCEs
Channel Id: undefined
Length: 98min 12sec (5892 seconds)
Published: Tue Nov 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.