Wednesday, 17 October 2018

Save Data Functions: MYSQLI CRUD OOP Series

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
function testing() to process the data to database.
Code:

public function save_userlevel($name,$desc){
if($result = $this->dbconn->query("SELECT ulevel_name FROM tbl_userlevel WHERE ulevel_name ='$name'")){
$row_cnt = $result->num_rows;
if($row_cnt==0){
$sql = mysqli_query($this->dbconn, "INSERT INTO tbl_userlevel (ulevel_name, ulevel_desc) VALUES ('$name','$desc')") or die (mysqli_connect_error());
return $sql;
}else{
return FALSE;
}
}
}

To notify user for the status of database operation, add the code after the <table> tag.
<tr>
<td colspan="2">
<?php if (!empty($errors)): ?>
<strong>Oops!</strong><?php echo implode('', $errors); ?>
<?php endif; ?>
</td>
</tr>
<tr>
<td colspan="2"><?php if (!empty($success)): ?>
<strong>Good!</strong><?php echo implode('', $success); ?>
<?php endif; ?>
</td>
</tr>

0 comments:

Post a Comment

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