Html template.
First create a folder named phpCrud then inside the folder create 3 files config.php, Database.php and update.php. Inside the update.php file paste the html code then open config.php file and paste the code then last open the Database.php file and paste the code i have given.
If you can not create the mysql database you can see video from youtube how mysql database create then you give the database name in config file.
After click the edit button from read page update page will open.
<!doctype html>
<html>
<head>
<title>PHP OOP CRUD</title>
<style>
body{font-family:verdana}
.phpcoding{width:900px; margin: 0 auto;
background:#ddd}
.headeroption, .footeroption{background:#444;
color:#fff;text-align:center;padding:20px;}
.headeroption h2, .footeroption h2{margin:0;font-size:24px}
.maincontent{min-height:400px;padding:20px;font-size:18px}
p{margin:0}
input[type="text"]{width:238px;padding:5px;}
select{font-size:18px;padding:2px 5px;width:250px;}
.tblone{width:100%;border:1px solid #fff;margin:20px 0}
.tblone td{padding:5px 10px;text-align:center;}
table.tblone tr:nth-child(2n+1){background:#fff;height:30px;}
table.tblone tr:nth-child(2n){background:#f1f1f1;height:30px;}
#myform{width:400px;border:1px solid #fff;padding:10px;}
</style>
</head>
<body>
<div class="phpcoding">
<section class="headeroption">
<h2>CRUD Using OOP PHP and MYSQLi</h2>
</section>
<section class="maincontent">
<form action="" method="post">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $getData['name'] ?>"/></td>
<td><input type="hidden" name="id" value="<?php echo $getData['id'] ?>"/></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="<?php echo $getData['email'] ?>"/></td>
</tr>
<tr>
<td>Skill</td>
<td><input type="text" name="skill" value="<?php echo $getData['skill'] ?>"/></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="update" value="Update" />
<input type="submit" name="delete" value="Delete" />
</td>
</tr>
</table>
</form>
</section>
<section class="footeroption">
<p>© 2020 I Am a PHP Learner</p>
<h2>www.programsolve.com</h2>
</section>
</div>
</body>
</html>
Config file.
Second we create php file and the name of the file will be config.php, in config file we write this line of codeā¦
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "db_crud");
Database file creation.
Here we follow full php oop method so, we use data insert file and the file name will be Database.php. This file will include the config.php file.
<?php
class Database{
public $host = DB_HOST;
public $user = DB_USER;
public $pass = DB_PASS;
public $dbname = DB_NAME;
public $link;
public $error;
public function __construct(){
$this->connectdb();
}
private function connectdb(){
$this->link = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
if(!$this->link){
$this->error = "connection fail".$this->link->connect_error;
return false;
}
}
// Update Data
public function update($data) {
$id = mysqli_real_escape_string($this->link, $data['id']);
$name = mysqli_real_escape_string($this->link, $data['name']);
$email = mysqli_real_escape_string($this->link, $data['email']);
$skill = mysqli_real_escape_string($this->link, $data['skill']);
if ($name == '' || $email == '' || $skill == '') {
return $error = "Field must not be Empty !!";
} else {
$query = "UPDATE tbl_user SET name = '$name', email = '$email', skill = '$skill' WHERE id = '$id' ";
$insert_row = mysqli_query($this->link, $query) or die($this->link->error . __LINE__);
if ($insert_row) {
header("Location: index.php?msg=" . urlencode('Data Updated Successfully.'));
exit();
} else {
die("Error : (" . $this->link->errno . ")" . $this->link->error);
}
}
}
Finally i use this line of code inside the html file which name i gave read.php. This line of code need to pest inside body tag.
<?php
include "config.php";
include "Database.php";
?>
<?php
$id = $_GET['id'];
$db = new Database();
$query = "SELECT * FROM tbl_user WHERE id = $id";
$getData = $getData = mysqli_query($db->link, $query)->fetch_assoc();
if (isset($_POST['update'])) {
$error = $db->update($_POST);
}
?>
<?php
if(isset($error)){
echo "<span style='color:red'>".$error."</span>";
}
?>
At last i give some picture of file and after the code.
Files folder picture.
Be First to Comment