├── README.md
├── add.html
├── add.php
├── classes
├── Crud.php
├── DbConfig.php
└── Validation.php
├── config.php
├── database.sql
├── delete.php
├── edit.php
├── editaction.php
└── index.php
/README.md:
--------------------------------------------------------------------------------
1 | Simple Create, Read, Update, Delete (CRUD) in PHP & MySQL using Object Oriented Programming (OOP)
2 | ========
3 |
4 | A simple and basic system to add, edit, delete and view using PHP and MySQL using OOP.
5 |
6 | Blog Article: [PHP: CRUD (Add, Edit, Delete, View) Application using OOP (Object Oriented Programming)](http://blog.chapagain.com.np/php-crud-add-edit-delete-view-application-using-oop-object-oriented-programming/)
7 |
8 | SQL script to create database and tables is present in **database.sql** file.
9 |
10 |
--------------------------------------------------------------------------------
/add.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Add Data
4 |
19 |
20 |
21 |
22 | Home
23 |
24 |
25 |
26 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/add.php:
--------------------------------------------------------------------------------
1 |
2 |
3 | Add Data
4 |
5 |
6 |
7 | escape_string($_POST['name']);
17 | $age = $crud->escape_string($_POST['age']);
18 | $email = $crud->escape_string($_POST['email']);
19 |
20 | $msg = $validation->check_empty($_POST, array('name', 'age', 'email'));
21 | $check_age = $validation->is_age_valid($_POST['age']);
22 | $check_email = $validation->is_email_valid($_POST['email']);
23 |
24 | // checking empty fields
25 | if($msg != null) {
26 | echo $msg;
27 | //link to the previous page
28 | echo "
Go Back";
29 | } elseif (!$check_age) {
30 | echo 'Please provide proper age.';
31 | } elseif (!$check_email) {
32 | echo 'Please provide proper email.';
33 | }
34 | else {
35 | // if all the fields are filled (not empty)
36 |
37 | //insert data to database
38 | $result = $crud->execute("INSERT INTO users(name,age,email) VALUES('$name','$age','$email')");
39 |
40 | //display success message
41 | echo "Data added successfully.";
42 | echo "
View Result";
43 | }
44 | }
45 | ?>
46 |
47 |
48 |
--------------------------------------------------------------------------------
/classes/Crud.php:
--------------------------------------------------------------------------------
1 | connection->query($query);
14 |
15 | if ($result == false) {
16 | return false;
17 | }
18 |
19 | $rows = array();
20 |
21 | while ($row = $result->fetch_assoc()) {
22 | $rows[] = $row;
23 | }
24 |
25 | return $rows;
26 | }
27 |
28 | public function execute($query)
29 | {
30 | $result = $this->connection->query($query);
31 |
32 | if ($result == false) {
33 | echo 'Error: cannot execute the command';
34 | return false;
35 | } else {
36 | return true;
37 | }
38 | }
39 |
40 | public function delete($id, $table)
41 | {
42 | $query = "DELETE FROM $table WHERE id = $id";
43 |
44 | $result = $this->connection->query($query);
45 |
46 | if ($result == false) {
47 | echo 'Error: cannot delete id ' . $id . ' from table ' . $table;
48 | return false;
49 | } else {
50 | return true;
51 | }
52 | }
53 |
54 | public function escape_string($value)
55 | {
56 | return $this->connection->real_escape_string($value);
57 | }
58 | }
59 | ?>
60 |
--------------------------------------------------------------------------------
/classes/DbConfig.php:
--------------------------------------------------------------------------------
1 | connection)) {
14 |
15 | $this->connection = new mysqli($this->_host, $this->_username, $this->_password, $this->_database);
16 |
17 | if (!$this->connection) {
18 | echo 'Cannot connect to database server';
19 | exit;
20 | }
21 | }
22 |
23 | return $this->connection;
24 | }
25 | }
26 | ?>
27 |
--------------------------------------------------------------------------------
/classes/Validation.php:
--------------------------------------------------------------------------------
1 | ";
10 | }
11 | }
12 | return $msg;
13 | }
14 |
15 | public function is_age_valid($age)
16 | {
17 | //if (is_numeric($age)) {
18 | if (preg_match("/^[0-9]+$/", $age)) {
19 | return true;
20 | }
21 | return false;
22 | }
23 |
24 | public function is_email_valid($email)
25 | {
26 | //if (preg_match("/^[_a-z0-9-+]+(\.[_a-z0-9-+]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {
27 | if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
28 | return true;
29 | }
30 | return false;
31 | }
32 | }
33 | ?>
34 |
--------------------------------------------------------------------------------
/config.php:
--------------------------------------------------------------------------------
1 |
18 |
--------------------------------------------------------------------------------
/database.sql:
--------------------------------------------------------------------------------
1 | create database test;
2 |
3 | use test;
4 |
5 | CREATE TABLE `users` (
6 | `id` int(11) NOT NULL auto_increment,
7 | `name` varchar(100) NOT NULL,
8 | `age` int(3) NOT NULL,
9 | `email` varchar(100) NOT NULL,
10 | PRIMARY KEY (`id`)
11 | );
--------------------------------------------------------------------------------
/delete.php:
--------------------------------------------------------------------------------
1 | escape_string($_GET['id']);
9 |
10 | //deleting the row from table
11 | //$result = $crud->execute("DELETE FROM users WHERE id=$id");
12 | $result = $crud->delete($id, 'users');
13 |
14 | if ($result) {
15 | //redirecting to the display page (index.php in our case)
16 | header("Location:index.php");
17 | }
18 | ?>
19 |
20 |
--------------------------------------------------------------------------------
/edit.php:
--------------------------------------------------------------------------------
1 | escape_string($_GET['id']);
9 |
10 | //selecting data associated with this particular id
11 | $result = $crud->getData("SELECT * FROM users WHERE id=$id");
12 |
13 | foreach ($result as $res) {
14 | $name = $res['name'];
15 | $age = $res['age'];
16 | $email = $res['email'];
17 | }
18 | ?>
19 |
20 |
21 | Edit Data
22 |
23 |
24 |
25 | Home
26 |
27 |
28 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/editaction.php:
--------------------------------------------------------------------------------
1 | escape_string($_POST['id']);
12 |
13 | $name = $crud->escape_string($_POST['name']);
14 | $age = $crud->escape_string($_POST['age']);
15 | $email = $crud->escape_string($_POST['email']);
16 |
17 | $msg = $validation->check_empty($_POST, array('name', 'age', 'email'));
18 | $check_age = $validation->is_age_valid($_POST['age']);
19 | $check_email = $validation->is_email_valid($_POST['email']);
20 |
21 | // checking empty fields
22 | if($msg) {
23 | echo $msg;
24 | //link to the previous page
25 | echo "
Go Back";
26 | } elseif (!$check_age) {
27 | echo 'Please provide proper age.';
28 | } elseif (!$check_email) {
29 | echo 'Please provide proper email.';
30 | } else {
31 | //updating the table
32 | $result = $crud->execute("UPDATE users SET name='$name',age='$age',email='$email' WHERE id=$id");
33 |
34 | //redirectig to the display page. In our case, it is index.php
35 | header("Location: index.php");
36 | }
37 | }
38 | ?>
39 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 | getData($query);
10 | //echo ''; print_r($result); exit;
11 | ?>
12 |
13 |
14 |
15 | Homepage
16 |
17 |
18 |
19 | Add New Data
20 |
21 |
22 |
23 |
24 | Name |
25 | Age |
26 | Email |
27 | Update |
28 |
29 | $res) {
31 | //while($res = mysqli_fetch_array($result)) {
32 | echo "";
33 | echo "".$res['name']." | ";
34 | echo "".$res['age']." | ";
35 | echo "".$res['email']." | ";
36 | echo "Edit | Delete | ";
37 | }
38 | ?>
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------