Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The last step in creating a data management system is to give the user a way to delete data from the MySQL table. I'll start this exercise by creating a completely new file. I'll select File > New and then create a new blank PHP page. I'll go into Code View, and I'm going to delete all of the HTML in the page. The user will never see this page rendered in the browser. It will execute a backend database operation to delete a record and then immediately return to the list page.
Then I'll save the file into the same folder that contains my other data management pages, and I'll name it explorerdelete.php. I'm going to add a server behavior. I'll go to the Server Behaviors panel, click plus, and choose Delete Record. In the Delete Record dialog, I'll check my Connection and then set the Table to Explorers. I've accepted the settings for the Primary key column and value, and then indicate that after deleting, I want to go to explorerlist.php.
When I click OK, all of the required PHP code is added to the page. Down at the bottom, PHP first checks to make sure that I have a variable named explorerId, a Get variable that will be passed from another page. It also makes sure that this variable isn't blank, and then if that's all true, it executes a deleteSQL statement, removing the requested record from the database table. Down at the bottom, it sets a variable, indicating which page we want to navigate to, and then using this command, header, sprintf, Location, deleteGoTo, it redirects the browser to that page.
In the page's current state, watch what happens if I browse it directly. I'll run the page in the browser, and I see that the page is blank. The problem is with the logic. If this condition in the 'if' statement turns out to be false, PHP simply is told, jump past all this code and don't do anything. To fix this, I'm going to take everything starting with the setting of the deleteGoTo variable, all the way through the header command. I'll cut it to the clipboard, and then paste it after the closing brace for the 'if' statement.
I'll then select most of the code and press Shift+Tab and then do a little bit more tabulation to make the code look right. If the page is called directly from the browser for some reason, instead of just showing a blank page, it will navigate to the list page. So now my page is ready to delete records. I am going to call this page from the list page. I'll go to explorerlist.php and look at it in Design View. In Design View, I'll click and drag down to select the entire final column in the table.
Then I'll right-click and choose Table > Insert Rows or Columns. Don't choose Insert Column here, because it would insert a column before the current selected column. If I choose Insert Rows or Columns, I have the option to insert a new column either before or after the current column. I will choose Columns, and one column after the current column, and click OK. Then I'll click into the final column into the second row, the row that contains the data, and I'll type the word "Delete." I'll select the word "Delete," and then go to the Link box, and I'll browse and select my new file, explorerdelete.php.
Now I'll go to Code View. Just as with the Update command, I need to pass a variable, which I'll name explorerId, to the receiving page. The easiest way to do this is to take the existing code that I already put into the Update command. So I'll go to the explorerupdate2.php href. I'll start with the question mark, and I'll take everything through the output of the explorerId column. I'll copy that to the clipboard. Then I'll go down to my new explorerdelete.php href, I'll right-click there, and paste.
I'm ready to test my delete operation. I'll save and run the list page, and when the list page is displayed, it shows a delete item for each record in the database table. I'll click Delete for my Revised Explorer record. You will see that it doesn't work quite right yet. So, I am going to go back to my list page, and I'm going to change the case of the variable that I'm passing from one page to another, so that it has an uppercase I instead of a lowercase i. I'll run the page in the browser again, I'll click Delete, and now the record is correctly removed.
It's important to match the case in the variable name and in the code that's looking for the variable in the Delete page. So now you have the ability to insert new records by clicking the Insert New Explorer link. You have the ability to update records by clicking on the First Name of any record, and you also have the ability to delete data from the MySQL database by clicking on the Delete links. Dreamweaver has generated most of the PHP code we needed, but I have made minor alterations, and along the way you've learned a good bit about how to write PHP code to manage data in a backend MySQL database.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98069 Viewers
61 Video lessons · 85308 Viewers
71 Video lessons · 69354 Viewers
56 Video lessons · 101717 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.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.