irwan

CRUD

Oct 27th, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. DATABASE : myusers
  2.  
  3. TABEL : members
  4.  
  5. CREATE TABLE IF NOT EXISTS `members` (
  6. `id` int(11) NOT NULL AUTO_INCREMENT,
  7. `firstname` varchar(32) CHARACTER SET utf8 NOT NULL,
  8. `lastname` varchar(32) CHARACTER SET utf8 NOT NULL,
  9. `email` varchar(32) CHARACTER SET utf8 NOT NULL,
  10. `password` varchar(32) CHARACTER SET utf8 NOT NULL,
  11. PRIMARY KEY (`id`);
  12.  
  13.  
  14.  
  15. SCRIPT :
  16.  
  17. config.php
  18.  
  19. <?php
  20. $server = 'localhost';
  21. $user = '';
  22. $pass = '';
  23. $db = 'myusers';
  24.  
  25. // Connect to Database
  26. $connection = mysql_connect("localhost", "root", "")
  27. or die ("Could not connect to server ... \n" . mysql_error ());
  28. mysql_select_db("myusers")
  29. or die ("Could not connect to database ... \n" . mysql_error ());
  30. ?>
  31.  
  32.  
  33.  
  34.  
  35. delete.php
  36.  
  37. <?php
  38. // connect to the database
  39. include('config.php');
  40.  
  41. // check if the 'id' variable is set in URL, and check that it is valid
  42. if (isset($_GET['id']) && is_numeric($_GET['id']))
  43. {
  44. // get id value
  45. $id = $_GET['id'];
  46.  
  47. // delete the entry
  48. $result = mysql_query("DELETE FROM members WHERE id=$id")
  49. or die(mysql_error());
  50.  
  51. // redirect back to the view page
  52. header("Location: view.php");
  53. }
  54. else
  55. // if id isn't set, or isn't valid, redirect back to view page
  56. {
  57. header("Location: view.php");
  58. }
  59. ?>
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. edit.php
  67.  
  68. <?php
  69. /*
  70. EDIT.PHP
  71. Allows user to edit specific entry in database
  72. */
  73.  
  74. // creates the edit record form
  75. // since this form is used multiple times in this file, I have made it a function that is easily reusable
  76. function renderForm($id, $firstname, $lastname, $error)
  77. {
  78. ?>
  79. <html>
  80. <head>
  81. <title>Edit</title>
  82. </head>
  83. <body>
  84. <?php
  85. // if there are any errors, display them
  86. if ($error != '')
  87. {
  88. echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
  89. }
  90. ?>
  91. <form action="" method="post">
  92. <input type="hidden" name="id" value="<?php echo $id; ?>"/>
  93. <div>
  94. <p><strong>ID:</strong> <?php echo $id; ?></p>
  95. <strong>First Name: *</strong> <input type="text" name="firstname" value="<?php echo $firstname; ?>"/><br/>
  96. <strong>Last Name: *</strong> <input type="text" name="lastname" value="<?php echo $lastname; ?>"/><br/>
  97. <p>* Required</p>
  98. <input type="submit" name="submit" value="Submit">
  99. </div>
  100. </form>
  101. </body>
  102. </html>
  103. <?php
  104. }
  105. // connect to the database
  106. include('config.php');
  107.  
  108. // check if the form has been submitted. If it has, process the form and save it to the database
  109. if (isset($_POST['submit']))
  110. {
  111. // confirm that the 'id' value is a valid integer before getting the form data
  112. if (is_numeric($_POST['id']))
  113. {
  114. // get form data, making sure it is valid
  115. $id = $_POST['id'];
  116. $firstname = mysql_real_escape_string(htmlspecialchars($_POST['firstname']));
  117. $lastname = mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
  118.  
  119. // check that firstname/lastname fields are both filled in
  120. if ($firstname == '' || $lastname == '')
  121. {
  122. // generate error message
  123. $error = 'ERROR: Please fill in all required fields!';
  124.  
  125. //error, display form
  126. renderForm($id, $firstname, $lastname, $error);
  127. }
  128. else
  129. {
  130. // save the data to the database
  131. mysql_query("UPDATE members SET firstname='$firstname', lastname='$lastname' WHERE id='$id'")
  132. or die(mysql_error());
  133.  
  134. // once saved, redirect back to the view page
  135. header("Location: view.php");
  136. }
  137. }
  138. else
  139. {
  140. // if the 'id' isn't valid, display an error
  141. echo 'Error!';
  142. }
  143. }
  144. else
  145. // if the form hasn't been submitted, get the data from the db and display the form
  146. {
  147.  
  148. // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
  149. if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
  150. {
  151. // query db
  152. $id = $_GET['id'];
  153. $result = mysql_query("SELECT * FROM members WHERE id=$id")
  154. or die(mysql_error());
  155. $row = mysql_fetch_array($result);
  156.  
  157. // check that the 'id' matches up with a row in the databse
  158. if($row)
  159. {
  160. // get data from db
  161. $firstname = $row['firstname'];
  162. $lastname = $row['lastname'];
  163.  
  164. // show form
  165. renderForm($id, $firstname, $lastname, '');
  166. }
  167. else
  168. // if no match, display result
  169. {
  170. echo "No results!";
  171. }
  172. }
  173. else
  174. // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
  175. {
  176. echo 'Error!';
  177. }
  178. }
  179. ?>
  180.  
  181.  
  182.  
  183.  
  184. update.php
  185.  
  186. <html>
  187. <head>
  188. <title>update</title>
  189. </head>
  190. <body>
  191. <form action="" method="post">
  192. <div>
  193. <strong>First Name: </strong> <input type="text" name="firstname" value="" /><br/>
  194. <strong>Last Name: </strong> <input type="text" name="lastname" value="" /><br/>
  195. <strong>Email: </strong> <input type="text" name="email" value="" /><br/>
  196. <strong>Password: </strong> <input type="text" name="password" value="" /><br/>
  197. <p>* required</p>
  198. <input type="submit" name="submit" value="Submit">
  199. </div>
  200. </form>
  201. <?php
  202. // connect to the database
  203. include('config.php');
  204.  
  205. // check if the form has been submitted. If it has, start to process the form and save it to the database
  206. if (isset($_POST['submit']))
  207. {
  208. // get form data, making sure it is valid
  209. $firstname = mysql_real_escape_string(htmlspecialchars($_POST['firstname']));
  210.  
  211. $lastname = mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
  212.  
  213. $email = mysql_real_escape_string(htmlspecialchars($_POST['email']));
  214. $password = mysql_real_escape_string(htmlspecialchars($_POST['password']));
  215.  
  216. // check to make sure both fields are entered
  217. if ($firstname=='' || $lastname=='' || $email == '' || $password == '')
  218. {
  219. // generate error message
  220. $error = 'ERROR: Please fill in all required fields!';
  221.  
  222. // if either field is blank, display the form again
  223. renderForm($firstname, $lastname, $email, $password, $error);
  224. }
  225. else
  226. {
  227. // save the data to the database
  228. mysql_query("INSERT members SET firstname='$firstname', lastname='$lastname', email='$email', password='$password'")
  229. or die(mysql_error());
  230.  
  231. // once saved, redirect back to the view page
  232. header("Location: view.php");
  233. }
  234. }
  235. ?>
  236. </body>
  237. </html>
  238.  
  239.  
  240.  
  241.  
  242.  
  243. view.php
  244.  
  245. <html>
  246. <head>
  247. <title>view</title>
  248. </head>
  249.  
  250. <body>
  251. <?php
  252. // connect to the database
  253. include('config.php');
  254.  
  255. // get results from database
  256. $result = mysql_query("SELECT*FROM members")
  257. or die(mysql_error());
  258.  
  259. // display data in table
  260. echo "<p><b>View All</b> | <a href='viewpage.php?page=1'>View Paginated</a></p>";
  261.  
  262. echo "<table border='1' cellpadding='10'>";
  263. echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>email</th> <th>password</th></tr>";
  264.  
  265. // loop through results of database query, displaying them in the table
  266. while($row = mysql_fetch_array( $result )) {
  267.  
  268. // echo out the contents of each row into a table
  269. echo "<tr>";
  270. echo '<td>' . $row['id'] . '</td>';
  271. echo '<td>' . $row['firstname'] . '</td>';
  272. echo '<td>' . $row['lastname'] . '</td>';
  273. echo '<td>' . $row['email'] . '</td>';
  274. echo '<td>' . $row['password'] . '</td>';
  275. echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
  276. echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
  277. echo "</tr>";
  278. }
  279.  
  280. // close table>
  281. echo "</table>";
  282. ?>
  283. <p><a href="update.php">Add a new record</a></p>
  284.  
  285. </body>
  286. </html>
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293. viewpage.php
  294.  
  295. <html>
  296.  
  297. <head>
  298. <title>viewpage</title>
  299. </head>
  300.  
  301. <body>
  302.  
  303. <?php
  304.  
  305. // connect to the database
  306. include('config.php');
  307.  
  308. // number of results to show per page
  309. $per_page = 3;
  310.  
  311. // figure out the total pages in the database
  312. $result = mysql_query("SELECT * FROM members");
  313. $total_results = mysql_num_rows($result);
  314. $total_pages = ceil($total_results / $per_page);
  315.  
  316. // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
  317. if (isset($_GET['page']) && is_numeric($_GET['page']))
  318. {
  319. $show_page = $_GET['page'];
  320.  
  321. // make sure the $show_page value is valid
  322. if ($show_page > 0 && $show_page <= $total_pages)
  323. {
  324. $start = ($show_page -1) * $per_page;
  325. $end = $start + $per_page;
  326. }
  327. else
  328. {
  329. // error - show first set of results
  330. $start = 0;
  331. $end = $per_page;
  332. }
  333. }
  334. else
  335. {
  336. // if page isn't set, show first set of results
  337. $start = 0;
  338. $end = $per_page;
  339. }
  340.  
  341. // display pagination
  342.  
  343. echo "<p><a href='view.php'>View All</a> | <b>View Page:</b> ";
  344. for ($i = 1; $i <= $total_pages; $i++)
  345. {
  346. echo "<a href='viewpage.php?page=$i'>$i</a> ";
  347. }
  348. echo "</p>";
  349.  
  350. // display data in table
  351. echo "<table border='1' cellpadding='10'>";
  352. echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Email</th> <th>Password</th></tr>";
  353.  
  354. // loop through results of database query, displaying them in the table
  355. for ($i = $start; $i < $end; $i++)
  356. {
  357. // make sure that PHP doesn't try to show results that don't exist
  358. if ($i == $total_results) { break; }
  359.  
  360. // echo out the contents of each row into a table
  361. echo "<tr>";
  362. echo '<td>' . mysql_result($result, $i, 'id') . '</td>';
  363. echo '<td>' . mysql_result($result, $i, 'firstname') . '</td>';
  364. echo '<td>' . mysql_result($result, $i, 'lastname') . '</td>';
  365.  
  366. echo '<td>' . mysql_result($result, $i, 'email') . '</td>';
  367.  
  368. echo '<td>' . mysql_result($result, $i, 'password') . '</td>';
  369.  
  370.  
  371. echo '<td><a href="edit.php?id=' . mysql_result($result, $i, 'id') . '">Edit</a></td>';
  372. echo '<td><a href="delete.php?id=' . mysql_result($result, $i, 'id') . '">Delete</a></td>';
  373. echo "</tr>";
  374. }
  375. // close table>
  376. echo "</table>";
  377.  
  378. // pagination
  379.  
  380. ?>
  381. <p><a href="update.php">Add a new record</a></p>
  382. </body>
  383. </html>
Advertisement
Add Comment
Please, Sign In to add comment