Advertisement
Guest User

Untitled

a guest
Apr 10th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. index.php
  2.  
  3.  
  4. <?php
  5.  
  6. include_once("../class/Crud.php");
  7.  
  8. $crud = new Crud();
  9.  
  10. $query = "SELECT * FROM users ORDER BY id DESC ";
  11. $result = $crud->getData($query);
  12. //echo "
  13.  
  14. "; print_r($result); exit;
  15.  
  16. ?>
  17.  
  18. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  19.  
  20.  
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  22. <title>Homepage
  23.  
  24.  
  25.  
  26. <a href="add.php">Add New Data</a><br><br>
  27.  
  28. <?php
  29.  
  30. foreach($result as $key => $res) {
  31. echo "";
  32. echo "";
  33. echo "";
  34. echo "";
  35. echo " ";
  36. }
  37.  
  38. ?>
  39. <table width="80%" border="0"><tbody><tr> <td>Name</td> <td>Age</td> <td>Email</td> <td>Update</td> </tr><tr><td>".$res['name']."</td><td>".$res['age']."</td><td>".$res['email']."</td><td><a href="edit.php?id=$res[id]">Edit |
  40. <a href="delete.php?id=$res[id]" onclick="return confirm('Are you sure you want to delete?')">Delete</td></tr></tbody></table>
  41.  
  42.  
  43.  
  44.  
  45.  
  46. Crud.php
  47.  
  48.  
  49.  
  50. <?php
  51.  
  52. include_once '../class/DbConfig.php';
  53.  
  54. class Crud extends DbConfig {
  55. public function __construct() {
  56. parent::__construct();
  57. }
  58.  
  59. public function getData($query) {
  60. $result = $this->connection->query($query);
  61.  
  62. if($result == false) {
  63. return false;
  64. }
  65.  
  66. $rows = array();
  67.  
  68. while($row = $result->fetch_assoc()) {
  69. $rows[] = $row;
  70. }
  71. return $rows;
  72. }
  73.  
  74. public function execute($query) {
  75. $result = $this->connection->query($query);
  76.  
  77. if($result == false) {
  78. echo "can't execute the command";
  79. return false;
  80. } else {
  81. return true;
  82. }
  83. }
  84.  
  85. public function escape_string($value) {
  86. return $this->connection->real_escape_string($value);
  87. }
  88. }
  89.  
  90. ?>
  91.  
  92.  
  93. add.php
  94.  
  95.  
  96. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  97.  
  98.  
  99. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  100. <title>Add data
  101.  
  102.  
  103.  
  104. <?php
  105.  
  106. include_once("../class/Crud.php");
  107. include_once("../class/Validation.php");
  108.  
  109. $crud = new Crud();
  110. $validation = new Validation();
  111.  
  112. if(isset($_POST['Submit'])) {
  113. $name = $crud->escape_string($_POST['name']);
  114. $age = $crud->escape_string($_POST['age']);
  115. $email = $crud->escape_string($_POST['email']);
  116.  
  117. $msg = $validation->check_empty($_POST, array('name', 'age', 'email'));
  118. $check_age = $validation->is_age_valid($_POST['age']);
  119. $check_email = $validation->is_email_valid($_POST['email']);
  120.  
  121. if($msg != null) {
  122. echo $msg;
  123. echo "<br><a>Go back</a>";
  124. } else if(!$check_age) {
  125. echo "Please provide proper age.";
  126. } else if(!$check_email) {
  127. echo "Please provide proper email.";
  128. }
  129. else {
  130. $result = $crud->execute("INSERT INTO users(name, age, email)VALUES('$name', '$age', '$email')");
  131.  
  132. echo "<font color="green">Data added successfully.";
  133. echo "<br><a href="index.php">View result</a>";
  134. }
  135. }
  136.  
  137. ?>
  138.  
  139.  
  140.  
  141.  
  142. Validation.php
  143.  
  144.  
  145. <?php
  146.  
  147. class Validation {
  148. public function check_empty($data, $fields) {
  149. $msg = null;
  150. foreach($fields as $value) {
  151. if(empty($data[$value])) {
  152. $msg .= "$value field empty <br>";
  153. }
  154. }
  155. return $msg;
  156. }
  157.  
  158. public function is_age_valid($age) {
  159. if(preg_match("/^[0-9]+$/", $age)) {
  160. return true;
  161. }
  162. return false;
  163. }
  164.  
  165. public function is_email_valid($email) {
  166. if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
  167. return true;
  168. }
  169. return false;
  170. }
  171. }
  172.  
  173. ?>
  174.  
  175.  
  176. DbCongig.php
  177.  
  178.  
  179. <?php
  180.  
  181. class DbConfig {
  182. private $_host = 'localhost';
  183. private $_username = 'root';
  184. private $_password = '';
  185. private $_database = 'test1';
  186.  
  187. protected $connection;
  188.  
  189. public function __construct() {
  190. if(!isset($this->connection)) {
  191. $this->connection = new mysqli($this->_host, $this->_username, $this->_password, $this->_database);
  192.  
  193. if(! $this->connection) {
  194. echo "can't connect to database";
  195. exit;
  196. }
  197. }
  198. return $this->connection;
  199. }
  200. }
  201.  
  202. ?>
  203.  
  204.  
  205. add.html
  206.  
  207.  
  208. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  209.  
  210.  
  211. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  212. <title>Add data
  213.  
  214.  
  215.  
  216. <a href="add.php">Home</a>
  217. <br><br>
  218.  
  219.  
  220. <table width="25%" border="0"><tbody><tr><td>Name</td><td></td></tr><tr><td>Age</td><td></td></tr><tr><td>Email</td><td></td></tr><tr><td></td><td></td></tr></tbody></table>
  221.  
  222.  
  223.  
  224.  
  225.  
  226. edit.php
  227.  
  228.  
  229. <?php
  230.  
  231. include_once("../class/Crud.php");
  232.  
  233. $crud = new Crud();
  234.  
  235. $id = $crud->escape_string($_GET['id']);
  236.  
  237. $result = $crud->getData("SELECT * FROM users WHERE id=$id");
  238.  
  239. foreach($result as $res) {
  240. $name = $res['name'];
  241. $age = $res['age'];
  242. $email = $res['email'];
  243. }
  244.  
  245. ?>
  246. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  247.  
  248.  
  249. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  250. <title>Edit data
  251.  
  252.  
  253.  
  254. <a href="index.php">Home</a>
  255. <br><br>
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262. <table border="0"><tbody><tr> <td>Name</td> <td></td> </tr><tr> <td>Age</td> <td></td> </tr><tr> <td>Email</td> <td></td> </tr><tr> <td></td> <td></td> </tr> </tbody></table>
  263.  
  264.  
  265.  
  266.  
  267.  
  268. editaction.php
  269.  
  270.  
  271. <?php
  272.  
  273. include_once("../class/Crud.php");
  274. include_once("../class/Validation.php");
  275.  
  276. $crud = new Crud();
  277. $validation = new Validation();
  278.  
  279. if(isset($_POST['update'])) {
  280. $id = $crud->escape_string($_POST['id']);
  281.  
  282. $name = $crud->escape_string($_POST['name']);
  283. $age = $crud->escape_string($_POST['age']);
  284. $email = $crud->escape_string($_POST['email']);
  285.  
  286. $msg = $validation->check_empty($_POST, array('name', 'age', 'email'));
  287. $check_age = $validation->is_age_valid($_POST['age']);
  288. $check_email = $validation->is_email_valid($_POST['email']);
  289.  
  290. if($msg) {
  291. echo $msg;
  292. echo "<br><a>Go Back</a>";
  293. } elseif(! $check_age) {
  294. echo "Please provide proper age.";
  295. } elseif(! $check_email) {
  296. echo "Please provide proper email.";
  297. } else {
  298. $result = $crud->execute("UPDATE users SET name='$name', age='$age', email='$email' WHERE id=$id ");
  299. header("Location:index.php");
  300. }
  301. }
  302.  
  303. ?>
  304.  
  305.  
  306. error:
  307.  
  308. Notice: Undefined index: id in C:xampphtdocscrud_phpclassedit.php on line 7
  309.  
  310. Warning: Invalid argument supplied for foreach() in C:xampphtdocscrud_phpclassedit.php on line 11
  311.  
  312. inside name field: <br>Notice: Undefined variable: name in C:xampphtdocscrud_phpclassedit.php on line 33<br>
  313.  
  314.  
  315. inside age field: <br>Notice: Undefined variable: age in C:xampphtdocscrud_phpclassedit.php on line 38<br>
  316.  
  317.  
  318.  
  319. inside email field: <br>Notice: Undefined variable: email in C:xampphtdocscrud_phpclassedit.php on line 43<br>
  320.  
  321. What I have tried:
  322.  
  323. <big>i try every solution but its shows error like this</big>
  324.  
  325.  
  326. Notice: Undefined index: id in C:xampphtdocscrud_phpclassedit.php on line 7
  327.  
  328. Warning: Invalid argument supplied for foreach() in C:xampphtdocscrud_phpclassedit.php on line 11
  329.  
  330. inside name field: <br>Notice: Undefined variable: name in C:xampphtdocscrud_phpclassedit.php on line 33<br>
  331.  
  332.  
  333. inside age field: <br>Notice: Undefined variable: age in C:xampphtdocscrud_phpclassedit.php on line 38<br>
  334.  
  335.  
  336.  
  337. inside email field: <br>Notice: Undefined variable: email in C:xampphtdocscrud_phpclassedit.php on line 43<br></font>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement