Hi, welcome back. In this tutorial we will write some data into a database.
To do this we will use our "mysql query" function.
Now you can see here that we have our... records....
I am redoing this tutorial because the first time I did it, it did not work.
So first of all, I will just delete this data here.
Well... ok... So we have a blank table, we have no data in our table whatsoever at the moment.
we can see that there is nothing in here.
There are just our field names here.
To begin with, in here, lets just comment this.
So "write some data". Then we will set up a query that will write data.
So... "write" and we will use "mysql query" function.
And this takes exactly 1 parameter which is our sql query.
To do this, we will type "INSERT" to insert data.
We are going to say "INSERT INTO".
Now the reason I've typed this in capitals is because its sql code.
If I type anything in uppercase it means it is sql code.
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.
So "INSERT INTO people" because that is our table name here.
"INSERT INTO people" and then "VALUES" and then in brackets we will create a little area for each value.
So we got 1,2,3,4,5.
There are 5 fields so we need exactly 5 pieces of database written here.
We need the id, firstname, lastname, all the way down to gender.
These are created in or created using single quotes each separated by comas.
The reason we don't use double quotes is because we've got these at the end or rather start and end over here.
We don't need to insert our id here.
Our next one is firstname - so I'll say "Alex".
My lastname I'll say is "Garrett".
For my date of birth I'll create a date function which equals to a variable "date"
I'll put this in the particular structure.
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.
So on clicking 23rd, we can see the structure this field takes.
Its the year in long format.
Next is the month and then the day.
So 2009 02 23 which is 23rd of the 2nd, 2009.
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.
So this will be structured like that.
This will equal to this and that will be the current date.
Using the date and presuming that is in the structure of our date, we can insert it into our table here.
The last one is gender and since I'm a male, I'm putting in "M" for male.
Presuming that will work, we can run this.
But before that, we could say "or die" at the end followed by a mysql error.
I will skip that for now but feel free to add them if you like.
Ok so refreshing our page.
What you see is from the last tutorial.
ummmm..... Lets comment this out.
Lets ignore this.
This will completely ignore this part of tutorial.
Ok - so back to the code that I am currently showing and lets refresh.
I've refreshed it twice so accordingly 2 records have been put in.
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.
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.
I don't want my date of birth as the current date because I was not born today.
My firstname is ok. My lastname is ok. My gender is fine.
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.
You should know that by now.
Next what I'll show you is how to change my date of birth because I have made a mistake.
So first I will comment these 2 lines so we don't have to re-run this.
And I'll create a new variable. We will just comment this as "update data".
Current variable called "update" and that's equal to "mysql query" function.
And inside the parameter that we are calling is "mysql query" code itself.
And here you will type "UPDATE" and we are going to say the table name which is "people".
Then we will say "SET" and we need to pick a particular field in which to set.
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.
By running this command what we are actually doing is we are updating everyone's date of birth in this table to this.
That's because we've not specified where we want to update this.
But we can do is after this we can say "WHERE id=6" because my unique id is 6.
Lets have a look here.
Otherwise it will update everyone else's.
Remember I said the id is unique. Its better to say update my id.
What I could do instead, is say, "WHERE firstname equals Alex". However this will update every record that has firstname "Alex".
But we can also say "AND lastname equals Garrett".
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.
So its best to use our "unique" and thats the key word "unique id" which for me is 6.
So at the moment, you can see that my date of birth is set to 2009 which is the current date.
But by refreshing this page, nothing's happened because we are just running a command.
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.
So if you need to update data in your database or anything like that, you can specify what data you want to update.
I used "dob" and that equals to the date of birth that was necessary.
I could have updated my lastname.
You also need to specify where you want this to be updated.
So I said this record which is this long line here.
These are called records and I specified "WHERE" the id was equal to 6 and that has updated my unique record.
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.
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.
See you soon. This is Juanita Jayakar dubbing for the Spoken Tutorial Project.