PHP-and-MySQL/C3/MySQL-Part-7/English
From Script | Spoken-Tutorial
Time | Narration |
---|---|
0:00 | In this part of the tutorial, I am going to give you a chance to create a simple program. |
0:06 | This program will allow us to select a name from a list. |
0:15 | Also it will allow us to update a piece of information and I am selecting the ability to update the name itself. |
0:25 | I will say the "firstname" in this for example. |
0:28 | Here, we can select from a list and then update that information. |
0:33 | I will modify this page a little bit to get rid of some information which we do not need. |
0:38 | We don't need this echoing here. |
0:40 | We are also going to change our form here, so we don't need this for now. |
0:47 | Let us delete that. |
0:48 | We are not going to need this either. |
0:52 | We will only need the firstname and lastname. Date of birth and gender don't really matter. |
0:59 | Let us delete this too.We don't need this either.... neither this. |
1:04 | Ok there we are. |
1:05 | This tutorial is not going to be exhaustive and is not going to be completely correct either. |
1:14 | However it is going to show you how to apply your records to html select boxes. |
1:22 | And also showing you how to update information based on what you selected. |
1:31 | As you see here, I am going to create some data inside the while loop. |
1:45 | We are going to create some html data here. |
1:47 | Here I am echoing out. Let me just stop here for the time being. |
1:55 | Lets go down here. |
1:57 | We are going to create a select area which is a select box. |
2:01 | It is a drop down box and for each of these boxes, we have an option. |
2:14 | For example this could be 1 or 2. |
2:15 | So lets come back here and "refresh button". Lets refresh this. |
2:27 | Let us get rid of this dialog box. |
2:30 | Here we have got 1 or 2. This is the piece of html here. |
2:35 | Here we are going to apply this and we are going to search for our records. We are going to give a name.......... in each of these option boxes. |
2:40 | I will give an option name to each record that we found. |
2:43 | If you don't understand this, it means inside the code which is going to be repeated for every record, outside the loop, over here, we want to echo the first part of our html code. |
3:00 | This will be "select" and the name of that is going to be "name". |
3:08 | Or rather let me call it people name. |
3:13 | After this, outside of our while loop, here we are going to echo out the ends tag. So let us type forward slash and select". |
3:21 | The reason for not incorporating this inside our while loop is because if it is repeating, then it is going to repeat the start and the end tags and not the option part, which we need. |
3:36 | Here the option part goes inside the loop. |
3:39 | Let me echo out, lets say the "firstname". |
3:41 | And what this does is, for every record, it is going to echo this option code |
3:48 | And if you remember down here, we had "option" and "option end". |
3:52 | This was being repeated over and over again. |
3:56 | Now we have the "select" part here and the "select end" here. |
3:58 | We need this echoed once, this echoed once and these echoed for every record in the database or in the table. |
4:10 | You can verify this by refreshing. |
4:12 | Oh! Where's our code gone? |
4:14 | Looks go back and find where the mistake is. Actually we need to change this part here - the "if" statement. |
4:25 | We are not looking for our "submit" button anymore, so we can delete that. |
4:28 | So now, presuming everything is alright, we will refresh and we have a list box of all the first names of the records in our database. |
4:39 | Now I want to make this look better and so I will say "surname" or "lastname" in the code. |
4:46 | There you go! Refresh. This is a really easy way to use html code, is'nt it? |
4:52 | Now we are going to talk about "option". |
4:52 | We need to have a name for each option and the name for each one will be "id". |
5:00 | If I click on refresh and come on to my page source, you can see that we got 1,2,3,4, in each one here. |
5:13 | This is really useful because now we can update, unique records rather than just going by the name. |
5:23 | So here, I will start to create my update form. |
5:27 | I will put an input box after my "select", and this is going to be "text". |
5:33 | The name will be to change. This is what we are going to change by. |
5:40 | Next we will create another button or another input element called the submit button whose value will be "change". |
5:53 | Here I will just change the first name for now. Just for an example. |
5:58 | So here is the basis of our form. |
6:00 | We have got the "name" here and what we want to change it to. |
6:04 | So here I will change it from "Alex" to "Alexander" and then click on Change. |
6:10 | At the moment nothing is happening. |
6:12 | Now what we need to do is put this inside the form so that I can end my form. |
6:18 | Over here, it is getting kind of messy but hopefully you can see whats going on. |
6:23 | Let me scroll down over here. Up here we need to start our form. |
6:22 | The action will be the page we are on at the moment which is "mysql dot php". |
6:33 | Infact I will do it on another page. |
6:36 | So rename it to "mysql update dot php". |
6:40 | This will make it a bit easier for you to see and a lot easier for me to write. |
6:45 | Then by refreshing that, we can see that we go to the new page which isn't found at the moment. |
6:52 | I am going to create that inside here. |
6:55 | We will save that straight away as "mysql underscore update dot php". |
7:00 | We need to start our php tags. |
7:02 | We need to require our "connect dot php" because we are connecting to our database again. |
7:14 | We also need to get our value of the name that we are changing. |
7:17 | So we will call our select name peoplename. |
7:20 | So here we will type "peoplename" equals POST and peoplename. |
7:28 | That is the name of the html element we are taking. |
7:32 | This is going to be called 1,2,3. |
7:36 | This is our id which is inside our database. |
7:39 | "tochange" is the field in which we have ready to type our new value in. |
7:46 | Here I will code a brief if statement just to say if peoplename and tochange. |
7:57 | This makes sure that we have got both our values there. |
8:00 | Then what we will do is type "change" equals "mysql query" and that is simply "UPDATE people", which is the name of our table here. |
8:17 | "UPDATE people SET firstname equals tochange" where "firstname equals".... |
8:30 | No, in fact we are not..... We are changing it by "id", isn't it? |
8:39 | So we type where the "ID" is equal to the value of this "peoplename". |
8:52 | Right, lets go back. |
8:57 | Lets say I picked "Kyle" to change the name of "Kyle". |
9:02 | The name of this is 2 so the "peoplename" is also 2. |
9:05 | So we are changing this to this where the id is this. |
9:10 | I will show you that in the next part of tutorial because I am running out of time. |
9:14 | So I will see you there soon. This is Evan Varkey dubbing for the Spoken Tutorial Project. (Script contributed by Juanita Jayakar). |