8. Food Order Website with PHP and MySQL (Delete and Update Food)

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 am back with new episode of our food order website with php and mysql and today we will work on deleting and updating food so without further ado let's start our tutorial [Music] so first of all i log into our admin panel so go to the url to go to our admin panel and enter the username and password so my username and password is admin and admin hope you remember your username and password and then login login successful and so today we'll be working on food so here we'll be working on updating and deleting food so first we will work on deleting food because it takes less time so let's work on deleting food and then we'll work on updating food okay so i'll go back to our code editor and along with all of these pages i will create a new page called read dash food so i'll click on this new file and then i'll call it as delete does food dot psp so delete dash food dot psp and we'll be working on this so here i'll just open php close php and just echo the page name just for testing purpose so delete for page so this is just for testing purpose let's save it by pressing ctrl and s and let's go back to manage food.psp and in our button we need to add the link so here is our delete food so i will remove this hash our pound sign and then open php close php echo site url site url and then we need to go inside admin and then uh inside admin uh we are going to delete as uh food dot php so i'll save it site url admin reach that delete dashfood.psp and let's go back to our browser and i'll refresh the manage foot page and i'll click on delete foot button and here you can still delete food page that means we have successfully linked our delete page with our delete button if you are seeing any uh resource not found object not found error then you have uh some error in your link so add link like this and there won't be any problem so we have successfully connected delete page but we need to pass some value like id to delete to specify which food we want to delete so to pass the id i will add question mark and then i'll create a get variable since we are passing the value through a url this is a gate method so i'll get a get variable called id and here we need to pass value so php and then eq dollar id that's it we have already obtained id uh here here we have already obtained id and along with id we also need to pass image name because our for some of our food will have image name so we need to delete the physical file so that's why we need to pass uh the image name as well so after the php ends here i will add ampersand or and and then i will create another get variable called image underscore name and image name is equals to here we need to uh display the image name so php open and then let's okay so here i will echo dollar image name so that's it so here is our link admin slash read dashfood.psp and we are passing id and image name id is equals to display the id and here we have image name echo image image name is equals to image name and they are get method because we are passing value through url okay so let's save it let's go back to browser and i'll refresh the browser and when i hover over the button at the bottom at the bottom left you can see id as well as image name okay when we have image name it will display the image name but when we do not have image name that it won't uh display any image name okay so it will be blank okay so that's it uh if i click on delete button we are successfully going to do uh dot dash food dot php page now we need to work on deleting uh food from database as well as removing the physical file of our image if available okay so i will comment out this echo and here i will write the steps first we need to check whether value is passed uh on a url or not for that i will check if it's set uh where is it set if it's it and then another underscore get id and we have set uh two values id and image name so and is it dollar underscore gate image under school name and ama name then only proc as a process to delete else redirect to redirect to manage food page okay so hope you understand why we need to do this i please let me know in the comment if you know why we need to check whether this value is set or not okay so i will save it and here either you can add two amber's end or you can add single and sign okay either you can use a and the end or you can use double land okay so i will write a comment here either use sorry use double ampersand or a and the end okay so you can either use either of these either two ampersand or single and okay and here i will just echo dro css process to delete delete and here i'll echo redirect okay so that's it let's save it let's go back to browser and refresh the browser and i'll click on delete food and here you can see process to delete that means we are inside this section and we will be deleting the image okay so drilling food as well as image but if i just go to uh click under foot button and if i do not pass id and image name this is going to delete a food page but without id and image name so in this case we need to redirect it to manage food section okay we do not want to display any error since we have not passed any id and image name so that's why we need to check whether uh id animal name is set or not okay so that's it now let's work on these two of these and i will just comment out this and instead what we will do is we need to set the session message dollar underscore season and what is this this is delete and is equals to i will open div and close div and we will pass a class of error so this will be plus is equals to error and here i will say on authorize access that's it we will set this session and we will redirect it header and then location and we need to concatenate site url and then uh admin slash uh yummy and manage this food dot php okay so that's it it will redirect us to manage food dot php page but first we need to include our constraints dot php okay so here at the top i will include constants dot constant page because since we have not included menu we do not have constraint space so that's why we need to include constant space so include and here we are inside admin but we need to go inside config and then constraints.psp so we need to get out of this admin folder and then config slash constants dot php so that's it hope it's linked correctly i will save it go back to browser i'll refresh it i'll click underneath food and we do not have any error that means our constant.plc file is linked correctly okay so we have successfully included constraints.php now let's display this message delete when we when we do not pass id or image name okay so i'll go to manage food and along with add at the top i will check whether deletation is selected or not created or not and then display if created okay so if it said dollar underscore session delete then we will echo echo dollar underscore system uh delete delete that's it and then we also need to onset dollar underscore session delete that's it okay so let's save it and then go back to our delete food dot php and then we can now work on processing the delete thing so here i commented out the delete echo delete and i will start writing the steps so first step is to get id and image name so this will be step one so i'll write this step and then we need to remove the image remove the image if available a v a i l a b l e so i will call it as number two and then at last we need to uh delete food from database database and then we need to redirect to manage food with session message okay we will do all of these four things so let's start with number one so number one is uh getting uh id and image name okay so i'll create a variable called dollar id is equals to dollar underscore gate and then id and next is image name so i'll create another variable image underscore name is equals to dollar underscore gate and then image underscore name okay so that's it and our next section is uh removing the image if available so first we need to check whether image is available or not so check whether the image is available aba ila be early available not and delete only if available available so what we'll do is if uh download image name and then is not is equals to empty uh this means when image name is empty then it has image name okay so this means it has image and need to remove from folder okay for that we need to get uh the image path so get the image path so i'll call or i'll create a variable called path and then this path will be we are inside uh admin folder so we need to get out of admin folder and get inside images and then food okay so let's get out of admin folder to get out double fully stuff backslash and then get inside images and then we need to get inside food slash and at the end we need to concatenate dollar image name okay dollar image image image underscore name okay so that's it so this should give the path to our image which needs to be deleted okay so then we need to uh remove image file uh file uh from folder so i will create a variable called remove and i will use on link function uenal i n k only function so this function will remove our image from folder so here we need to pass our path so dollar path that's it so this function this online function will remove our image file from our folder and then we need to check whether image is successfully removed from folder or not and this remove variable will have true or false value so if it has true value then image is successfully removed else uh if the remove has false value then we fail to remove the image so we'll check that so check whether the image is removed removed or not so if dollar remove is equal to is equals to false this means we failed to remove failed f i l a f i l a refill to remove image so we need to create a session message and redirect to manage page and also stop uh for the process of deleting the food okay so here i will create a session message dollar underscore session and this will be upload and let's add a session message deep and then i will create a class of error is equals to error and let's add a message called fail to remove uh image file okay so this will be the message and then we need to redirect to manage food so redirect to manage sorry manage food so here we will use header and then location and we'll concatenate site url and then admin uh slash manage dash food dot php okay so this will redirect us to manage food and also we need to stop the process of deleting food so here i will use die function to stop the process okay so when we fail to remove image then we do not want to delete the data from database okay so that's it now let's display this upload message in manage food as well so i'll go to manage food.plc page and along with add and delete we will display uh upload message if available so if if we set dollar underscore and then upload and here we will eco dollar underscore system upload upload and here we will onset dollar underscore system here upload upload upload so that's it now let's go back to delete food and here uh we have completed number two which is removing the image name if image name is available and we can now we can go to step three which is deleting food from database okay so first thing we will do is create a sql query so dollar sql is equals to delete from tbl underscore food and we need to use where keyword so where id is equals to dollar id okay so we'll use this sql query to delete the food from food table okay so and then we need to execute the query so execute the query so i'll create another variable called dollar raise is equals to my sqli underscore query and here we will pass dollar connection and then dollar sql that's it and check whether a query is executed successfully or not and set the session message respectively okay so check whether the query executed or not and set the system message respectively respect ipl respectively okay so if dollar raise is equal to is equals to true it means food deleted else fail to delete food okay so that's it now set the let's set the session message respectively and display the message okay so i will create dollar underscore season and then this will be also called delete and is equals to and div and then uh close the div and here uh i will add the class is equals to uh this one is success success and we will uh share the message as food deleted successfully and then we need to redirect it to manage food so headers and then location and sorry header location and then we need to concatenate site url so here we'll continue site url site url and then admin slash managed as food food dot php okay so that's it now i'll just copy this and i'll paste it in l section so copy and paste here and put uh we will remove this one delete and this will be error and here fail to delete food okay so that's it and we are directing to manage food page so we have done this section as well so i'll cut it from here i'll paste it here above so that's it and we have all of the session message delete and one of this will only be created at a time if you are confused we can just change the name of this so let's call it as on on its ori jt unauthorized okay so we need to display this message as well so i'll copy this and let's go back to manage food and here along with add delete upload uh let's display the session on unauthorized so this will be u n a u t h o all right jt unauthorized that's it copy control and c and then ctrl v and then ctrl v okay so that's it let's save it by pressing ctrl and s and let's go back to browser and let's check whether our works or not unfortunately we have an error so online 24 manage dashfood.psp online 24 okay so let's go back manage jazz food line 24 so here i added the semicolon before the parenthesis uh it needs to be at the end that's it let's save it and then go back to browser and let's refresh so here we have our delete page or management page now let's try deleting uh one of these so first i will try to delete without image so i'll click on delete food food related successfully and let's check the database as well so localhost.php my admin let's uh expand our food order database and then tvl food so here we only have one food and here we can also see one food and now let's try deleting uh the food with image okay so here uh first i will show you that here we have one image name so when we try or when we delete this food then this should be removed as well okay so i will click on delete food and food deleted successfully and let's check our image folder and we do not have any image in our food folder as well okay so that's it guys we have successfully added the functionality to delete the food now we need to work on update food uh but first let's let's add some food so i'll click and add food and i will form fill add again i will add another one add and i will add another food with image so i'll just add random balances file and desktop world desktop and i'll add this one add food so we have image for one and two of them do not have image now let's work on updating food okay so let's go back to our code view or code editor and i will remove delete for psp and here what we need to do is first we need to design update form same as add form so i'll try to design a form same as add form for update and it will be filled with data of selected food so let's go back and along with all of these pages let's create new file called update food so update dash food dot psp that's it uh here uh first i will just create php and echo the page name to check whether our page is linked or not so eco update food page that's it let's save it and then let's go to manage food page and here in our update food button we need to link that page so here we have our update foot button and here i will remove this has learned and what sorry what psp and then eco ecs eco site url and then here i will go to admin slash update there's food dot psp okay and we also need to pass id this time so i'll pass id so what id is equals to psp and then eco dollar id okay so that's it and here we don't need to pass image name because we need to get all the details from database as well so at that time we will get image name as well okay so let's save it and let's go back to browser and i'll go to food page and i'll click on update food button and here we are successfully going to update food page and we also passed id id equals to three so if we have any error like object not found then you have an error in your link so please add the link correctly now let's work on update food so here instead of displaying just update food page here we will display all the html page things like menu footer and form to update the food so first i will open php close php and include include menu so include parcels prti als less menu.php that's it at semicolon let's save it by pressing ctrl ns and let's go back to browser and let's refresh the page and here we have our menu similarly let's add our footer so i'll copy it go down and here i will add footer okay so let's save it by pressing ctrl and s let's go back to browser refresh the browser now we have footer as well now let's add the main content section so i'll create a division deep with a class of main desk content main content and inside this main content i will create a division with wrapper class so deep wrapper and here we have our wrapper and inside this wrapper we will create a title for title we are using h1 tag and this is update food page okay so let's save it go back to browser and let's refresh the page and here we have update food and after this we will display or will create a form to update the food so let's create the form okay so first i will create a separate br tags and then uh we will create form okay so here i will create a form form and action will be blank and then we need to add method me method is equals to this is post method and since we need to upload the new image as well we also need to keep in ink type a and c t y p ink type is equals to uh multi part uh slash form dash data okay so that's it so please guys uh please resource on this tag ink type and please add some details about this tag in comment below okay so that way we can learn each other so i will add the form ending form tag at the bottom and inside this form tag i will create a table to align our form uh nicely so let's get table and we will give the class for our table and class will be tbl tbl that's 30 and i know you know the reason behind adding tv al30 class so if you know you can add the meaning of tbl30 class in the link or in the comment below okay and then we let's create tr and then t d and first thing we have is so let's check our ad food so i'll just open the add food page in new tab and we will design same form so first we have title and then description so i'll create uh title and for that we will use td and it will be input type text input input type will be text name will be sorry not here here name will be title and then we'll have plse placeholder is equals to uh for title goes g-o-e-s goes here now that's it after title we have description so tr and then let's add td this will be description yeah c r i p t i o n description and then for description we are using text area so i will create text area in new td so text area and here we need to give a name for name we'll give c r d e s c r i p tion description we do not need id and uh cool 30 is okay instead of row 10 let's add 5 and let's save it by pressing ctrl and as let's go back and let's update our let's refresh our update food page so here we have and by the way we do not need to give placeholder instead we will be displaying the current value here so we do not need to add placeholder refresh it here will be displaying the value of selected food so that's why i remove the placeholder and after description we have title description and then price and select image we will have current image as well as select image okay so price and then select image okay so let's create tr tr and then this will be td it is uh price and td will create input input type will be number and please add the region for using input type number in comment below okay input type number and name is equals to uh price and that's it and we will display the value of selected food after price we have we need to display current image so here td will be current see our ent current image and here we will display the image so td i will just type the message here for now and display the image if available a v a i l a b l available after that we have category feature active so let's display or let's create section for category so it will be catagory and i will create another td and display the drop down for our category so to create a display uh to create a drop down we use select tag so select and name will be category we do not need to give id and inside select we will have options for now i'll just create one option and later we will get all the option from our database okay all the of all the categories from database so value will be let's say zero for now and this will be uh test option twice category that's it let's save it now let's add featured so i'll create another tr tr and td this will be featured featured and then we need to create another cd and here we will create input type radio and uh create the buttons to select for featured so input and this will be type radio led io and then name will be feature and value will be is equals to uh yes that's it and here we'll add yes to display the name and another one will be input type for no so copy i'll copy this paste it here this will be no and displaying will also be no so this is displaying value this is the value that will be saved in database okay so now let's create a active uh button for active as well so i'll just copy whole tr copy and after this chair i will paste it here and instead of featured this should be active and here in name it will be ct iv active and this will also be ac div active okay so that's it let's save it and at the end we will create update button so tr and then td and here we will create input type submit input and type will be submit and name will be name is equals to sub mit submit and here we will add value value is equals to update food and class will be c cla class is equals to btn secondary so that's it let's save it by pressing ctrl ns and let's go back to browser and i will refresh this update for page now we have a title description price current image and we forgot to add uh select image option after current image other than that we have successfully added everything so i'll add select image option so after current image tr and td and this will be select new new image and here td and here we need to create input type input type will be file and the name will be name is equals to image okay so that's it let's go back to browser after saving this page so refresh the update foot now we have our button to select new image okay so that's it now first let's work on displaying all the categories from database and then we will uh display uh the details of selected food okay so let's go back to our code and in our category section here we will display all the categories from database okay so for that we need to open php so open php close php and then here we will create sql query dollar sql is equals to uh yeah let's select all from tbl underscore category and we also use where keyword because we only want to display category which are active okay so aware active is equals to yes okay so that's it then we will execute the query so i'll comment add comment as well so go easy give you query to get active categories get a go reach and then we need to execute the query so execute the query so dollar race is equals to mysqli underscore query and then we need to pass dollar con as well as dollar sql okay so that's it and after that we need to count the rows so um count rows so dollar count underscore rules uh is equals to or we can only say count is equals to my sqli underscore norm underscore rules and here we need to pass the last race that's it and then check whether a category is available or not so take away the wst category available or not so if dollar count is greater than zero that means category available [Music] available else category not available okay so category not available one category is not available we will display a message called category not available with value zero okay so for that i will use echo and inside this php i will use html tag okay so inside this echo i will add op sorry option option and then close option o p t i o n option and here i will see i will say the message as uh get a glory not away label and i'll set the value as zero okay so i'll set the value is equals to zero okay so that's it here we have added a double quote at the beginning so we are reading single quote okay so either we could have like broken the php open the php and between that we could have written html tag but uh this was just a single line uh value that's why i wrote the php i wrote the html tag inside php okay and for our displaying all the categories here we will use while loop so while and then dollar row sorry dollar or double row is equals to my sqli underscore f e t uh ces underscore s suck and here we need to pass dollar risk that's it and then we will get uh the category title and uh category id okay so let's say dollar category underscore title is equals to dollar row and then title and we'll get category id so dollar category underscore id is cost to dollar row and then id okay so here uh i will break the php and then start the php and between this i will just copy uh this or we can do same as this one so let's try uh adding html inside psp okay so i'll add echo echo echo and then here i will close and here i will add option optio okay option and close and then here i will close option option and here i will add dollar category title here we will display category title and here that value is equals to uh dollar category id okay so i will remove this option from here now and i will save it and go back to browser and try to check whether it works or not now here you can see all the categories from database okay so this is how you can write html inside php okay so previously we used to like create a brick php tag and display the html inside php so if you check here so here you can see we have broken the php and between this broken and opening the psp we wrote html tag but now we have written the html code inside php okay so this is how we can write it but uh i think we need to write like before because here we need to check we need to use if condition as well later so i will show that in a while so i'll comment out this for now so you can use this way and for now i'll close the php and open the php and use the our previous way so option value is equals to here we will like echo dollar category id and here in between option will display category title eco is here so eco dollar category title okay so that's it let's save it go back to browser and let's refresh and here still you can see all the categories okay now we have successfully displayed a category in drop down now let's work on getting all the details of selected food okay so let's go at the top and where is our form so here is our form so i will get all the details at the top okay so after menu is included i will create a php here and first i will check check whether id is set or not so if is it uh dollar underscore gate id if id is set then we will get all the details else else redirect to to manage food okay food so here i will create header and then location and dot site you are l and then admin so there's manage dash food dot psp okay so that's it so do you know why we are checking this please add in the comment below okay for now i will not check so you need to add the region in comment below okay so now get all the details so first we need to get the id from url so let's get the id so i'll create a variable called dollar id is equals to dollar underscore gate id and based on this id we will get all other details okay so let's the sql let's create sql query sql query to get the selected food okay so here i will create variable dollar sql to because we have already created dollar sql while displaying the categories here we have dollar sql so i'll create dollar sql too and query will be select all from from from tbl underscore food where id is equals to dollar id okay so that's it and then we need to execute the query so execute the query so dollar res 2 is equal to mysqli underscore query and then we need to pass dollar connection dollar connection is same for all so we don't need to create dollar connection too and will pass sql 2 okay so that's it and after the xql is executed we need to get the value in array so we will create get the value based value based on query executed so here i'll create dollar row is equals to my underscore fetch underscore fetch underscore asset and here we'll pass dollar raise to so this dollar row will get all the values or all the details of selected food in array so now let's get the individual values so get the individual value values of selected food okay so let's save it and go back to our database so i'll select the database and here i'll go to structure so let's get we have id at the top so we don't need to get the id because id will be same and we can get title description price image name category id featured and active okay so let's start with id so i'll call dollar title is equals to dollar not for dollar row and then this will be title and then dollar description is equals to dollar row and then right r i p t i o n description after that what we have we have price and the image name okay so dollar price is equals to dollars row and then this will be price and for image name i will uh create the variable called current image because we will uh add the image name variable i will call we will call the image name variable for new image okay so for now i will create current color in the underscore image is equals to dollar row and then image underscore name after that we have category id so for category id as well i will call current category so dollar c score range url and current underscore category is equals to dollar row and then c t e g o r category underscore id okay so because we have uh created a column called category id and we need to give the column name okay so current category current category and then featured and active active okay so dollar feat you already featured is equals to dollar row and then featured and after that dollar active is equals to dollar row active okay so that's it so we have obtained all the values now we need to display this value in our update food form okay so we'll display all the value here so let's go and let's go down in our form so here we have title and input type title and along with name we will pass value so is equals to php echo dollar title okay so that's it let's save it go back to browser and let's refresh sorry we have now syntax you know unexpected uh rest to online 13 so let's go top on line 13. dollar row we have dollar raised to connection oh here i forgot the semicolon and here also i will call it as row 2 and i will add row 2 everywhere row 2 sorry guys for forgetting this row 2 row 2 row 2 row 2 so that's it so change row 2 and add a row to everywhere instead of row i will add row 2 because row is already created down here so if we create again row so this may conflict and we may have other error error so here i forgot semicolon now added it i will save it go back to browser and refresh it now we do not have error and we obtain the title we are displaying the title similarly let's display description and price and other things as well okay so let's go back and so make this change okay row 2 make the row 2 so after title let's display the value for text area for text area will display the value between text area tag so here php echo dollar description and then save it go back to browser and refresh so description is also obtained there's a display price so price here along with name let's add value value value is equals to psp echo echo sorry dollar price that's it and here we need to add code uh we don't have code after value so value is goes to we need double quote and then only display the price okay so that's it now let's save it and go back to browser and refresh and here we can see the price as well so now let's work on displaying the image so for displaying the image we will check whether image is available or not and we will display the image only if a white label else will display the message like image is not available okay so let's go back and here i will open uh create php tag php and check whether image is available or not if dollar current image is equal to is equals to empty that means image not available and if else image available available so when image is not available we'll create a message so here echo and then semicolon and inside it div slash div and i will add a class class is equals to error and m is not available this will be our message else we will display the image so here i will close the php open the php and between this i will create a img tag and here in src source we will give the path to our image so echo site url and then here go to admin and then not admin we need to go to images food and then we need to give current image name so eco dollar not imagine current image so current image holds the name of selected food image okay and here in alt this alt attribute is important for seo only that's why i used to skip in previous tutorials but if you want to add some value then here you can add the name of food title like psp echo dollar title like this you can display the title or if you do not want to mess with this right now then you can just remove it okay so that's it let's save it go back to our browser and let's refresh it so here image not available but if we go to food and select the food with image i'll select update food we have our image displayed okay so let's resize this image and make it smaller so i'll go back to uh code view and here inside image i will add width is equals to 150 pixels so let's save it go back to browser and let's refresh now our image is smaller i think this is okay for now if you want it bigger then you can increase the width okay now first we will work on uh displaying the value for fisture and active and will work on displaying category for selected food okay so let's go and add featured so here we have our feature so here we need to check uh whether yes it checked or whether no it checked okay so here i will create open open psp and then close php and then here i'll add if dollar featured is equal to is equals to yes then we will sorry we'll create parenthesis and echo check checked okay so that's it so if feature is equal to is equal to s then we will display checked i'll copy this copy and go to next one and paste it here and if it is no if the value is no then we will display this one if the value is yes then we'll display this one okay hope you understand this code if we don't understand please let me know in the comment and i would be more than happy uh to explain you okay so i'll save it go back to browser and refresh so our no value is checked so let's go to food and for this one for this one featured is no and we are getting no value similarly let's check for active so here i will just uh copy this php code and i'll go in active paste it here and this is first one is yes because this is yes this value is yes and instead of feature this is active okay so now i'll copy this one x ctrl c and here ctrl v so if dollar active is equals to no then this will be checked okay so let's save it go back to browser and refresh it so here our value is yes we can confirm it by going to food and here feature is no activities yes featured is no active is yes similarly let's check another one so this is a feature is yes activity no okay so feature is yes activities no so we are successfully getting all the values now let's uh select the category of selected food okay for that we need to go to code view and in our category section so here i comment out this because uh we need to check our ad if condition and inside if inside echo we cannot add if condition so that's why i prefer this way for update okay so here uh same as in our featured and active we will check with if so psp if dollar current category is equal to is equals to dollar category id then we will echo selected okay selected that's it so this is a drop down so we need to use selected i'll use a small selector so selected uh this was the radio button that's why i have checked since this is a drop down this will be selected okay so if current category current category is obtained from category of selected food so here is our current category and category id is here okay so we are comparing these two and if this matches then we will select it okay so let's go back to browser let's refresh it and here snacks is selected so that means uh the current category for our this food is snacks so that's it guys we have successfully displayed the values for everything now you need to work on updating food so before updating the food we also need to pass uh two different values uh before submit button in a hidden input field okay so if you remember in our category module we have also done this because uh we need to update the category we need id of selected food as well as current image name so that's why we will create input type with hidden type okay hidden and then name will be name will be id sorry id and here we need to pass value is equals to php and then echo dollar id so that's it and then we need to pass current image so i'll create another input inp ut input and type will be hidden and name will be name is equals to here we will pass current image current underscore image and here we need to pass value okay so value is equals to and then php echo dollar current image okay so we need this current image because we need to uh remove the previous image if new image is selected and uploaded we need to uh remove the current image from folder okay so that's why uh we need to pass a current image as well as id id is needed for updating our food so that's it guys now let's work on updating the food for that uh we need to go to the bottom after form and we'll open the php so here i'll open php and here we need to complete our two three multiple tasks so i will add uh all the steps so first we need to check so first let's check whether a button is click or not so if i said dollar underscore post and then submit this post and then bmi submit that means oh here i forgot this and then submit then only will process update so let's check eco button clicked and let's save it go back to browser refresh it and here we want to update the food only if this button is clicked so button is click that means our form is working perfectly so here i will remove it and i will add the steps that needs to be carried out okay so at first we need to uh number one we need to get all uh the details all the details from the form and then we need to upload the image if selected and then we need to change remove the image if new image is uploaded uploaded and current image exist okay so that's it and lastly we will uh update the food in database and finally we will redirect to uh manage food with session message okay so that's it now let's work from step one so step one is getting all the details so first i'll get dollar id is equals to dollar underscore post id and then dollar title is equals to dollar underscore post uh title and then description dollar description is equal to dollar row uh not draw sorry dollar underscore post this script sun and then dollar price is equals to dollar underscore post price price and then uh we will work on getting category uh we will work select new image later so first let's let's get a current image so i'll get current image so dollar current image is equals to dollar underscore post and then current underscore image and then let's give dollar category dollar cat tag go reset easy our category dollar underscore post and this will also be category and then uh is our input type name category so i'll go to select where is select select so name is category so that means it's correct so category after category are featured and active okay and here we do not need to check uh whether featured and active is set or not you know the reason right write the comment below for not checking whether uh featured and active is set or not okay we checked it on ad form but we are not checking on update form why let's write the comment uh let's write the region comment okay so i'll get featured is equals to dollar underscore post and then featured and here dollar active is equals to dollar underscore post active that's it so here successfully getting all the values now we will need to upload the image if image button is clicked so that means we will work on getting this okay so first we will check whether the upload button is clicked or not uh check weather s80 weather upload button is clicked or not if it's set a dollar underscore files image and then name that means [Music] upload button clicked and else if it's not clicked then our image name remains image underscore name remains dollar current image okay so if our update button is clicked then our image name will be image name is equals to dollar underscore files image and then name so this is new image name so if uh image name is selected or if the file is selected then image name will be new image name else if the file is not selected then image name will be previous image name or current image name okay so that's it and then we need to check whether file is available or not check whether the file is available or not so if if dollar image name is not is equals to blank this means image is available image is available level and what we need to do is we need to rename the image so let's rename the image so our new image name will be dollar image name is equals to uh but first we need to get the extension so let's get the extension dollar ext is equals to a e and d and then explode explode and here we need to pass the symbol that we want to explode with and we need to pass the image name so dollar image name so this code will uh gets the extension of the image and then we need to rename the image so here we need to rename the image so image name is equals to new name will be food that's uh what was our previous thing food names food dash name dash and then concatenate random number sorry random and minimum value maximum value that's it and then we will concatenate fully stop and add the red dollar ext extension okay so that's it sorry extension so that's it so this will be renamed image okay so that's it so this is our remaining image now we need to upload the image to upload the image uh to upload the image we need to get get the source path and destination path okay yesterday destination path so let's create dollar source underscore path is equals to dollar underscore files and then emails and then tmp underscore name that's it uh after that uh we need to get destination files so dst underscore path is equals to uh we need to get out of we are currently on inside admin folder so we are in update for so we are inside folder but we need to get inside images and food so here i will give we'll get out of this admin folder get inside images folder and then get inside food folder and here we will give the or concatenate uh image name dollar image underscore name that's that needs to be uploaded okay so these are our source path and destination path so here i will add a comment source path and then dst i need ion destination path that's it and finally we will upload the image so upload the image for that i will create upload upload upload variable and we'll use move uploaded file so this function will upload our uh file this uh into our folder okay so here we need to pass source path as well as uh destination path okay so dst path that's it so this will upload our image then we need to check whether image is uploaded or not check whether ct whether the image is uploaded uh not so if failed to upload if uh dollar upload is because it's also false that means fail to upload then we will create a session variable session is equals to upload and let's set the message i will create div close the div and then i will give the class as error and value will be failed to upload new image and then we need to redirect it to redirect to uh manage food page food so header location and then here we will cite you rl and then uh admin and then manage food dot php okay so that's it so let's go back to our manage food and let's uh display this session if available and after redirecting we also need to stop the process go down and stop the process because when images image fails to upload then we want one to update the data in database so that's why we need to update we need to stop the process so here we will use die function to start the process so let's go to manage food and display upload session if available okay so at the top we already have session call upload this was from add function but this will work for us so that's it we also checked the sessions okay so here we have uh this is image available here i will call it as uh tax a let's add a comment that this is uploading new image okay after image is uploaded new image is uploaded successfully then we also need to do h b this will be remove current image if available okay so here we will check with if if dollar current image is is not is equals to blank okay that means current image is available okay image is available not all of the food have images if we go back to our food here only one has image so that we need to check whether the selected so that we need to check whether the selected image has image or not okay so that's why we are checking here so and we are only uh removing the image if current image has image okay so call it image available yummy and we remove the image okay for that we will create a remove path path is equals to we are inside admin folder so let's get out of admin folder get out of editing folder go inside images go inside food and then inside food we need to give our current uh current current underscore image okay so this will get the current path to remove the image current image and then we need to remove the image for that we will use i will create a remove variable and i will use on link function to remove the image okay so here we need to give remove a path so that's it so this will successfully remove the image from our given path but we need to check check whether wrct the image is removed or not so if dollar remove remove is equal to is equals to false that means fail to remove image fail to remove current image in this case we will set a session and redirect to manage foot page management page and also stop the process so i'll create the session dollar underscore session uh we'll call removed as failed and let's create a system uh value tip and close the div div and add semicolon its class will be error class [Music] is equals to error and value will be failed to remove current image okay so that's it now we need to redirect to manage food relate to manage food and stop the process to restart the process we use dive function and to redirect the page we need to use header function header and then location we need to conquer net site url and then here we will go to admin and then manage dash food dot php okay so that's it so we have successfully uploaded i added the code to upload the new image as well as remove the current image after if everything work is working fine then we will uh we have done this part also so let's cut it from here so i'll cut it from here and we can add it here we can add it here okay we did both of this so our three step three is also done remove the image uh if new image is uploaded and current image exists so we have also completed step three now we need to go to step four okay so for step four we need to create sql query so i'll create sql3 and is equals to uh let's create sql query so update tbl underscore food set and then here we need to pass the value so we'll pass with title is equals to uh dollar ti title and then dollar uh not not dollar title and then yes c r i p t i and description is equals to dollar description after that we have price price price is equals to dollar price and we do not need to give a quote for price you know the reason right because price is a numerical value for numerical battery with no need to keep code we only give code for string value okay so after price what we have so let's check database title description price and then image name and category so image name is equals to dollar image name and then category for category what we have we have category underscore id so we need to give the column name so here get a c g o our category underscore id this is column name and here this is variable so for variable uh we have obtained the value as category i think so if we go at the top we are obtaining the category value as not from here not from here from here after form is submitted here so we have category okay so we are getting the value as category so we'll save we will pass it in our sql query so this is dollar category after that we have featured so tu already featured is equals to dollar featured and then last one is active is equals to dollar active okay so that's it this is the last column and after last column we do not need to add comma so here i will add where keyword where id is equals to dollar id okay so this will specify which row or which foot to update so that's it so this is our sql query now we need to execute the sql query so execute the sql query q uery and here we will create uh we will create dollar raise three to execute the query and we'll use mysql i underscore query and here we'll pass dollar connection and then we need to buy dollar sql3 okay so this is sql3 that's it after that we need to check whether query is executed or not check whether the query is executed or not uh so if dollar raise three is equal to is equals to true then query executed and sorry and uh food updated else [Music] failed to update food okay so that's it now let's add the session message so dollar on this question this is update and we need to pass the value so div slash div and then this class will be success so see la ss class is equals to success and here we will see the message like food updated success fully and then we need to redirect it to manage food okay so header and then location and dot site url and then admin slash manage uh food dot php okay so that's it now i will just copy it and we will add the same for else condition as well so i'll paste it here and this will be error and message will be filled to update food okay fail to update food so that's it guys so we have also done this section so i'll cut it so that's it let's save it and let's go back to our browser and let's check whether our update functionality works or not okay so i'll go to food order website manage food i will close this one and i refresh it and i will select on first i'll select on food with image so i'll click on update food and here we have everything now i will click on this uh autoform filler and this will change all the values and i will also select the image here so for now i'll select another image and open and i'll click on update food and oh i forgot to display the system message but if you see here everything is changed image name is also changed image is also changed and if we go back to our here in our food folder and we only have one image that means our current image removed and new image is uploaded okay so let's display this message so i'll go to manage food and display the update message as well so here i will check if it's set dollar underscore session and then update then we will echo echo dollar underscore season up up update and then uh onset dollar underscore session update okay so that's it let's save it and then go back to browser and if i refresh it i can see the message so here our message was seen and image is uploaded i'll refresh again and i will uh for now i will update this one so check the remember the values okay title price and image okay so i'll check this one update and click on form filler and everything is changed everything change i will select the image i will select this one and update food and here we have our image and if we go to our folder and here we have our two images okay so that's it guys so that's it guys we have successfully added the functionality to delete the food as well as update the food in the next video we will work on displaying or connecting front end and back end together okay so if you go to our front end which is local host and slash only food order not admin if you go to food order right now we are displaying all of this value these are static value in next episode we will work on displaying all of this value from our database okay so next episode will be very very important and very very interesting so i will see you on next episode till then stay safe take care bye you
Info
Channel: Vijay Thapa
Views: 15,193
Rating: undefined out of 5
Keywords: vijay thapa, software development, web development, programming courses, free courses, php, mysql, php tutorial for beginners, php full course, php project, learn php, food ordering system, food order website, food order website with php and mysql, how to create food order website with php and mysql, php project step by step, php project tutorial for beginners full, update in php, delete in php, file upload in php, file remove in php, delete file in php
Id: UJmVA3bR5HE
Channel Id: undefined
Length: 88min 29sec (5309 seconds)
Published: Tue Dec 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.