Updating and dealing records from PHP is going to be very similar to the insert process that we just saw in the last movie. However there are two important differences, let's take a look. To begin with let's open up databases insert.php, because what we're going to be doing is going to be very similar to this one. But don't mess with this, let's just do a Save As and instead of Insert let's make it Update.php. Now, the process is going to be very similar, like I said, we're going to do our database connection opening at the top, then we'll do our query and then we'll close it at the bottom. There's not going to be any data for us to work with.
So, let's paste in our update query, just a basic SQL update query. Update statement, set. And then I'm setting menu name equal to whatever the menu name is, position equal to position, visible equal to visible and where id equals id. Notice that I have single quotes around the string which is the menu name. And then there's commas in between all those values. Now this brings up the first difference which is that we're going to need an id. We didn't have that when we were creating, we just let MySQL set the id for us because it was set to auto increment it will always just pick the next id.
But we're going to need to actually provide an id, so id equals. And then what do we want our idea to be? Well, normally, you would be getting that probably from the page that preceded it. It would come with the request, telling you what id you wanted to make a change to. It would either be in a form value or it'd be in the get, telling us. Instead, we need to find out what id we want to use. So, let's do that by going to databases.php and let's just add, right here after subject name, menu_name, I'm just going to do a concatenate. And then a space, parentheses, actually type two parentheses, which is fine.
I'll type another double quote, concatenate that and then in between there, I'm going to put subject and ask for the key id. So, that'll basically put space, parenthesis, id, and then closing parenthesis. Let's try that out real quick. Go over here to Firefox, here I am on my databases.php page reload the page and now you can see the list of ids. So, for me that id is number five. Now again you wouldn't normally have this exposed to your end user. It would just be part of a link or part of a form. When you clicked the link, that's what it would send. It would send the id along with it and the user would never be aware of the id. But for our purposes, we're just going to expose it so that we can see it in the HTML.
So your id may be different than mine. Don't worry that if it is, we may have added or deleted different records to get our database to this point. MySQL is going to handle assigning the ids. Just make sure that you use the right one for you in the code that's coming up. So, for me, it's id number 5. I'm going to switch back. I'll close that page. And in databasesupdate.php, I'm going to be updating record number 5. And I'll change it to delete me. So, I'll save it. Now, we're going to have our query constructed, it's going to drop these values into this query.
The query will run here mysqli_query will return a result, which is true or false. If it's true, then it's success, and we'll do whatever the success action was. If it's failure, then subject update failed, it's probably what we would show the user, something like that. But instead we're going to actually show the mysqli error that came in if there is one. So we'll save that. Let's go and try it. Go to databases, underscore, update. Sucess. Hit the back button, let's reload that page and there it is. You can see our change took place, Delete me.
We can also try another change here, let's make visible equal to zero, save the page, go back I'll just the forward button and reload that page. Now if we hit the back button and reload this page, delete me is gone, because it's not visible. And this databases.php page was actually asking for visible subjects. You can start to see how that visibility is going to work on our final web site. Let's go ahead and set it back to visible, just so that we can see it again. And, we'll update one last time, there we go. You can go back if you want to confirm that it actually did do it.
So that's the first thing we need to know about doing Update statement. The first change is that id has to be set. Now let me show you the second change. We did it successfully, but there's still one more thing we need to watch out for. Try doing id 2000. We don't have 2000 records, but let's save it, and let's go back and reload our update page. Success. Every time we hit reload it comes back and says success. But that's not possible. It can't be succeeding. Well, that answer is that it is succeeding in sending this query to MySQL. MySQL came back and said, okay, I did that. It didn't actually do anything in the process, but MySQL says, well, I successfully did what you asked me to do. Not my fault that it didn't actually do anything.
So, what we need to do is do an additional check to find out if something else happened. Because it is possible with a update or delete to write a successful query that doesn't change anything. The way we're going to do that is, after we check and see, did the result succeed, we're going to say. And mysquli_affected_rows, and then the connection is equal to 1. This will return the number of rows that were affected by our last change on the current connection. So, it looks for the connection and it says, alright, in the last query, how many rows were changed? You may remember when we were in the mysql command line, it actually tells you that.
It actually says to you, when you type a command, it says one row affected, two rows affected. So, this number can be any number that we want. In this case, we're expecting it to be one if it succeeded. Where id equals one, means that there should be only one of them. If we were changing multiple records, well then we might be expecting it to be more. We might want to say greater than zero, for example. And if we did that, if we took away this statement, the where statement, it would change all of the subjects in the database. Or we could write a where statement that's different let's imagine that we had a bunch of customers, we wanted to find all users whose name was Susie, and change it to Susan. We could do that.
And it would change 37 records, perhaps, at the same time. MySQL affected rows would return 37. If it fails and there's a complete error, then MySQL affected rows returns negative one. So, some people don't even put the result in front, they just check MySQL affected rows. I think it's better to go ahead and check result. We've already got it, right? It's right here. Might as well go ahead and check and see if that's true or false before we go and do any other API call. Now one thing that you need to watch out for is that if you pass values that are exactly the same as what's in the database already, then MySQL will return 0 rows affected.
Because it didn't make a change. It doesn't bother to write the same values a second time. It says oh, these are the same. Never mind, and mysqli affected rows will be 0. So, just be mindful of that. So, if no rows were affected, then it means that either the record didn't update or no rows matched my Where clause. One of those two things is possible. So, now that we have that, let's check it out. Let try it. Let's go back over here and let's reload the page. This is asking for id equals 2000. Database query failed. You see it no longer works. If we went back though, and we changed it to be five, and then we said well, we want visible to be zero. Now it says success.
If we do one, it comes back and tells us success again. All right, let's make sure that we have this visible at the end because what we're going to do next is try to delete it. And I want us to see how to do the delete process, and I want us to see whether we succeeded in deleting. So, deleting works almost exactly the same way as updating does. Delete, we'll do Save As on that same file. And the only thing is, we won't need any of these values, we'll just have our id. And our delete statement is going to be, delete from subjects, and we can remove those, where id equals whatever our id is, id five, in this case.
One more thing I think we should add here though, which is let's add to our query, limit one. This is a really good practice. Limit is something we can use to limit the results that we affect. We can use it with select, we can use it with update, but it's most useful when we're working with delete, because we can make sure that we only delete one. Even though we should only be getting one with id, it's just sanity check, just to make sure that we don't delete too much information, kind of a safety net. So, the same thing will be true here. Mysqli query, we'll make the query, return the result, it'll be true or false. We also want to check affected rows. We're expecting it to delete one row, so that's what we should get back is one. And then all of this will be the same except subject delete failed would be a better message in this case.
So, let's try it. But first let's go backwards. And let's just reload this page so we see delete me is there. And then let's add delete. Oops, it say's I got a database query failed error. And it say's there's an error in My_SQ L syntax. Now this is a good place where we just put in an echo. And we could check that out. Query, let's reload the page. Here's what my query looks like, you can spot the problem. You can see that the id and limit run right together, that's no good. So, we actually need a space here when we're constructing these.
We need to make sure that the id space limit 1. Here I can take out that debug statement and now we do delete. Reload it and it comes back and says success. Let's go backwards to databases.php and reload it and you can see that it did successfully delete it. So that's it, you can see that the process for all database interactivity follows this basic pattern, there are just a couple of differences between the different ones. But you now know how to create, read, update and delete records in MySQL using PHP.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101787 Viewers
61 Video lessons · 88547 Viewers
71 Video lessons · 72357 Viewers
56 Video lessons · 104066 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Your file was successfully uploaded.