Wednesday 17 October 2018

Update Data: MYSQLI CRUD OOP Series

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.

0 comments:

Post a Comment

If possible, leave a positive comment. No hate speech or elicit comments. Thank you.