├── database.sql ├── README.md ├── delete.php ├── config.php ├── add.html ├── index.php ├── add.php └── edit.php /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 | ); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Simple PHP-MySQL CRUD (Add, Edit, Delete, View) using PDO 2 | ======== 3 | 4 | A simple and basic system to add, edit, delete and view using PHP and MySQL. 5 | 6 | Blog Article: [Simple PHP-MySQL CRUD (Add, Edit, Delete, View) using PDO](http://blog.chapagain.com.np/php-mysql-simple-crud-add-edit-delete-view-using-pdo/) 7 | 8 | SQL script to create database and tables is present in **database.sql** file. 9 | 10 | -------------------------------------------------------------------------------- /delete.php: -------------------------------------------------------------------------------- 1 | prepare($sql); 11 | $query->execute(array(':id' => $id)); 12 | 13 | //redirecting to the display page (index.php in our case) 14 | header("Location:index.php"); 15 | ?> 16 | -------------------------------------------------------------------------------- /config.php: -------------------------------------------------------------------------------- 1 | setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Setting Error Mode as Exception 13 | // More on setAttribute: http://php.net/manual/en/pdo.setattribute.php 14 | } catch(PDOException $e) { 15 | echo $e->getMessage(); 16 | } 17 | 18 | ?> 19 | -------------------------------------------------------------------------------- /add.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Add Data 4 | 5 | 6 | 7 | Home 8 |

9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 |
Name
Age
Email
29 |
30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | query("SELECT * FROM users ORDER BY id DESC"); 7 | ?> 8 | 9 | 10 | 11 | Homepage 12 | 13 | 14 | 15 | Add New Data

16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | fetch(PDO::FETCH_ASSOC)) { 27 | echo ""; 28 | echo ""; 29 | echo ""; 30 | echo ""; 31 | echo ""; 32 | } 33 | ?> 34 |
NameAgeEmailUpdate
".$row['name']."".$row['age']."".$row['email']."Edit | Delete
35 | 36 | 37 | -------------------------------------------------------------------------------- /add.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | Add Data 4 | 5 | 6 | 7 | Name field is empty.
"; 21 | } 22 | 23 | if(empty($age)) { 24 | echo "Age field is empty.
"; 25 | } 26 | 27 | if(empty($email)) { 28 | echo "Email field is empty.
"; 29 | } 30 | 31 | //link to the previous page 32 | echo "
Go Back"; 33 | } else { 34 | // if all the fields are filled (not empty) 35 | 36 | //insert data to database 37 | $sql = "INSERT INTO users(name, age, email) VALUES(:name, :age, :email)"; 38 | $query = $dbConn->prepare($sql); 39 | 40 | $query->bindparam(':name', $name); 41 | $query->bindparam(':age', $age); 42 | $query->bindparam(':email', $email); 43 | $query->execute(); 44 | 45 | // Alternative to above bindparam and execute 46 | // $query->execute(array(':name' => $name, ':email' => $email, ':age' => $age)); 47 | 48 | //display success message 49 | echo "Data added successfully."; 50 | echo "
View Result"; 51 | } 52 | } 53 | ?> 54 | 55 | 56 | -------------------------------------------------------------------------------- /edit.php: -------------------------------------------------------------------------------- 1 | Name field is empty.

"; 18 | } 19 | 20 | if(empty($age)) { 21 | echo "Age field is empty.
"; 22 | } 23 | 24 | if(empty($email)) { 25 | echo "Email field is empty.
"; 26 | } 27 | } else { 28 | //updating the table 29 | $sql = "UPDATE users SET name=:name, age=:age, email=:email WHERE id=:id"; 30 | $query = $dbConn->prepare($sql); 31 | 32 | $query->bindparam(':id', $id); 33 | $query->bindparam(':name', $name); 34 | $query->bindparam(':age', $age); 35 | $query->bindparam(':email', $email); 36 | $query->execute(); 37 | 38 | // Alternative to above bindparam and execute 39 | // $query->execute(array(':id' => $id, ':name' => $name, ':email' => $email, ':age' => $age)); 40 | 41 | //redirectig to the display page. In our case, it is index.php 42 | header("Location: index.php"); 43 | } 44 | } 45 | ?> 46 | prepare($sql); 53 | $query->execute(array(':id' => $id)); 54 | 55 | while($row = $query->fetch(PDO::FETCH_ASSOC)) 56 | { 57 | $name = $row['name']; 58 | $age = $row['age']; 59 | $email = $row['email']; 60 | } 61 | ?> 62 | 63 | 64 | Edit Data 65 | 66 | 67 | 68 | Home 69 |

70 | 71 |
72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 |
Name
Age
Email
>
90 |
91 | 92 | 93 | --------------------------------------------------------------------------------