This is a test comment
Hello there how are you doing? this is just a test comment. Don't mind this.
Google
<?php class DbConnection { private $host = 'localhost'; private $username = 'root'; private $password = ''; private $database = 'crud'; protected $connection; public function __construct(){ $this->connection = new mysqli($this->host, $this->username, $this->password, $this->database); if (!$this->connection) { echo 'Cannot connect to database server'; exit; } } return $this->connection; } } ?>
<?php include_once('DbConnection.php'); class Crud extends DbConnection { public function __construct(){ parent::__construct(); } public function read($sql){ $query = $this->connection->query($sql); if ($query == false) { return false; } while ($row = $query->fetch_array()) { $rows[] = $row; } return $rows; } public function execute($sql){ $query = $this->connection->query($sql); if ($query == false) { return false; } else { return true; } } public function escape_string($value){ return $this->connection->real_escape_string($value); } }
<?php //start session //crud with database connection include_once('Crud.php'); $crud = new Crud(); //fetch data $sql = "SELECT * FROM members"; $result = $crud->read($sql); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>PHP CRUD using OOP Approach</title> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <h1 class="page-header text-center">PHP CRUD using OOP Approach</h1> <div class="row"> <div class="col-sm-8 col-sm-offset-2"> <?php ?> <div class="alert alert-info text-center"> <?php echo $_SESSION['message']; ?> </div> <?php } ?> <a href="#add" data-toggle="modal" class="btn btn-primary">Add New</a><br><br> <table class="table table-bordered table-striped"> <thead> <tr> <th>ID</th> <th>Firstname</th> <th>Lastname</th> <th>Address</th> <th>Action</th> </tr> </thead> <tbody> <?php foreach ($result as $key => $row) { ?> <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['firstname']; ?></td> <td><?php echo $row['lastname']; ?></td> <td><?php echo $row['address']; ?></td> <td><a href="#edit<?php echo $row['id']; ?>" data-toggle="modal" class="btn btn-success">Edit</a> | <a href="#delete<?php echo $row['id']; ?>" data-toggle="modal" class="btn btn-danger">Delete</a> </td> <?php include('action_modal.php'); ?> </tr> <?php } ?> </tbody> </table> </div> </div> </div> <?php include('add_modal.php'); ?> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" rel="stylesheet"></script> </body> </html>
<!-- Add New --> <div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <center><h4 class="modal-title" id="myModalLabel">Add Member</h4></center> </div> <div class="modal-body"> <div class="container-fluid"> <form method="POST" action="add.php"> <div class="row"> <div class="col-lg-2"> <label class="control-label" style="position:relative; top:7px;">Firstname:</label> </div> <div class="col-lg-10"> <input type="text" class="form-control" name="firstname"> </div> </div> <div style="height:10px;"></div> <div class="row"> <div class="col-lg-2"> <label class="control-label" style="position:relative; top:7px;">Lastname:</label> </div> <div class="col-lg-10"> <input type="text" class="form-control" name="lastname"> </div> </div> <div style="height:10px;"></div> <div class="row"> <div class="col-lg-2"> <label class="control-label" style="position:relative; top:7px;">Address:</label> </div> <div class="col-lg-10"> <input type="text" class="form-control" name="address"> </div> </div> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> <button type="submit" name="add" class="btn btn-primary">Save</button> </form> </div> </div> </div> </div>
<!-- Delete --> <div class="modal fade" id="delete<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <center><h4 class="modal-title" id="myModalLabel">Delete Member</h4></center> </div> <div class="modal-body"> <div class="container-fluid text-center"> <h5>Are sure you want to delete</h5> <h2>Name: <b><?php echo $row['firstname'].' '.$row['lastname']; ?></b></h2> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> <a href="delete.php?id=<?php echo $row['id']; ?>" class="btn btn-danger">Yes</a> </div> </div> </div> </div> <!-- /.modal --> <!-- Edit --> <div class="modal fade" id="edit<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <center><h4 class="modal-title" id="myModalLabel">Edit Member</h4></center> </div> <div class="modal-body"> <div class="container-fluid"> <form method="POST" action="edit.php?id=<?php echo $row['id']; ?>"> <div class="row"> <div class="col-lg-2"> <label style="position:relative; top:7px;">Firstname:</label> </div> <div class="col-lg-10"> <input type="text" name="firstname" class="form-control" value="<?php echo $row['firstname']; ?>"> </div> </div> <div style="height:10px;"></div> <div class="row"> <div class="col-lg-2"> <label style="position:relative; top:7px;">Lastname:</label> </div> <div class="col-lg-10"> <input type="text" name="lastname" class="form-control" value="<?php echo $row['lastname']; ?>"> </div> </div> <div style="height:10px;"></div> <div class="row"> <div class="col-lg-2"> <label style="position:relative; top:7px;">Address:</label> </div> <div class="col-lg-10"> <input type="text" name="address" class="form-control" value="<?php echo $row['address']; ?>"> </div> </div> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> <button type="submit" name="edit" class="btn btn-warning">Save</button> </div> </form> </div> </div> </div> <!-- /.modal -->
<?php //start session //including the database connection file include_once('Crud.php'); $crud = new Crud(); $firstname = $crud->escape_string($_POST['firstname']); $lastname = $crud->escape_string($_POST['lastname']); $address = $crud->escape_string($_POST['address']); //insert data to database $sql = "INSERT INTO members (firstname, lastname, address) VALUES ('$firstname','$lastname','$address')"; if($crud->execute($sql)){ $_SESSION['message'] = 'Member added successfully'; } else{ $_SESSION['message'] = 'Cannot add member'; } } else{ $_SESSION['message'] = 'Fill up add form first'; } ?>
<?php //start session //including the database connection file include_once('Crud.php'); //getting the id $id = $_GET['id']; $crud = new Crud(); $firstname = $crud->escape_string($_POST['firstname']); $lastname = $crud->escape_string($_POST['lastname']); $address = $crud->escape_string($_POST['address']); //update data $sql = "UPDATE members SET firstname = '$firstname', lastname = '$lastname', address = '$address' WHERE id = '$id'"; if($crud->execute($sql)){ $_SESSION['message'] = 'Member updated successfully'; } else{ $_SESSION['message'] = 'Cannot update member'; } } else{ $_SESSION['message'] = 'Select user to edit first'; } ?>
<?php //start session //including the database connection file include_once('Crud.php'); //getting the id $id = $_GET['id']; $crud = new Crud(); //delete data $sql = "DELETE FROM members WHERE id = '$id'"; if($crud->execute($sql)){ $_SESSION['message'] = 'Member deleted successfully'; } else{ $_SESSION['message'] = 'Cannot delete member'; } } else{ $_SESSION['message'] = 'Select user to delete first'; } ?>