PHP-and-MySQL/C3/MySQL-Part-3/English-timed
From Script | Spoken-Tutorial
Time | Narration |
---|---|
0:00 | Hi, welcome back. In this tutorial we will write some data into a database. |
0:07 | To do this we will use our "mysql query" function. |
0:12 | 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:22 | 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:37 | we can see that there is nothing in here. |
0:40 | There 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:57 | And this takes exactly 1 parameter which is our sql query. |
1:02 | 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:28 | So "INSERT INTO people" because that is our table name here. |
1:33 | "INSERT INTO people" and then "VALUES" and then in brackets we will create a little area for each value. |
1:42 | 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:53 | We need the id, firstname, lastname, all the way down to gender. |
1:58 | These are created in or created using single quotes each separated by comas. |
2:07 | 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:15 | We don't need to insert our id here. |
2:18 | Our next one is firstname - so I'll say "Alex". |
2:22 | My lastname I'll say is "Garrett". |
2:25 | For my date of birth I'll create a date function which equals to a variable "date" |
2:31 | I'll put this in the particular structure. |
2:35 | 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:44 | So on clicking 23rd, we can see the structure this field takes. |
2:50 | Its the year in long format. |
2:52 | Next is the month and then the day. |
2:55 | So 2009 02 23 which is 23rd of the 2nd, 2009. |
03:02 | 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. |
03:13 | So this will be structured like that. |
03:16 | This will equal to this and that will be the current date. |
03:20 | Using the date and presuming that is in the structure of our date, we can insert it into our table here. |
03:28 | The last one is gender and since I'm a male, I'm putting in "M" for male. |
03:34 | Presuming that will work, we can run this. |
03:37 | But before that, we could say "or die" at the end followed by a mysql error. |
03:44 | I will skip that for now but feel free to add them if you like. |
03:50 | Ok so refreshing our page. |
03:53 | What you see is from the last tutorial. |
03:57 | ummmm..... Lets comment this out. |
03:59 | Lets ignore this. |
04:01 | This will completely ignore this part of tutorial. |
04:08 | Ok - so back to the code that I am currently showing and lets refresh. |
04:14 | I've refreshed it twice so accordingly 2 records have been put in. |
04:24 | 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. |
04:35 | 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. |
04:43 | I don't want my date of birth as the current date because I was not born today. |
04:48 | My firstname is ok. My lastname is ok. My gender is fine. |
04:53 | 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. |
05:02 | You should know that by now. |
05:03 | Next what I'll show you is how to change my date of birth because I have made a mistake. |
05:09 | So first I will comment these 2 lines so we don't have to re-run this. |
05:15 | And I'll create a new variable. We will just comment this as "update data". |
05:20 | Current variable called "update" and that's equal to "mysql query" function. |
05:26 | And inside the parameter that we are calling is "mysql query" code itself. |
05:32 | And here you will type "UPDATE" and we are going to say the table name which is "people". |
05:38 | Then we will say "SET" and we need to pick a particular field in which to set. |
05:43 | 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. |
05:57 | By running this command what we are actually doing is we are updating everyone's date of birth in this table to this. |
06:05 | That's because we've not specified where we want to update this. |
06:10 | But we can do is after this we can say "WHERE id=6" because my unique id is 6. |
06:18 | Lets have a look here. |
06:23 | Otherwise it will update everyone else's. |
06:26 | Remember I said the id is unique. Its better to say update my id. |
06:32 | What I could do instead, is say, "WHERE firstname equals Alex". However this will update every record that has firstname "Alex". |
06:41 | But we can also say "AND lastname equals Garrett". |
06:46 | However if we will have two people in the database with the same firstname and lastname, we are still running the same risk as before. |
06:54 | So its best to use our "unique" and thats the key word "unique id" which for me is 6. |
07:00 | So at the moment, you can see that my date of birth is set to 2009 which is the current date. |
07:06 | But by refreshing this page, nothing's happened because we are just running a command. |
07:11 | 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. |
07:21 | So if you need to update data in your database or anything like that, you can specify what data you want to update. |
07:29 | I used "dob" and that equals to the date of birth that was necessary. |
07:34 | I could have updated my lastname. |
07:36 | You also need to specify where you want this to be updated. |
07:40 | So I said this record which is this long line here. |
07:46 | These are called records and I specified "WHERE" the id was equal to 6 and that has updated my unique record. |
07:56 | 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. |
08:10 | 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. |
08:17 | See you soon. This is Juanita Jayakar dubbing for the Spoken Tutorial Project. |