This is now the final script for checking the submitted data from web form elements as either in Edit Mode or Save Data Operation.
Step 8. Updating data from Database
Rewrite the codes in index.php from Step 4 into the following codes below.
<?php
if(empty($_POST)===FALSE){
$ulevel_name = trim($_POST['ulevel_name']);
$ulevel_desc = trim($_POST['ulevel_desc']);
if(empty($ulevel_name) || empty($ulevel_desc)){
$errors[]="Please fill in required data to process";
}else{
if (isset($_GET['edit'])){
$edit = $obj->edit_userlevel($_GET['edit'],$ulevel_name,$ulevel_desc);
if ($edit==TRUE){
$success[]="Successfully update data in database.";
}else{
$errors[]="Unable to update data in database.";
}
}else{
$save = $obj->insert_userlevel($ulevel_name,$ulevel_desc);
if($save==TRUE){
$success[]='Successfully added to database.';//print_r($success);
}else{
$errors[]='Unable to process database.'; }
}
}
}
?>
Add the following codes to class.functions.php to update the data in the database from Edit Web Form elements that was submitted by the user.
public function edit_userlevel($ul_id,$ul_name,$ul_desc){
$sql=mysqli_query($this->con,"UPDATE tbl_userlevel SET ulevel_name='$ul_name', ulevel_desc='$ul_desc' WHERE ulevel_id='$ul_id'") or die (mysqli_connect_error());
if ($sql){
return $sql;
$sql->close();
}else{
return FALSE;
}
}
Discussion: This will now process the data either from Edit Data or Save Data in the system and the function edit_userlevel will execute the update query statement in the database.
This Klase Notes of simple CRUD OOP system was documented for academic purposes of students taking up IT Elective (PHP) Class. This is a modified version based on the MYSQL to MYSQLI functions CRUD Implementation in PHP
.
Students: Please leave a comment if you consider this article helpful to you. Thank you.
To access the full notes, click here and leave a comment in full notes to access the full source code.
Data can be updated in the database by calling its id, and the matching data to it. In this step, the index.php file will be rewritten. We need to write algorithm for editing and edit web form for treating data for editing.
Step 7. Create an “Edit Controls
After saving data to database, we will write an algorithm that will display data from database. This time the data will be displayed as is, no pagination applied to it.
Step 6. Display data from database in index.php
Insert the following codes after
To treat the data in the previous article, we need to create a function in class.dbfunctions.php which will store data in the database.
Step 5. Create a function that will process the data to database
Insert the following codes in the class.dbfunctions.php after
This script and codes will treat the submitted data in web forms. It will check the value of the submitted data in web forms and will instantiate a new class to execute the functions written in specific class.functions.php.
Step 4. Treat the submitted data in index.php
To capture the submitted data using Web Forms, you need to insert the following PHP codes after the <body> tag in index.php.
<?php
if (empty($_POST)===FALSE){
print_r($_POST);
$ul_name = mysql_real_escape_string($_POST['ul_name']);
$ul_desc = mysql_real_escape_string($_POST['ul_desc']);
if (empty($ul_name) || (empty($ul_desc))){
$errors[] = "Please fill in required data to process.";
}else{
//save data commands
$save = $db->save_userlevel($ul_name,$ul_desc);
if ($save==TRUE){
$success[]="Data successfully saved to database";
}else{
$errors[] = 'Unable to process data.';
}
}
}
?>
We use mysql_real_escape_string functions to treat future mysql injection issues.
The code $save = $db->save_userlevel($ul_name,$ul_desc); will call the class save_userlevel with arguments user level name and user level description.
This will prompt an error since we have not created yet the function under class.dbfunctions.php
This page will serve as the dashboard of the CRUD system. It calls the functionality of the class.functions.php that will be instantiated in this page.
Step 3. Create an index page for the CRUD
Filename: index.php
Code:
<?php
include 'class.functions.php';
$db = new DBfunctions();
echo $db->testing();
?>
<!DOCTYPE html>
<html>
<head>
<title>SQLI OOP::Demo System</title>
</head>
<body>
<!--Save Data Form-->
<form action='<?php echo $_SERVER['PHP_SELF'] ?>' method='post'>
<table border="1" align="center" cellpadding="0">
<th colspan="2">Userlevel Maintenance</th>
<tr>
<td>Userlevel::</td>
<td><input type="text" name="ul_name" placeholder="User level here" required></td>
</tr>
<tr>
<td>Description::</td>
<td><input type="text" name="ul_desc" placeholder="Description here" required></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
Discussion:
The first three lines of PHP at the top established the connection of the different class from class DBconn and class DBfunctions.
$db = new DBfunctions(); creates a new instance of the class DBfunctions
include 'class.functions.php'; this will make the class.functions.php become part of the actual index.php codes
echo $db->testing(); This will print the data in the function testing( ) under class.functions.php
The rest of the HTML codes, establish the web form elements that will be used in the different database operation. Web form elements should be named properly.
<form action='<?php echo $_SERVER['PHP_SELF'] ?>' method='post'>
The code tell us the submitted data will be processed by itself using method post. The post method is recommended to use when submitting data using web forms.
This article is a continuation to Database Class article which extends the functionality of the DBClass and perform OOP concept of inheritance.
Step 2. Create Class for Database Functions
This will extend the class DBconn to implement OOP inheritance and allow the user to store, retrieve and update data from database.
The following steps will walk us through in creating a system with simple CRU (Create, Read and Update) implementation in PHP Class. This Class Notes was made possible in collaboration with Professor Jessie Richie Naval de los Santos handling IT Elective (PHP) Class. The source codes was documented to serve as Notes for students in writing up their system with OOP implementation.
Step 1. Create Database Class
This class will establish the database connection