PHP - OOP CRUD Operation
Submitted by nurhodelta_17 on Friday, October 27, 2017 - 17:16.
Language
Getting Started
Please take note that CSS and jQuery used in this tutorial are hosted so you need internet connection for them to work.Creating our Database
First step is to create our database. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as crud_oop. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.- CREATE TABLE `member` (
- `memberid` INT(11) NOT NULL AUTO_INCREMENT,
- `firstname` VARCHAR(50) NOT NULL,
- `lastname` VARCHAR(50) NOT NULL,
- PRIMARY KEY(`memberid`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Creating our Connection
Next, we create our connection to our database. This will serve as the bridge between our forms and database. We name this as conn.php.- <?php
- $conn = new mysqli("localhost", "root", "", "crud_oop");
- if ($conn->connect_error) {
- }
- ?>
index.php
Our index that contains our sample table.- <?php
- include('conn.php');
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>PHP - OOP CRUD Operation</title>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container">
- <div style="height:50px;"></div>
- <div class="well" style="margin-left:auto; margin-right:auto; padding:auto; width:70%;">
- <span style="font-size:25px; color:blue"><strong>PHP - OOP CRUD Operation</strong></span>
- <span class="pull-right"><a href="#addnew" data-toggle="modal" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Add New</a></span>
- <div style="height:15px;"></div>
- <table class="table table-striped table-bordered table-hover">
- <thead>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>Action</th>
- </thead>
- <tbody>
- <?php
- $query=$conn->query("select * from `member`");
- while($row=$query->fetch_array()){
- ?>
- <tr>
- <td><?php echo $row['firstname']; ?></td>
- <td><?php echo $row['lastname']; ?></td>
- <td>
- <a href="#edit<?php echo $row['memberid']; ?>" data-toggle="modal" class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span> Edit</a> ||
- <a href="#del<?php echo $row['memberid']; ?>" data-toggle="modal" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete</a>
- <?php include('button.php'); ?>
- </td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- <?php
- ?>
- <div class="alert alert-success">
- <center><?php echo $_SESSION['msg']; ?></center>
- </div>
- <?php
- }
- ?>
- </div>
- <?php include('add_modal.php'); ?>
- </div>
- </body>
- </html>
add_modal.php
This is our modal for our add function.- <!-- Add New -->
- <div class="modal fade" id="addnew" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
- <div class="modal-dialog">
- <div class="modal-content">
- <div class="modal-header">
- </div>
- <div class="modal-body">
- <div class="container-fluid">
- <form method="POST" action="addnew.php">
- <div class="row">
- <div class="col-md-2">
- </div>
- <div class="col-md-10">
- <input type="text" class="form-control" name="firstname">
- </div>
- </div>
- <div class="row">
- <div class="col-md-2">
- </div>
- <div class="col-md-10">
- <input type="text" class="form-control" name="lastname">
- </div>
- </div>
- </div>
- </div>
- <div class="modal-footer">
- </form>
- </div>
- </div>
- </div>
- </div>
addnew.php
This is our add code.- <?php
- include('conn.php');
- $firstname=$_POST['firstname'];
- $lastname=$_POST['lastname'];
- $conn->query("insert into member (firstname, lastname) values ('$firstname', '$lastname')");
- $_SESSION['msg']="Member Added Succesfully";
- ?>
button.php
This contains our edit and delete modal.- <!-- Delete -->
- <div class="modal fade" id="del<?php echo $row['memberid']; ?>" 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">
- <?php
- ?>
- <div class="container-fluid">
- <h5><center>Firstname: <strong><?php echo $drow['firstname']; ?></strong></center></h5>
- </div>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
- <a href="delete.php?id=<?php echo $row['memberid']; ?>" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete</a>
- </div>
- </div>
- </div>
- </div>
- <!-- /.modal -->
- <!-- Edit -->
- <div class="modal fade" id="edit<?php echo $row['memberid']; ?>" 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">
- <?php
- $edit=$conn->query("select * from member where memberid='".$row['memberid']."'");
- $erow=$edit->fetch_array();
- ?>
- <div class="container-fluid">
- <form method="POST" action="edit.php?id=<?php echo $erow['memberid']; ?>">
- <div class="row">
- <div class="col-md-2">
- <label style="position:relative; top:7px;">Firstname:</label>
- </div>
- <div class="col-md-10">
- <input type="text" name="firstname" class="form-control" value="<?php echo $erow['firstname']; ?>">
- </div>
- </div>
- <div style="height:10px;"></div>
- <div class="row">
- <div class="col-md-2">
- <label style="position:relative; top:7px;">Lastname:</label>
- </div>
- <div class="col-md-10">
- <input type="text" name="lastname" class="form-control" value="<?php echo $erow['lastname']; ?>">
- </div>
- </div>
- </div>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
- <button type="submit" class="btn btn-warning"><span class="glyphicon glyphicon-check"></span> Save</button>
- </div>
- </form>
- </div>
- </div>
- </div>
- <!-- /.modal -->
edit.php
Our update code.- <?php
- include('conn.php');
- $id=$_GET['id'];
- $firstname=$_POST['firstname'];
- $lastname=$_POST['lastname'];
- $conn->query("update member set firstname='$firstname', lastname='$lastname' where memberid='$id'");
- $_SESSION['msg']="Member Updated Succesfully";
- ?>
delete.php
Lastly, this is our delete code.- <?php
- include('conn.php');
- $id=$_GET['id'];
- $conn->query("delete from member where memberid='$id'");
- $_SESSION['msg']="Member Deleted Succesfully";
- ?>
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Comments
Add new comment
- Add new comment
- 1065 views