Hello Everyone! In the previous tutorial we had selected the data and we successfully displayed the data to our page.
Let me take you back to this page here.We can see that everything is working fine.
We have all our data here.
The next thing we will learn is how to specify somewhere that the user themselves can enter and specify data.
To do this, I will delete all the codes in my page except "connect include".
If I don't connect to my database, its not going to work.
There is a lot of extracting and some other things to be found.
I will retain firstname, lastname, date of birth, and gender either male or female.
Below here, I will create a form.
This will be an html form, so we need to start and end our tags.
Our "action" will be "mysql dot php" and our "method" is going to be "POST".
Here we will create a form in which the user can specify a name.
For the name we will use our "surname".
You can use any one of them. For example, your date of birth or gender to search.
It is your choice.
You could even use 2 fields to search.
I will now show you how to use 2 fields so that you know that it is not very complicated but easy.
Here I will type "firstname" and I will create an "input" box of type "text " and the "name" of this will be "firstname".
We will create a break here and type "lastname".
Next we will create another input box by copying the previous line and pasting it here.
So this line is exactly the same but instead of "firstname", we type in "lastname".
We will type "input" type equals "submit" and the "value" of that will be "Get data".
Hence we have taken the "firstname" and "lastname" and the "submit" button.
If I refresh this, we can see "Firstname" and "Lastname" has appeared.
I will delete this for now and to do this I will check if the "submit" button has been pressed.
To do this, I will first name the "submit" button as "submit".
Then I will type in an "if" statement - "if dollar underscore POST submit".
If the submit button has been pressed, start the block.
This is the block of code to be executed and we end our block where we want this to end.
This is after this curly bracket here because that is a part of our "while" statement here.
We don't actually need our while statement for this. But I will still keep it for the sake of it.
I will now type "grab POST data" and "dollar firstname".
So this is "dollar underscore POST firstname" and then I will type "lastname".
I will copy "firstname" and paste it here and change it to "lastname".
So we have got our "firstname" and "lastname".
I will type "echo" and this message "Record for firstname" . On second thoughts, let us not do this at the moment.
I will go straight to my query here.
If you remember, I had mentioned that you can specify what data you want in particular.
For this, I will type "SELECT star FROM people WHERE firstname equals Alex and lastname equals Garrett".
I had not shown this inside this piece of query where we are selecting.
I had shown it in the update where we can update where the firstname equals "Alex" and the lastname equals "Garrett".
Now we will select everything we want from our table here and we will say select everything where our firstname is Alex to lastname is Garrett.
If we look at our database here, we are searching for our firstname and matching it to "Alex", searching the surname and matching it to "Garrett".
We are selecting this whole row of data over here which is highlighted in pink at the moment and we are just selecting the whole data from here.
From the date of birth to gender, my id to my firstname and lastname.
So we have understood that this will select only one record at this time, therefore we don't need to order by the "id".
But I will keep it like that anyway, since there is only one record.
Ordering is not an issue, so we can just leave it that way.
Ok so we are running our loop, we are selecting each part of data here and we are changing our male to "male" and female to "female".
The echoing out our data based on this query because this data based query is specific to my first and surname.
Then we are only going to echo out the firstname and the lastname over here that has been found, which is my first and lastname but by changing "Alex" here to "firstname" which is here.
Now that's a repetition. This is a double variable here.
We have the "firstname" and so here, I will name this as "firstname underscore form" and "lastname underscore form"
We will select from where the firstname has been posted equal to this and the lastname that we posted equal to lastname form.
So This is the data that's come from our form.
so if I were to type "Alex Garrett" in my html form and submit it then this will equal "Alex" and this will equal "Garrett".
Our query will return only one result because at the moment we have only one person named "Alex Garrett".
So it will take all the data for "Alex Garrett" and check for female or male and then echo out this particular message.
So if I come here and refresh at the moment there is nothing because no data has been specified in these form variables here.
Let me show you. These are empty therefore we are selecting "star" from people under this condition "WHERE the name equals to nothing and the lastname equals to nothing".
That has not returned any data at the moment because what we got in our firstname and lastname are actual peoples names.
Anyway now I will just type in a completely random name.
So lets say "David Green" and click get data and nothing has happened, ok?
If we had an error message at the end over here, this is our query written over here, I could say "or die mysql error".
I go back here and say "David Green" and click get data and oh! we don't have an error!
Oh yes that's because the structure of the sql code correct and so we don't get any error.
I was just testing that out.
Now lets suppose that we do have a name that's in our database.
Lets say "Alex Garrett" and we click "Get data".
We are presented here with alex garrett was bron blah blah blah and is male.
Lets type "Dale Garrett" and click "Get data" and we receive that information from the database.
So you can see that this is very useful to incorporate forms into our data.
I will stop here and go on to the next part where I will be talking about how to update records using this method.
Many of you would be able to do that on your own by now but I will take you through it anyways along with a few other useful things.
Ok so, see you soon. This is Evan Varkey dubbing for the Spoken Tutorial Project. (Script contributed by Juanita Jayakar).