PHP OOP CRUD Tutorial – Step By Step Guide! Part 8
7.0 UPDATING RECORD IN PHP THE OOP WAY
I know our PHP OOP CRUD tutorial is kinda long. Please take a break or drink some coffee first!
7.1 Create File: update_product.php
Create update_product.php file, open that file and put the following code.
9.0 DELETING RECORD IN PHP THE OOP WAYThis is the last coding part of our PHP OOP CRUD Tutorial. Enjoy every code! 9.1 Put this JavaScript code in layout_footer.phpPut the following JavaScript code before the closing "body" tag in layout_footer.php file. We used Bootbox.js to make a Bootstrap-style confirm dialog box.
9.2 Create delete_product.phpCreate a new file and name it "delete_product.php". This file accepts the ID posted by the JavaScript code in the previous section. A record will be deleted from the database based on posted ID. Open delete_product.php and put the following code.
9.3 Delete Code in Product ClassThe previous section will not work with the "delete()" method in the product object. Open "product.php" which is inside the "objects" folder and put the following code.
9.4 OutputClick any "Delete" button in the index page. A pop up confirmation will be shown. If the user clicks "OK" the record will be deleted and gone in the table. A record was deleted in the database. 10.0 SEARCH RECORDS IN PHP THE OOP WAYWe'll continue by adding the search feature. This will answer the question: How to search data from database in php? This is a very useful feature because you enable your users to easily search a certain data from our MySQL database. Please note that this is a bonus section. The code in this section is not included in our LEVEL 1 source code download. 10.1 Change index.phpWe have to change index.php because we are adding a “search” feature and we want our code to be short. Our index.php will now look like the following code.
10.2 Create read_template.phpWhy do we need this template? We need it because exactly the same code can be used by index.php and search.php for displaying a list of records. Using a template means lesser code. This template holds our search form as well.
10.3 Create core.php in "config" folderCreate a new folder and name it "config". Inside that folder, create a new file and name it "core.php". This file will hold our pagination variables. Using a core.php file is a good practice, it can be used to hold other configuration values that you might need in the future. Open core.php and put the following code.
10.4 Change paging.php codeThe new paging.php code will look like the following.
|
7.2 Create a "Read Products" Button
The following code will render a button. This button, when clicked, will let us go back to the records list. Replace the previous section's "contents will be here" comments with the following code.
|
7.3 Retrieve One Product Information Based on the Given ID.
The following code will retrieve data that will populate our HTML form. This is important because this will let the user know what exactly the record he is updating.
Open update_product.php file. Replace "// retrieve one product will be here" comment with the following code.
|
7.4 Add readOne() method in the Product Object Class.
The readOne() method used in the previous section will not work without the following code inside /objects/product.php file.
|
7.5 Put the Values in the Form.
Now we can put the latest values to each form elements. Replace "<!-- 'update product' form will be here -->" comment of update_product.php with the following code.
|
7.6 Loop Through the Categories Records to show as Drop-down
The following code will list the categories in a drop-down.
Notice that we put "if($product->category_id==$category_id){..." inside the while loop. This is to pre-select the option of the current record.
Replace the previouse section's comments "categories select drop-down will be here" with the following code.
|
7.7 Code When Form was Submitted
The following code will assign the "posted" values to the object properties. Once assigned, it will update the database with those values using the update() method.
Open update_product.php file. Replace <!-- post code will be here -->" comment with the following code.
|
7.8 Update Code in the Product Class
The following code will make the previous section's "$product->update()" method work. Open our "product.php" which is inside the "objects" folder and add the following code.
|
7.9 Output
Click any "Edit" button in the index page. The update record form should look like the following.
When you submit the form, a message will be shown.
A record was changed in the database.
8.0 READ ONE RECORD IN PHP THE OOP WAY
We previously made the code for "update record", this section for reading one record from a database will be easier to do.
8.1 Create read_one.php file
This is the page where the data of a single record will be displayed. Create a new file and name it "read_one.php", open that file and put the following code.
|
8.2 Read one record based on given record ID
The following code will read a single record from the database. Put the following code before the "set page headers" comments of the previous section.
|
8.3 Display record on HTML table
This time, we will display the record details on an HTML table. Put the following code under the closing "div" tag of "Read Products" button.
|
8.4 Output
Click any "Read" button in the index page, you should see something like the image below.