Hello, in this video, we are going to take a look at updating data in our table. So we've looked at selecting inserting data or by existing data. So if I go to table, notice data well a bunch of duplicate as well. So let's say I want to update, you know, this one here, we can do it based on an ID. So that's great. So we say this particular ID, which is unique as a primary key, I want to update the username from Yoda to say Yoda 123, for example.
To do that, it's really simple. You just create another query. And this is going to equal to connection. Again, you will prepare in the query to prevent SQL injection, that's the VG PDO. And here you put your query and the query for my update. And then you are stating the table name which happened to be user user, and then you say set so basically what you want to change I want to change this column.
So username, username, and then what you want to set it to, there could be, you know, a, you know, like a variable inside of here if you want to, I'm not using a variable, it's gonna just directly say Yoda 123. But if I just do that will literally update all of them. So we weren't aware. So where particular condition is met, or where ID equals three, I believe it was yes. And the reason we don't need to put quotations around the number is because this column is actually a integer type. If we had an integer or a number in the username, because it's, you know, var char, it would be, you know, detecting it as a string.
So it's got to, you know, make sure you handle that accordingly when you are, you know, manipulating the database. So I haven't reloaded yet. So there you can see, it's the latest version of the database. save that and this one work, because I forgot to actually execute To execute, nice, easy to execute, run this function, and we're ready to go. If I reload, no errors, and if I go, there we go Yoga 123. Now what I want to show you what happens if I say, username, so I want to change all of the Darth Vader users.
So I want to change all of them to let's say, tech, let's say and again, so I want to change username to earn again, and again, Scott Walker. And so you'll be based on where the username is now equal to this string of Darth Vader. So if I run this, all of these now should update. I could reload. There we go. And he can Skywalker.
So you can do it not just on the primary key or some sort of uniquely you can do it on multiple roles. If There's something coming in that you could maybe change the you know, use a type. There's a bunch of stuff you could do. But that's how you update data using the update query in your database. simple stuff. If you have any questions though, feel free to shoot me a message and as usual, I look forward to seeing you in the next video.