Difference between revisions of "PHP-and-MySQL/C3/MySQL-Part-3/English-timed"
From Script | Spoken-Tutorial
Line 4: | Line 4: | ||
|- | |- | ||
|0:00 | |0:00 | ||
− | | | + | |Hi, welcome back. In this tutorial we will write some data into a database. |
|- | |- | ||
|0:06 | |0:06 | ||
− | | | + | |To do this we will use our "mysql query" function. |
|- | |- | ||
|0:10 | |0:10 | ||
− | | | + | |Now you can see here that we have our... records.... |
|- | |- | ||
|0:16 | |0:16 | ||
− | | | + | |I am redoing this tutorial because the first time I did it, it did not work. |
|- | |- | ||
|0:21 | |0:21 | ||
− | | | + | |So first of all, I will just delete this data here. |
|- | |- | ||
|0:29 | |0:29 | ||
− | | | + | |Well... ok... So we have a blank table, we have no data in our table whatsoever at the moment. |
|- | |- | ||
|0:36 | |0:36 | ||
− | | | + | |we can see that there is nothing in here. |
|- | |- | ||
|0:40 | |0:40 | ||
− | | | + | |These are just our field names here. |
|- | |- | ||
|0:43 | |0:43 | ||
− | | | + | |To begin with, in here, lets just comment this. |
|- | |- | ||
|0:47 | |0:47 | ||
− | | | + | |So "write some data". Then we will set up a query that will write data. |
|- | |- | ||
|0:52 | |0:52 | ||
− | | | + | |So... "write" and we will use "mysql query" function. |
|- | |- | ||
|0:59 | |0:59 | ||
− | | | + | |And this takes exactly 1 parameter which is our sql query. |
|- | |- | ||
|1:01 | |1:01 | ||
− | | | + | |To do this, we will type "INSERT" to insert data. |
|- | |- | ||
|1:06 | |1:06 | ||
− | | | + | |We are going to say "INSERT INTO". |
|- | |- | ||
|1:09 | |1:09 | ||
− | | | + | |Now the reason I've typed this in capitals is because its sql code. |
|- | |- | ||
|1:14 | |1:14 | ||
− | | | + | |If I type anything in uppercase it means it is sql code. |
|- | |- | ||
|1:19 | |1:19 | ||
− | | | + | |If I type anything in lowercase it means its either a table name, database name or it is data that I'm writing to the database. |
|- | |- | ||
|1:30 | |1:30 | ||
− | | | + | |So "INSERT INTO people" because that is our table name here. |
|- | |- | ||
|1:34 | |1:34 | ||
− | | | + | | "INSERT INTO people" and then "VALUES" and then in brackets we will create a little area for each value. |
|- | |- | ||
|1:40 | |1:40 | ||
− | | | + | |So we got 1,2,3,4,5. |
|- | |- | ||
|1:46 | |1:46 | ||
− | | | + | |There are 5 fields so we need exactly 5 pieces of database written here. |
|- | |- | ||
|1:50 | |1:50 | ||
− | | | + | |We need the id, firstname, lastname, all the way down to gender. |
|- | |- | ||
|1:55 | |1:55 | ||
− | | | + | |These are created in or created using single quotes each separated by comas. |
|- | |- | ||
|2:03 | |2:03 | ||
− | | | + | |The reason we don't use double quotes is because we've got these at the end or rather start and end over here. |
|- | |- | ||
|2:10 | |2:10 | ||
− | | | + | |We don't need to insert our id here. |
|- | |- | ||
|2:13 | |2:13 | ||
− | | | + | |Our next one is firstname - so I'll say "Alex". |
|- | |- | ||
|2:17 | |2:17 | ||
− | | | + | |My lastname I'll say is "Garrett". |
|- | |- | ||
|2:20 | |2:20 | ||
− | | | + | |For my date of birth I'll create a date function which equals to a variable "date" |
|- | |- | ||
|2:26 | |2:26 | ||
− | | | + | |I'll put this in the particular structure. |
|- | |- | ||
|2:28 | |2:28 | ||
− | | | + | |We can see from our database over here that when we go to insert a value, we can scroll down and see that our calender function has dates on it. |
|- | |- | ||
|2:39 | |2:39 | ||
− | | | + | |So on clicking 23rd, we can see the structure this field takes. |
|- | |- | ||
|2:45 | |2:45 | ||
− | | | + | |Its the year in long format. |
|- | |- | ||
|2:47 | |2:47 | ||
− | | | + | |Next is the month and then the day. |
|- | |- | ||
|2:48 | |2:48 | ||
− | | | + | |So 2009 02 23 which is 23rd of the 2nd, 2009. |
|- | |- | ||
|2:54 | |2:54 | ||
− | | | + | |So what we can do here is we can structure our date function in capital Y m and then d using hyphen in between to get the structure we need. |
|- | |- | ||
|3:05 | |3:05 | ||
− | | | + | |So this will be structured like that. |
|- | |- | ||
|3:09 | |3:09 | ||
− | | | + | |This will equal to this and that will be the current date. |
|- | |- | ||
|3:13 | |3:13 | ||
− | | | + | |Using the date and presuming that is in the structure of our date, we can insert it into our table here. |
|- | |- | ||
|3:24 | |3:24 | ||
− | | | + | |The last one is gender and since I'm a male, I'm putting in "M" for male. |
|- | |- | ||
|3:28 | |3:28 | ||
− | | | + | |Presuming that will work, we can run this. |
|- | |- | ||
|3:30 | |3:30 | ||
− | | | + | |But before that, we could say "or die" at the end followed by a mysql error. |
|- | |- | ||
|3:38 | |3:38 | ||
− | | | + | |I will skip that for now but feel free to add them if you like. |
|- | |- | ||
|3:44 | |3:44 | ||
− | | | + | |Ok so refreshing our page. |
|- | |- | ||
|3:48 | |3:48 | ||
− | | | + | |What you see is from the last tutorial. |
|- | |- | ||
|3:50 | |3:50 | ||
− | | | + | |ummmm..... Lets comment this out. |
|- | |- | ||
|3:56 | |3:56 | ||
− | | | + | |Lets ignore this. |
|- | |- | ||
|3:57 | |3:57 | ||
− | | | + | |This will completely ignore this part of tutorial. |
|- | |- | ||
|4:02 | |4:02 | ||
− | | | + | |Ok - so back to the code that I am currently showing and lets refresh. |
|- | |- | ||
|4:10 | |4:10 | ||
− | | | + | | I've refreshed it twice so accordingly 2 records have been put in. |
|- | |- | ||
|4:14 | |4:14 | ||
− | | | + | |But by going back to browse and scrolling down we can see, lets delete 1 of these, we can see the data I just specified has been put into the database. |
|- | |- | ||
|4:26 | |4:26 | ||
− | | | + | |In fact what I have done is I have put my date of birth as the current date which I didn't mean to do. |
|- | |- | ||
|4:33 | |4:33 | ||
− | | | + | |I don't want my date of birth as the current date because I was not born today. |
|- | |- | ||
|4:39 | |4:39 | ||
− | | | + | |My firstname is ok. My lastname is ok. My gender is fine. |
|- | |- | ||
|4:43 | |4:43 | ||
− | | | + | |We can see that my id is 6 at the moment and the next time we insert a record this would go up to 7 and then again to 8. |
|- | |- | ||
|4:53 | |4:53 | ||
− | | | + | |You should know that by now. |
|- | |- | ||
|4:54 | |4:54 | ||
− | | | + | |Next what I'll show you is how to change my date of birth because I have made a mistake. |
|- | |- | ||
|5:00 | |5:00 | ||
− | | | + | |So first I will comment these 2 lines so we don't have to rerun this. |
|- | |- | ||
|5:04 | |5:04 | ||
− | | | + | |And I'll create a new variable. We will just comment this as "update data". |
|- | |- | ||
|5:08 | |5:08 | ||
− | | | + | |Current variable called "update" and that's equal to "mysql query" function. |
|- | |- | ||
|5:14 | |5:14 | ||
− | | | + | |And inside the parameter that we are calling is "mysql query" code itself. |
|- | |- | ||
|5:20 | |5:20 | ||
− | | | + | |So here we will type "UPDATE" and we are going to say the table name which is "people". |
|- | |- | ||
|5:28 | |5:28 | ||
− | | | + | |Then we will say "SET" and we need to pick a particular field in which to set. |
|- | |- | ||
|5:34 | |5:34 | ||
− | | | + | |This happens to be the "d o b" and that's equal to my actual date of birth which is 1989, the year I was born in and the month is November and the day I was born is 16th. |
|- | |- | ||
|5:47 | |5:47 | ||
− | | | + | |By running this command what we are actually doing is we are updating everyone's date of birth in this table to this. |
|- | |- | ||
|5:54 | |5:54 | ||
− | | | + | |That's because we've not specified where we want to update this. |
|- | |- | ||
|5:57 | |5:57 | ||
− | | | + | |But we can do is after this we can say "WHERE id=6" because my unique id is 6. |
|- | |- | ||
|6:08 | |6:08 | ||
− | | | + | |Lets have a look here. |
|- | |- | ||
|6:10 | |6:10 | ||
− | | | + | |Otherwise it will update everyone else's. |
|- | |- | ||
|6:12 | |6:12 | ||
− | | | + | |Remember I said the id is unique. Its better to say update my id. |
|- | |- | ||
|6:16 | |6:16 | ||
− | | | + | |What I could do instead, is say, "WHERE firstname equals Alex". However this will update every record that has firstname "Alex". |
|- | |- | ||
|6:24 | |6:24 | ||
− | | | + | |But we can also say "AND lastname equals Garrett". |
|- | |- | ||
|6:26 | |6:26 | ||
− | | | + | |However if we still have two people in the database with the same firstname and lastname, we are still running the same risk as before. |
|- | |- | ||
|6:43 | |6:43 | ||
− | | | + | |So its best to use our "unique" and thats the key word "unique id" which for me is 6. |
|- | |- | ||
|6:48 | |6:48 | ||
− | | | + | |So at the moment, you can see that my date of birth is set to 2009 which is the current date. |
|- | |- | ||
|6:53 | |6:53 | ||
− | | | + | |But by refreshing this page, nothing's happened because we are just running a command. |
|- | |- | ||
|7:00 | |7:00 | ||
− | | | + | |Now if we click on browse to refresh and we scroll down, we can see that it has changed to what we specified and everything else has been left intact. |
|- | |- | ||
|7:08 | |7:08 | ||
− | | | + | |So if you need to update data in your database or anything like that, you can specify what data you want to update. |
|- | |- | ||
|7:15 | |7:15 | ||
− | | | + | |I used "dob" and that equals to the date of birth that was necessary. |
|- | |- | ||
|7:18 | |7:18 | ||
− | | | + | |I could have updated my lastname. |
|- | |- | ||
|7:20 | |7:20 | ||
− | | | + | |You also need to specify where you want this to be updated. |
|- | |- | ||
|7:24 | |7:24 | ||
− | | | + | |So I said this record which is this long line here. |
|- | |- | ||
|7:28 | |7:28 | ||
− | | | + | |These are called records and I specified "WHERE" the id was equal to 6 and that has updated my unique record. |
|- | |- | ||
|7:35 | |7:35 | ||
− | | | + | |So that's what you have learnt - how to insert values and also how to update some values if you get it wrong like I did or if you just want to update some data which happens most of the time when your doing your databases. |
|- | |- | ||
|7:54 | |7:54 | ||
− | | | + | |Ok - so join me in the next part to find out how to start reading from your database and display the data to the user. |
|- | |- | ||
|8:04 | |8:04 | ||
− | | | + | |See you soon. This is Juanita Jayakar dubbing for the Spoken Tutorial Project. |
Revision as of 11:44, 6 December 2012
Time | Narration |
---|---|
0:00 | Hi, welcome back. In this tutorial we will write some data into a database. |
0:06 | To do this we will use our "mysql query" function. |
0:10 | Now you can see here that we have our... records.... |
0:16 | I am redoing this tutorial because the first time I did it, it did not work. |
0:21 | So first of all, I will just delete this data here. |
0:29 | Well... ok... So we have a blank table, we have no data in our table whatsoever at the moment. |
0:36 | we can see that there is nothing in here. |
0:40 | These are just our field names here. |
0:43 | To begin with, in here, lets just comment this. |
0:47 | So "write some data". Then we will set up a query that will write data. |
0:52 | So... "write" and we will use "mysql query" function. |
0:59 | And this takes exactly 1 parameter which is our sql query. |
1:01 | To do this, we will type "INSERT" to insert data. |
1:06 | We are going to say "INSERT INTO". |
1:09 | Now the reason I've typed this in capitals is because its sql code. |
1:14 | If I type anything in uppercase it means it is sql code. |
1:19 | If I type anything in lowercase it means its either a table name, database name or it is data that I'm writing to the database. |
1:30 | So "INSERT INTO people" because that is our table name here. |
1:34 | "INSERT INTO people" and then "VALUES" and then in brackets we will create a little area for each value. |
1:40 | So we got 1,2,3,4,5. |
1:46 | There are 5 fields so we need exactly 5 pieces of database written here. |
1:50 | We need the id, firstname, lastname, all the way down to gender. |
1:55 | These are created in or created using single quotes each separated by comas. |
2:03 | The reason we don't use double quotes is because we've got these at the end or rather start and end over here. |
2:10 | We don't need to insert our id here. |
2:13 | Our next one is firstname - so I'll say "Alex". |
2:17 | My lastname I'll say is "Garrett". |
2:20 | For my date of birth I'll create a date function which equals to a variable "date" |
2:26 | I'll put this in the particular structure. |
2:28 | We can see from our database over here that when we go to insert a value, we can scroll down and see that our calender function has dates on it. |
2:39 | So on clicking 23rd, we can see the structure this field takes. |
2:45 | Its the year in long format. |
2:47 | Next is the month and then the day. |
2:48 | So 2009 02 23 which is 23rd of the 2nd, 2009. |
2:54 | So what we can do here is we can structure our date function in capital Y m and then d using hyphen in between to get the structure we need. |
3:05 | So this will be structured like that. |
3:09 | This will equal to this and that will be the current date. |
3:13 | Using the date and presuming that is in the structure of our date, we can insert it into our table here. |
3:24 | The last one is gender and since I'm a male, I'm putting in "M" for male. |
3:28 | Presuming that will work, we can run this. |
3:30 | But before that, we could say "or die" at the end followed by a mysql error. |
3:38 | I will skip that for now but feel free to add them if you like. |
3:44 | Ok so refreshing our page. |
3:48 | What you see is from the last tutorial. |
3:50 | ummmm..... Lets comment this out. |
3:56 | Lets ignore this. |
3:57 | This will completely ignore this part of tutorial. |
4:02 | Ok - so back to the code that I am currently showing and lets refresh. |
4:10 | I've refreshed it twice so accordingly 2 records have been put in. |
4:14 | But by going back to browse and scrolling down we can see, lets delete 1 of these, we can see the data I just specified has been put into the database. |
4:26 | In fact what I have done is I have put my date of birth as the current date which I didn't mean to do. |
4:33 | I don't want my date of birth as the current date because I was not born today. |
4:39 | My firstname is ok. My lastname is ok. My gender is fine. |
4:43 | We can see that my id is 6 at the moment and the next time we insert a record this would go up to 7 and then again to 8. |
4:53 | You should know that by now. |
4:54 | Next what I'll show you is how to change my date of birth because I have made a mistake. |
5:00 | So first I will comment these 2 lines so we don't have to rerun this. |
5:04 | And I'll create a new variable. We will just comment this as "update data". |
5:08 | Current variable called "update" and that's equal to "mysql query" function. |
5:14 | And inside the parameter that we are calling is "mysql query" code itself. |
5:20 | So here we will type "UPDATE" and we are going to say the table name which is "people". |
5:28 | Then we will say "SET" and we need to pick a particular field in which to set. |
5:34 | This happens to be the "d o b" and that's equal to my actual date of birth which is 1989, the year I was born in and the month is November and the day I was born is 16th. |
5:47 | By running this command what we are actually doing is we are updating everyone's date of birth in this table to this. |
5:54 | That's because we've not specified where we want to update this. |
5:57 | But we can do is after this we can say "WHERE id=6" because my unique id is 6. |
6:08 | Lets have a look here. |
6:10 | Otherwise it will update everyone else's. |
6:12 | Remember I said the id is unique. Its better to say update my id. |
6:16 | What I could do instead, is say, "WHERE firstname equals Alex". However this will update every record that has firstname "Alex". |
6:24 | But we can also say "AND lastname equals Garrett". |
6:26 | However if we still have two people in the database with the same firstname and lastname, we are still running the same risk as before. |
6:43 | So its best to use our "unique" and thats the key word "unique id" which for me is 6. |
6:48 | So at the moment, you can see that my date of birth is set to 2009 which is the current date. |
6:53 | But by refreshing this page, nothing's happened because we are just running a command. |
7:00 | Now if we click on browse to refresh and we scroll down, we can see that it has changed to what we specified and everything else has been left intact. |
7:08 | So if you need to update data in your database or anything like that, you can specify what data you want to update. |
7:15 | I used "dob" and that equals to the date of birth that was necessary. |
7:18 | I could have updated my lastname. |
7:20 | You also need to specify where you want this to be updated. |
7:24 | So I said this record which is this long line here. |
7:28 | These are called records and I specified "WHERE" the id was equal to 6 and that has updated my unique record. |
7:35 | So that's what you have learnt - how to insert values and also how to update some values if you get it wrong like I did or if you just want to update some data which happens most of the time when your doing your databases. |
7:54 | Ok - so join me in the next part to find out how to start reading from your database and display the data to the user. |
8:04 | See you soon. This is Juanita Jayakar dubbing for the Spoken Tutorial Project. |