Summary
Keywords
Full Transcript
Hello guys welcome to the next video on Android MySQL database tutorial for beginners. Till now in the series we have seen how we can connect to our MySQL database and how we can create a simple login page and query our MySQL database using our Android app and PHP script. Now in this video I am going to show you how you can insert data into MySQL database using our registration activity on your Android app. So let's get started we are going to create a registration activity in a while but first of all what we are going to do is we are going to create a PHP script for registration.
So we are going to create a new PHP file. So first of all what we are going to do we are going to first of all if your VAMP server is not started just start your VAMP server because I will be using this localhost VAMP server for this demonstration. If you want to use online database it's perfectly fine. And then go to dub dub directory here in VAMP server and in here our con.php and our login.php is already there.
So we are going to create a registration.php also. So new file and we are going to save it as register.php and just save it. And then you can see in the folder this registration.php is created. Now in here as we have done in login.php we need to add this connection file first of all and then we need to write a query to insert the data and then run the query using PHP script.
So I am going to copy all the code from login.php and I will go to register.php and in here I am going to paste it. And now in our database if you remember let's open our database so open VAMP server.phpmyadmin and in our database let's see what are the column names. So this is our table name employee data and the column names are names, surname age, username and password. ID is auto incremented so we don't need it.
Right? So right now we have two variables. We are going to create two more or three more rather. And the first is the name variable.
Second variable I am going to name it as surname. Third variable is the age. Fourth variable is the password. And the fifth variable is the user name.
Right? So and I am going to just move this above so that we have the sequence username and password. Right? Now create the query.
Let's leave these things as default for now we are going to change the name here later but let's create our query first. So I am going to just delete all the query and we have mySQL query variable and in here we are going to create a new query. Now to insert data into the MySQL database if you know we need insert keyword. Right?
So just write insert into your table name. Your table name is if you remember employee data this one. So just give the table name and then give the column names in which you want to insert the data. So we have column name name and surname.
Third is age. So just write age and let's see fourth and fifth column username and password. Okay? So just give the column name username and password.
Use the name comma password. Okay? And now we just need to write values. So just write values and then in the bracket you need to pass your values.
So our values are in these variables. So just give a single quote here single quotation mark and inside the single quotation mark you need to give your variable name. Right? And you need to give it in the sequence.
So if the first name here is name, then you need to pass the variable for the name itself. Right? And then once again comma and do the same for the rest of the variables. Okay?
So I will just copy and paste it for more times and place of this variable. I will pass surname here. Second variable and then the age. Third variable and the username is the fourth and the password is the fifth.
Right? So just copy this password and paste it here. Okay? And let's align our code a little and then this is your query.
Right? Now in order to run your query, we have one more way of running our query. So just take your connection variable from your connection dot PHP. And in place of this line, we are going to just write gone and then we will call the query query method from here.
So just write query. And then we are going to pass our query here and then we are going to equate this value. So just write triple equal to instead of double, you need to use triple. Right?
True. Okay? And you can give any message as you wish. So instead of this echo, you can write insert successful.
Right? And in place of this else echo, what you can do is you can write something like this error dot this will be a concatenation operator for us. Your query names or query name variable is my squelquery dot break. This will break the line.
And then just write gone error. So this is going to show you the error if any error exists. Okay? And I forgot to tell you in the last videos.
If you are opening the connection from my squel database, then you need to close it at the end of your PHP file. So just at the last, you just need to write close. Okay? So just call close here and just give these brackets here.
Okay? Now let's try to check if the PHP code works or not. So I'm going to just give us some static values here. Some random name and the surname instead of this post and the age, for example, 22 user name, for example, what can be the username ABC and the password.
This is just for checking. We are passing these static values. Okay? Now if you remember how we can open our registration dot PHP file, you just need to call local host and then the file name.
So just write local host. And then instead of this long dot PHP, we will just write register dot PHP. Okay? And then just run this and it says insert successful.
So right now you can see our my squel database table has two columns or two rows rather. And when we refresh our table. So just click this one to refresh and now we have the third row. Okay?
So the ID username or name surname age and username and password. So we know that our PHP script is working. Right? Now once we are done checking, we can just pass this post method variable here.
So instead of this, we will just replace them by post everything here five variables. And let's say the name of this post method is same. So username for username and password for password age for name for name and surname for surname. Okay?
This should be username. Okay? So that's your registration dot PHP. Now once you are done with this, we are going to create our new activity.
So go to your Android studio. And in here, go to layout and let's create a new activity. So new and just search for activity. We are going to create a blank activity here.
And let's name it as register and then click finish and our register activity is created. I'm going to just delete this component from here. And now what we need to do here is we need to place some added text and a button. Right?
Let's take five added text and a button. So I have created these five added text and I have given some hints to these added text. So if you don't know how to give hints, you just need to select and search for hint property here and change the hint property. Okay?
So five added text and the hint and the name of these added text, I have changed to ET name, ET surname, ET age, ET username and ET password. And I have taken one button which I have named it as register and the ID of this button is BTN RG for button register. Okay? And now I'm going to go to the text section of this activity.
And in the button element, I'm going to add on click method here. So just write Android, cool and on click click. And in here, you can just write on R E G for on register. And just copy this method from here and go to your register dot Java class, which you have created.
And in here, we don't need these method. We just need on create method. And in here, we are going to create a method for on register. Right?
So public and this will be a void method. And the name is same as we have given in on click method. And this is going to take view once again view. And inside the method, we are going to write some code.
But let's first define some added text variables here and some string variables here. So just write added text. So I have defined these five added text variable and I'm going to define same string variables also. And I have defined these five string variables for the five columns.
Right? Now I'm going to go inside my on create method and I'm going to cast these added text one by one. So name is equal to bracket added text. And then you just need to write find view by ID.
And then R dot ID dot the ET name. Right? So ET and then the name. Okay?
So do the same for the rest of the five. Once your type casting of your edit text variables are done. What we are going to do is we are going to go to our main activity dot Java file. And in here, if you remember in the last videos, we have created on log in method.
So just copy the code from on log in method. And this method what it was doing basically is it was taking the text from your edit text and then passing these values in your background worker object. So just copy the same code from here and then go to your register dot Java file. And in on register method just paste this code.
And obviously it's going to give this error because we need to change these values from these added text values. So I'm just going to copy these a few times five times to be precise. And then just change these values name surname age user name and password and also change these variable names. So we have defined these variable names here.
String name string surname string age string username and string password. So in the last videos, we have defined these string variables locally, not globally in your main activity dot Java file. So we are going to do the same in your register dot Java file. And we can delete these global variables, which we have declared.
Right. So local is also OK in our case. OK. Now you can see here our background worker execute takes some variables.
And basically we can pass these variables as an argument here. So we will pass these variables one by one. So first is string name second is STR surname third is STR age fourth is STR user name. And fifth is STR password.
OK. And you can see here our type here in the last video we have defined for login. It was login. So this is the last for the register we will define here register here.
So this on register method is done. Now how we are going to open this register activity from our main activity. So what we are going to do next is we are going to go to our activity underscore main dot XML file here. And we are going to take one more button.
So just search for one more button. And in here we are going to change the text here register and the idea for example, BTN RG. And when we click this register button we want to open our register activity. So go to the text and in the button of this register button.
So we are going to create on click method and we will say open register. So just write open RG. OK. And create the same method in your main activity dot Java file.
So in here we are going to create a public void open register method and just pass your view and view. And basically inside this method we want to open or start our activity. So just call start activity method. And it's going to take new intent.
Right. So just write new intent. And this takes two parameter. First is this and the second is the class name.
So register dot class. Right register dot class. OK. So what this basically going to do is it's going to start the activity register.
So what this start activity is going to do is it's going to start the register activity. Now once we are done with all this code now we want to go to our background worker to identify if this request is login or register request. Right. So first of all we will create a URL.
So let's copy this variable login URL. And we will name this variable as register URL. And the last video I have changed this IP address to my website name. But you can you know for the local host you can change it to your local host.
So I'm going to once again open my command prompt. And in the command prompt I'm going to just write IP config. And it's going to give my IP here. So my IP is 192.168.1.6.
Right. So I will just write the same IP 192.168.1.6. And I'm going to just paste the same IP here also. OK.
And this will be our register dot PHP. If you remember we have created this file register dot PHP you can just copy the name from here and you can paste it here. Right. Now we want to distinguish the type which we want to pass.
So in the last video we have defined this condition if type dot equals log in then just you know execute this method. Otherwise we are going to create one more if condition. So go at the end of this if condition. This is the end of this if condition.
And just write else if type dot equals register. Right. So let's just complete the if condition here. So instead of log in if you go to register dot Java we have defined the type register.
Just copy the same name from here and go to your background worker dot Java and paste it here. Right. So if the type equals register what we want to do. So basically we want to do the same we want to post the request to our PHP page.
So we will just copy the same code as we have in our if condition for log in to the end. And this is the end I think for the if condition. So just copy the whole code from here and paste it in your if condition for register. Okay.
So paste the same code. Now at the top here we need to define five variables instead of two variables for registration. One is name surname age username and password. Right.
So I'm going to define these variable names. So just copy them five times. And first is the name second is the surname third is the age fourth is the username and fifth is the password and index you need to change one to this will be the third index. This will be the fourth index and this will be the fifth index.
Now next line you can see we have declared the URL variable and we have passed log and underscore URLs instead of this log and underscore URL. We need to pass our register underscore URL which is this one. Okay. So copy and paste it here.
Okay. And the next line will be the same and we need to go to our post data variable here and in here we were creating our post request. Right. So we need to modify our post request a little.
So what we need to do is I will just copy this one line three more times. Right. So just go down one two and three. Okay.
So this is one request separated by end and then second third fourth and fifth and just give the plus sign here concatenation sign for example and something like this. Okay. So this is our request. First we will pass the key and then we will pass the value.
So the key we have defined in our registration dot PHP. Name surname age username and password. These are the key in the post request. Right.
So give the same you know a keys here in your double quotes here. Okay. So name it will be surname and then age then username and password. I hope the spelling is the same username and password age surname.
I think it's same. Okay. Now the values. So values we have defined here string name.
So just give the name. Then the surname then the age and then the username and the last one is the password. And let's close this bracket. I think and from here just remove this.
So this is our post request. Right. And basically everything will be the same. So it's going to return the response and it's going to show this response in the alert box.
So this is our code for the registration. Now let's run our code and let's see if it works or not. So our app is running now. So this is for the login and we need to click registration to go to the next activity.
So just click register and it's going to open this register activity. Now let's open our PHP my admin and let's see how many rows it has. So right now it has three rows. You can see here.
I can refresh it and I can see three rows. So let's give the fourth row from here name for example. So the name and I'm going to press register button and it gives insert successful and this message is coming from your PHP file. You can see this insert successful.
And go to your app once again and click anywhere else and let's go to our PHP my admin and let's refresh this page. So refresh it and I can see the fourth row added to our table. But I can see one problem here. You can see in the name it's two line you know name right.
So one line and two line and this may be due to because we have changed the line in the query here. So you just need to keep your query in one line and save it. And let's insert the data once again. So once again some name.
And let's click the register button once again and insert is successful. Go to the PHP my admin refresh it. Now everything is fine. So in this way you can create a register activity in your Android app to insert the data into the MySQL database.
I hope you enjoyed this video. Please rate comment and subscribe and bye for now.
