hello everyone! my name is oum saokosal. once again i'm going to talk about how toconnect android with php mysql. in this video i'm going to talk about howto insert, update delete data from android into php mysql.
Android context, this is the part two if you are new to thisvideo i suggest you to go back to my youtube channel 'oumsaokosal' and watch the firstvideo, "how to connect android with php and read data and pass data into the json parserand by into listview and also you will learn how to use picasso universal image loader.
in that video or in the listview my libraryit is in generic asynctask v2. please don't use the first one because itis out of date, so please use the ones in two and also in the listview how to use jsonconverterto convert json text into json objects. and i introduced you how to use fudapter toapply the data into custom listview and lastly how to use picasso and universal image loader. so if you are new maybe you have to watchthis video first but if you're an old customer, let's go with me. so this is the last video that we did, wefinally got an image display in the text but there is something that i want to show youas well because there is something wrong with
that. first of all the padding is not right likethere is a space below and after so we have to make sure that there is padding. so you go to detail activity and here theimage loader you can say "ivimage.setpadding(0,0,0,0)" like this and you call "setadjustviewboundand you set true". and more thing about detail as well, if youdownload mysql you will see you cannot click the back button right like this so what youhave to do is to go to content detail and go to text you need to add another relativelayout. actually you just copy and paste from thisone and you know and paste it here because
three name spacing here, android apps, toolsand also the padding. and this is a very important one: the layout,the context showing and then you have to put the scrollview and relativelayout. because we want to use the edit text` andwe want to design our edit text so we have to have another relative layout that we don'thave to out anything like this just only width match parent and height match parent. to test our program again, see there is nopadding but this one has some padding so we want to go back and change something in thelist activity as well. to change something like this, ok now thereis no padding ok.
so let's continue, in order to insert thedata into mysql actually you would have to have like a webpage you know, like you canwrite in php, python, ruby but my last video used php so then let's use php again. so for the insert we have to insert the data,the name, the quantity, price, image url into the database. so let's look at the database viewer: customerand product, see: name, quantity, price, image url. we don't need to add the pid because it isthe auto increment and it is a primary key so we don't want to touch it.
so our code needs only the name, quantity,price, image url and we have to check in with isset and afterwe get the data, we insert the data into mysql and using mysql and if the result is greaterthan zero it means the data is inserted, then we can display success. and i want to show you one trick here: itis the mobile, i check it like if the mobile is set and mobile is equal to android thenthe word is success and we exit. if not then we display normal echo. why have to do that? because i want to use insert from webpagein a user browser as well as from the android.
so let me test it. so you go to "localhostcutomerinsert.php",actually it is a physical file here, the insert is here. and if you type it correctly you know, let'ssay: coke, 10, 5, coke, jpg. so the data now is inserted correctly intothe database and this can be used in a web browser as well. but we don't want to display the html partin the android because we want to get only the word success or no success. that's why i have to have another conditionand i invented like a secret code for that
like mobile and mobile android and the echois success and then we exit. it means we will stop the code from here,we don't display any symbol below here. okay, let's check the database first, nowas you can see the new record is already added but there is something wrong with the imageso i have to change the image url. so i change the image to something like thisotherwise it's going to show blank image. so let's go back to android and we have tocreate another activity, blank activity and now you call insert and what is the main page,its list. okay for the insert it's very similar to thedetail one so i just go here and copy all the code.
so let's copy from relative hereto the bottom,we pass round here. you can have another relative layout and relativelayout it doesn't matter, should be fine. yes, so we don't want image to be left asimage and change it to insert and "btninsert". that looks good except we have to delete thisone because it's not a part of our lesson. so you go to activity insert and you can deletethis code because this image is left to the activity so we have to go insert activityand delete this part. and if you want to delete from the log inas well let's go and delete it. activity log in delete this one go to loginactivity and you delete this code as well. and we want to open the insert activity fromthe list activity: this is the list activity
and this is the activity list, we want toclick this button and we go to the insert activity. so let's change this to something like that(ic, add). but as you can see the button is green whichis not good. so let's download the good one, you can goto material design icon so this is the official website that you can download any icon foryour application. just find the plus icon, okay after you enteredit you can see this is the plus one, click here and you click icon and you download thewhite or the black. now we need white one and i need a forty eightpixel, actually i already have one, this one
should be fine, just copy. you go back to android and you go to drawableand paste. because i already have one just lets overrunit, this is the plus sign you can double click on this, you can see the plus sign and yougo to text so add drawable/plus then you can see the white plus sign like this and thenyou want to click this image and then we go to the insert. so we go to list activity, so you go herethis is the event for the fourteen accent button and you can just delete this and youadd your code. now we want to open what we call intent andwe want to go to insert activity.class then
we start activity in, let's run it. as you can see now we don't have any iconover here, just have the correct password. now we have the plus sign, click on it, nowwe can see the insert activity. okay now we have to write some code in here,so first we need to code all the edit text and buttons so we let it like this and code etname, etquantity, etprice, etimageurl and a button, btninsert and then we get a view by find view by id,something like this and then you just code a button set and click this now and ok. now in this even you can call the code acting,respond, this is the live read that i use
in my first video, you should have it if youadd the library properly. if you don't know how to do that i guess youcould go back and watch the first video first. then we code taskinsert, new, post andthis and postdata and new asyncresponse, then we need to add data into the post data here,so declare hashmap postdata, new hashmap, postdata put. okay here is the very critical thing so topost to the data we need to know what is the key. so this is the key when you send the datato this page we have to match this key exactly the same, so usually you just copy and pastesomething like this name, quantity, price,
image url exactly the same to this one andthen you can call the text from the edit text above, name.gettext().tostring(). now we put the data that you enter from theactivity into the postdata and the postdata will send to the webpage. but we have to code the web page. what is the webpage? we call execute and our webpage is http://10.0.3.2then customer, insert.php. what is our result? our result is success.
but to get a success we have to set the datamobile for android so this is the important part (mobile here is android) so we use loghere, you should declare here a final string and i call log and the name should be thename of the class so it's very easy to do this by using the log like this, pass thelog here and the result is string, now you can see the log in here insert, if you wantto give a hint to edit type you can go to the layout and view it but now let's testthe data. it should be the name, let's code pepsi, insert,okay. now you can see that data now is success soit's very easy to do the web like this and you don't have to delete the log because it'snot appear to the user and it is for the debugging
only so just leave it here. so now the result is success and we can say'if it contains success, i explain it in my last video why you should use contain becauseequal means that is must exactly the same and start with, you can start with successbut sometimes there is some space before you know, you can use contain its easier. here you can toast it, so after your successyou wanna go back from insert activity to list activity so you call intent somethinglike this 'zenya, imageurl,' so insert again so if it is successfully inserted should bethe word insert successfully and you go back to the list.
okay so this is zenya so image might be wrongit's ok. so now you want to edit the data in detailactivity so it's quite similar to this one. so you call button 'btnupdate' and right here'setonclicklistener this, make detail activity implement onclick listener'. okay, the code is very similar so let's copythis, so i want to change taskinsert, i go to refractor and rename and i call it taskupdate and this one is detail activity, copy okay and update successfully and the log sowe go and copy the log here, here is detail activity and lastly we change the update. so we have to have another page update hereso for update it quite similar ok the mobile
thing because we want to test if it is a mobilethen if it is success then we equal the word success and then we exit. otherwise we just display the user html butfor now i don't want to care about html anymore just care about this to speed up our process. so it should be fine now, it's very similarto mobile pid. so this one you need pid the reason why thereare gonna be pid because the update cannot update without an id. so we have to send the id into the post aswell 'put and textpid' and where we get the id is from the product here so this productis declared inside (inaudible) you have to
declare it outside private product likethis and we delete declaration inside on pid. and now the product can be used into (inaudible). so you call 'product.pid' now product pidis not a string it is an integer and you want to pass it into the string so we have to makesure it is a string so we just put the (inaudible) and plus it and it can go back to the string. so it should be fine now. with cat and change to cat, 1, 101, updateokay now is successfully. so now we have to delete this is update, thisis just for testing so we have to delete it and we don't have to restart the android becausephp and android is different.
should be fine now. and lastly you want to delete from the listview,a long click on this there should be a menu, its called context menu. so to do that you go to listactivity and hereyou say 'register for context menu' and it will ask you which view you want to displaycontext menu. actually we want to display in the 'lvproduct'so 'lvproduct' is here so it should be fine and then here you say 'on create context menu'and another method 'oncontext icon selected' so this is the menu you want to display soto that you go to project and you go to menu and here is menu for the main.
you don't have the context menu so to do thatjust copy and then paste here and i will say 'list context menu' so the context here shouldbe the 'listactivity and the id here is menu update order one', the title is update, copy,paste 'menu remove, order 2, remove' and you go back to the list and you say 'menuinflator inflator, get menu inflator inflator.inflate' and then which menu. so now you want to call the 'r.menu.listcontextmenuand menu' so the last menu is a context menu here, so i call the lvproduct view right hereso i need to copy this line and paste it before i call the register for context menu. yes now you have update and remove and youwant to click update, you want to go to update
and you click remove, you want to remove thisitem from the list. so that's why we need another methods called'all context items selected 'and you go to call' item.getmenuinfo, and the return partis adaptor context menu' and you have to cast it to adaptor context menu info likethis and then you have the information from the item that you clicked. for example 'info position' so this is theonly thing that you need from there the position. so we can call it from the adapter, so whereis our adapter, so adapter is here so you need tp declare it outside as well over here,so that you can get a product from there. i call it like the product and you call 'adapterget item' and the information from there should
be fine. so this line basically you get a product thatyou click on so the information will tell the adapter which item that is selected sofirst maybe we just do the log see which is coming along, selected maybe 'name' easierto see. so here in debug you have to view the nameof the class of course this is the log name and let's see, click on update. okay now is pepsi so it means that you getan item off the pepsi. click here it's a cat, okay now we get theproduct, so we have to make sure that we select on a menu.
as you can see even though you clicked onupdate it's still cat and remove its still cat. it's ok now we get the item but which menu? so we got to test the menu which menu youwant, so you say 'if item.getitem id r.id.menu update' ok this is for the update menu, 'elsefor the menu remove', so for the update its easy because we just open a detail page 'intent,in. new. in' from listactivity and you go to detailactivity'. for the remove it's a little bit complicated,you want to remove which item, so now we get the item.
and we have to make sure that you don't wantto delete it accidentally so you may want to have another alert message. so you call 'alertdialog. builder and you call alert new, alertdialog'so you call alert dialogue like this and then you set some text, maybe a message so youwant to ask them do you want to remove? then our product name and after that we call 'removeand new onclicklistener and the button 'set positive and negative' and this one cancelbecause we don't have any even just leave them null' and lastly 'alert.show'. so here is the place that you want to writethe code but then first we do run and test
it. log in again, click on the cat, update, nowwe go to detail page and we don't have the data because we don't put any data here sowe call 'put extra' and if you go to detail page and you can see we have the 'serializableextra product' so write in the product and you can call the 'selected product', shouldbe fine. cat again, update okay here we go, we gotour information, you can go back here remove and you want to remove just click ok. right here we have to tell them that we wantto remove this item. so to remove that we have two things, firstwe remove from the list and second one we
remove from the database. so first let's remove from the listview first. to remove we have to say 'product list. remove and you can say selected product'. but then we have to call the 'adapter notifychange' so something like this should be fine. test it again. now let's delete this one, remove yes, andremove this one yes. okay let's look at the database. press, as you can see there is no data deletedhere because we don't remove from the database
just remove from the listview. so we have to go back and add our code, sowe have to call the postresposneasynctask again, so this is the code post response asynctaskand we have to have another php to delete. so this is the page to remove a php. for the remove we don't need anything exceptthe pid, the mobile is the thing we want to embed. so the list from table where pid, for pidit is greater than zero success. so just copy this code and you go to thishere, and we don't need this one and 'product selected pid and this one is listactivityand you say delete successfully and you don't
need to go to anywhere just here, remove. i got some problem here because i call pidwhich is different from this one, so just change to 'pid'. okay let's run again, okay let's delete` thefirst one, remove, yes, go back to the database. okay now it's removed. let's test another one, maybe in the middle.
test the test 2 remove and you see test2 now,browse again, yes. now you reach the end of the lesson today,if you enjoy it please subscribe my channel and i will see you in the next video.
thank you.