PHP for Beginners – Part 5

0
Want create site? Find Free WordPress Themes and plugins.

Hey Guys!  This is the last part of the tutorial series. In this tutorial you will learn to create a database, connect your database with PHP, and query the database (MySQL) using SQL to Create, retrieve, update, delete (CRUD) records. If you want to learn SQL you can learn from w3schools. After the end of this tutorial series you will surely not be a beginner in PHP language.

 Let’s get started with creating a database first. open phpMyAdmin, and create a database  say “MyDatabase“.  Then create a table named “players” and insert some data so you can play with them. If you have trouble creating database through PhpMyAdmin refer to this video Installing xampp. Given below is the SQL script  for creating a table and inserting some records.

How to include one file within another file ?

There are 4 types of function that we can choose for including a file in another file:

  • include – It will always include the file, irrespective if file was included earlier,  gives a warning if could not find the file and continues the script
  • include_once – – It will include the file only if file was not included earlier, gives a warning if could not find the file and continues the script
  • require – It will always include the file, irrespective if file was included earlier,  stops the script if file not found.
  • require_once – It will include the file only if file was not included earlier,  stops the script if file not found.

If you have some critical file that is very necessary for the script, like database connection file, use require function. If you have some template file for html designing purpose you can use include function. If you want to include a file which has common function use include_once or including a function file multiple time will gives “function redeclare” error.

I have made a mini project that will insert, edit, delete and display data from the database. Have a look at the screenshots and download the code as well.

Database connection using PDO and error reporting

Create a file config.php that will be included in each page. It will have error reporting method, database connection and error message functions.

Create a php file functions.php file which has error messaging functions.

Creating landing page with listing and search filter

Create a php file index.php file which will be the home page of the project. It is the landing page of the project. Let start creating it step by step. Start with basic template structure and including the database connection file.

Fetching players records from database and display them in a table listing with edit and delete action links.

Adding a search feature, so user can search players by name. Create a form and add a text field. Set the form method to GET method.

When the user types a search keyword and submits the form, then the server must process the form accordingly, query the database using that keyword and display the result.

Adding a new record in database using form

Create a new page insert.php which will have a form containing text fields for name, age and club. User will add values in the form and submit it. The server will then process the form and saves the value in the database. Lets get started by creating the form. Set the form method as POST which is secure and can transmit more values than GET method.

When the user submits the form add this piece of code at the top of the page (insert.php) . Do some basic data validation and then try to insert the data in the database.

Updating an existing record

Create a new page edit.php which will have a similar form like we create in add page (insert.php). When user select a record ( from listing) to edit/update, it will have its unique ID ( primary key ) in the query string of the page (edit.php?id=1). Using that ID you can fetch the records from the database.

Displaying the records in the form field. Store the ID in a hidden field so when user update the form it will pass that ID using POST method.

Finally when user submits the form after changing the value, the server must process the form values and update the database row.

Deleting a record from database

Create a new page delete.php which will use the same technique as edit.php. The record ID will be passed to delete.php using query string ( delete.php?id=4) and then using PHP code make a database call to delete that record.

Download the Full Source Code Here to Press the Download Button.
Did you find apk for android? You can find new Free Android Games and apps.
Share.

About Author

Entrepreneur & Dreamer I am a passionate Software Professional, love to learn and share my knowledge with others. Software is the hardware of my life.

Leave A Reply

CommentLuv badge