While these overriding (taking precedence over all other considerations) principles may not be obvious now, as you progress through this tutorial they will become more apparent. While you are going through this tutorial, you can refer back to them and when you have completed the tutorial review them again.
- A recordset is as the names implies, “A set of records” returned from a database. However, what is more important to remember is that it is in the computer's memory and is invisible to a user until it is displayed on the page. However, before, we can create a recordset, we have need of selecting a database.
- Many pages will have its action attribute in the <form> tag set to exactly the same name as the page. This is called self-posting or post back where the page post on TOP OF ITSELF when the submit button is clicked.
- A recordset should be RETRIEVED FIRST since it:
- stores a set of records in memory when a page is loaded (but does not display them on the page)
- allows you to creates a list of dynamic variables so that the result of a recordset can be DISPLAYED on a page.
- When creating a recordset for a page, it is helpful to think of how many records do you really need to return to the page. For example, for the Add, Update, Delete and Detail pages, you only need one record so you will want to use the WHERE clause to limit the number of records to one typically using the primary key. If you are creating a Master page or a Search page, then you would want to use a WHERE clause to return typically one or more records. The term typically is used because it’s possible that no record could be returned from the database but this is “typically” NOT the case. With the exception of the Update and Delete pages that requires a recordset of one specific record to be updated or deleted from the database, you typically create a recordset for ALL records in the database.
- Primary key field from the database typically should NOT be used in a table or displayed on a page. It is however, used to pass the primary key (e.g., recordID) to another page.
- The $_GET[ ... ] represents a PHP URL variable. Conversely, $_POST [ ... ] represents a PHP FORM variable.
- Typically, once you perform an operation (e.g., add/update/delete), the user will be redirected back to the main page. (e.g., index.php) or a confirmation page.
- Form elements (or form variables) are not set or defined UNTIL AFTER the form has been submitted by clicking on the SUBMIT button. Hence, if you are using dynamic form variables in an SQL Query or a table you will need to wrap the code with a conditional statement and use a built-in function called isset( ) to check to see it a particular form variable is set. Typically, a hidden field is used to check to see if it exists once the form has been submitted. Otherwise, you will get a variety of undefined errors.
- "ID" should used instead of "_ID" (e.g., employee_ID to employeeID) for consistency and make code less error prone and easier to read.
- It is best practice to add a prefix to common elements such as recordset (rs_myRecordSet), table (tbl_myTable), and connection (conn_myConnection)
- It is best practice to use FirstName and LastName instead of FullName for a person name so that it can be sorted easier.
- TIP: It is important to note the color of certain elements to determine if there is a syntax error(s) in your code. In PHP, the default color for string literals are in red, variables (e.g., $_POST) are in light blue and period and square brackets are in dark blue.
- There are sections of this tutorial entitled "For Geeks Only or Those Inspiring to be Geeks" to show more of a code-centric view of what going on "behind the scene."
- You cannot check a PHP page by double-clicking on it the File Explorer. The page has to be PROCESSED through a browser in order to be displayed. You can typically get to this file by typing http://localhost/AppDirectoryName which points to a file reference somewhere on your computer (e.g., C:/xampp/htdocs/AppDirectoryName). Where APPDirectoryName is the actual name of the directory (e.g., EmployeeDiretory).
- The $_GET or $_POST are special predefined arrays that can contain multiple values (e.g., $POST [ 'FirstName' ]).
- It is best to use single quotes for HTML attributes because you may need to later using a print statement which will require you to use double quotes around the complete statement. NOTE: Both double quotes ( " ) and single quotes ( ' ) are used in code. Content in double quotes are string literals used by PHP and are passed without evaluation. Content in single quotes are used by SQL (not PHP) and represents the values being inserted into a database.
- It is best practice to place the database connection and selection in an include file if it will be the same for all pages.
- When created NESTED "if/else" conditional statements, it is best to test one "if/else" statement at a time before the next one is nested. That way you can see the result of each conditional statement separately.
- As a final check, remove any code that was used for debugging purposes only before you upload the code to a public site.
- There are two ways in INJECT additional PHP code into an EXISTING PHP code block:
- Use the curly braces ( {.........} ) for simple statements (e.g., variables).
- Split the PHP code block into three separate PHP code blocks if the code is more involved (e.g., if/else statement):
From:
<?php>
// original code
?>
To:
?php>
// original code (first part)
?
?php>
// new code
?
?php>
// original code (second part)
?