Guest User

crud php

a guest
Feb 5th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. config.php
  2. <?php
  3. /*
  4. // mysql_connect("database-host", "username", "password")
  5. $conn = mysql_connect("localhost","root","root")
  6. or die("cannot connected");
  7. // mysql_select_db("database-name", "connection-link-identifier")
  8. @mysql_select_db("test",$conn);
  9. */
  10. /**
  11. * mysql_connect is deprecated
  12. * using mysqli_connect instead
  13. */
  14. $databaseHost = 'localhost';
  15. $databaseName = 'test';
  16. $databaseUsername = 'root';
  17. $databasePassword = 'root';
  18. $mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
  19.  
  20. ?>
  21. ---------------------------------------------------------------------------------------------------
  22. add.php
  23.  
  24. <html>
  25. <head>
  26. <title>Add Data</title>
  27. </head>
  28.  
  29. <body>
  30. <?php
  31. //including the database connection file
  32. include_once("config.php");
  33.  
  34. if(isset($_POST['Submit'])) {
  35. $name = mysqli_real_escape_string($mysqli, $_POST['name']);
  36. $age = mysqli_real_escape_string($mysqli, $_POST['age']);
  37. $email = mysqli_real_escape_string($mysqli, $_POST['email']);
  38.  
  39. // checking empty fields
  40. if(empty($name) || empty($age) || empty($email)) {
  41.  
  42. if(empty($name)) {
  43. echo "<font color='red'>Name field is empty.</font><br/>";
  44. }
  45.  
  46. if(empty($age)) {
  47. echo "<font color='red'>Age field is empty.</font><br/>";
  48. }
  49.  
  50. if(empty($email)) {
  51. echo "<font color='red'>Email field is empty.</font><br/>";
  52. }
  53.  
  54. //link to the previous page
  55. echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
  56. } else {
  57. // if all the fields are filled (not empty)
  58.  
  59. //insert data to database
  60. $result = mysqli_query($mysqli, "INSERT INTO users(name,age,email) VALUES('$name','$age','$email')");
  61.  
  62. //display success message
  63. echo "<font color='green'>Data added successfully.";
  64. echo "<br/><a href='index.php'>View Result</a>";
  65. }
  66. }
  67. ?>
  68. </body>
  69. </html>
  70.  
  71. ----------------------------------------------------------------------------
  72. edit.php
  73.  
  74. <?php
  75. // including the database connection file
  76. include_once("config.php");
  77. if(isset($_POST['update']))
  78. {
  79. $id = mysqli_real_escape_string($mysqli, $_POST['id']);
  80.  
  81. $name = mysqli_real_escape_string($mysqli, $_POST['name']);
  82. $age = mysqli_real_escape_string($mysqli, $_POST['age']);
  83. $email = mysqli_real_escape_string($mysqli, $_POST['email']);
  84.  
  85. // checking empty fields
  86. if(empty($name) || empty($age) || empty($email)) {
  87.  
  88. if(empty($name)) {
  89. echo "<font color='red'>Name field is empty.</font><br/>";
  90. }
  91.  
  92. if(empty($age)) {
  93. echo "<font color='red'>Age field is empty.</font><br/>";
  94. }
  95.  
  96. if(empty($email)) {
  97. echo "<font color='red'>Email field is empty.</font><br/>";
  98. }
  99. } else {
  100. //updating the table
  101. $result = mysqli_query($mysqli, "UPDATE users SET name='$name',age='$age',email='$email' WHERE id=$id");
  102.  
  103. //redirectig to the display page. In our case, it is index.php
  104. header("Location: index.php");
  105. }
  106. }
  107. ?>
  108. <?php
  109. //getting id from url
  110. $id = $_GET['id'];
  111. //selecting data associated with this particular id
  112. $result = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id");
  113. while($res = mysqli_fetch_array($result))
  114. {
  115. $name = $res['name'];
  116. $age = $res['age'];
  117. $email = $res['email'];
  118. }
  119. ?>
  120. <html>
  121. <head>
  122. <title>Edit Data</title>
  123. </head>
  124.  
  125. <body>
  126. <a href="index.php">Home</a>
  127. <br/><br/>
  128.  
  129. <form name="form1" method="post" action="edit.php">
  130. <table border="0">
  131. <tr>
  132. <td>Name</td>
  133. <td><input type="text" name="name" value="<?php echo $name;?>"></td>
  134. </tr>
  135. <tr>
  136. <td>Age</td>
  137. <td><input type="text" name="age" value="<?php echo $age;?>"></td>
  138. </tr>
  139. <tr>
  140. <td>Email</td>
  141. <td><input type="text" name="email" value="<?php echo $email;?>"></td>
  142. </tr>
  143. <tr>
  144. <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>
  145. <td><input type="submit" name="update" value="Update"></td>
  146. </tr>
  147. </table>
  148. </form>
  149. </body>
  150. </html>
  151.  
  152. ------------------------------------------------------------------------------------
  153. delete.php
  154. <?php
  155. //including the database connection file
  156. include("config.php");
  157. //getting id of the data from url
  158. $id = $_GET['id'];
  159. //deleting the row from table
  160. $result = mysqli_query($mysqli, "DELETE FROM users WHERE id=$id");
  161. //redirecting to the display page (index.php in our case)
  162. header("Location:index.php");
  163.  
  164. -------------------------------------------------------------------------------------
  165. index.php
  166. <?php
  167. //including the database connection file
  168. include_once("config.php");
  169. //fetching data in descending order (lastest entry first)
  170. //$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
  171. $result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC"); // using mysqli_query instead
  172. ?>
  173.  
  174. <html>
  175. <head>
  176. <title>Homepage</title>
  177. </head>
  178.  
  179. <body>
  180. <a href="add.html">Add New Data</a><br/><br/>
  181.  
  182. <table width='80%' border=0>
  183.  
  184. <tr bgcolor='#CCCCCC'>
  185. <td>Name</td>
  186. <td>Age</td>
  187. <td>Email</td>
  188. <td>Update</td>
  189. </tr>
  190. <?php
  191. //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
  192. while($res = mysqli_fetch_array($result)) {
  193. echo "<tr>";
  194. echo "<td>".$res['name']."</td>";
  195. echo "<td>".$res['age']."</td>";
  196. echo "<td>".$res['email']."</td>";
  197. echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";
  198. }
  199. ?>
  200. </table>
  201. </body>
  202. </html>
  203. ----------------------------------------------------------------------------
  204. add.html
  205.  
  206. <html>
  207. <head>
  208. <title>Add Data</title>
  209. </head>
  210.  
  211. <body>
  212. <a href="index.php">Home</a>
  213. <br/><br/>
  214.  
  215. <form action="add.php" method="post" name="form1">
  216. <table width="25%" border="0">
  217. <tr>
  218. <td>Name</td>
  219. <td><input type="text" name="name"></td>
  220. </tr>
  221. <tr>
  222. <td>Age</td>
  223. <td><input type="text" name="age"></td>
  224. </tr>
  225. <tr>
  226. <td>Email</td>
  227. <td><input type="text" name="email"></td>
  228. </tr>
  229. <tr>
  230. <td></td>
  231. <td><input type="submit" name="Submit" value="Add"></td>
  232. </tr>
  233. </table>
  234. </form>
  235. </body>
  236. </html>
Add Comment
Please, Sign In to add comment