daily pastebin goal
68%
SHARE
TWEET

Untitled

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