Advertisement
Guest User

basic crud php

a guest
Mar 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. <?php
  2.  
  3. $mysqli = new mysqli('localhost', 'root', '', 'crud_test') or die(mysqli_error($mysqli));
  4. session_start();
  5.  
  6. $update = false;
  7. $id = 0;
  8. $location = '';
  9. $name = '';
  10.  
  11. if(isset($_POST['save']))
  12. {
  13.     $name = $_POST['name'];
  14.     $location = $_POST['location'];
  15.     $mysqli->query("INSERT INTO data(name, location) VALUES('$name', '$location' )") or die($mysqli->error);
  16.  
  17.     $_SESSION['message'] = "Data succesfully inserted.";
  18.     $_SESSION['msg_type'] = "Success:";
  19.    
  20.     header("location: index.php");
  21. }
  22.  
  23. if(isset($_POST['update']))
  24. {
  25.     $id = $_POST['id'];
  26.     $name = $_POST['name'];
  27.     $location = $_POST['location'];
  28.    
  29.     $mysqli->query("UPDATE data SET name='$name', location='$location' WHERE id = $id") or die($mysqli->error);
  30.  
  31.     $_SESSION['message'] = "Data succesfully Updated.";
  32.     $_SESSION['msg_type'] = "Success:";
  33.    
  34.     header("location: index.php");
  35. }
  36.  
  37. if(isset($_GET['edit']))
  38. {
  39.     $id = $_GET['edit'];
  40.     $result = $mysqli->query("SELECT * FROM data WHERE id=$id") or die($mysqli->error);
  41.     $row = $result->fetch_array();
  42.     $name = $row['name'];
  43.     $location = $row['location'];
  44.     $update = true;
  45. }
  46.  
  47. if(isset($_GET['delete']))
  48. {
  49.     $id = $_GET['delete'];
  50.     $mysqli->query("DELETE FROM data WHERE id=$id") or die($mysqli->error);
  51.    
  52.     $_SESSION['message'] = "Data succesfully Deleted.";
  53.     $_SESSION['msg_type'] = "Information:";
  54.    
  55.     header("location: index.php");
  56. }
  57.    
  58. ?>
  59.  
  60. <!DOCTYPE html>
  61. <html>
  62.     <head>
  63.         <title>Crud test</title>
  64.     </head>
  65.     <body>
  66.     <?php
  67.         require_once "process.php";
  68.         $mysqli = new mysqli('localhost', 'root', '', 'crud_test') or die(mysqli_error($mysqli));
  69.         $result = $mysqli->query("SELECT * FROM data") or die($mysqli->error);
  70.        
  71.         if(isset($_SESSION['message']))
  72.         {
  73.             echo $_SESSION['msg_type'] . " " . $_SESSION['message'];
  74.             unset($_SESSION['message']);
  75.         }
  76.     ?>
  77.         <form action="process.php" method="POST">
  78.             <input type="hidden" name="id" value="<?php echo $id; ?>">
  79.             <input type="text" placeholder="name" value="<?php echo $name; ?>"  name="name">
  80.             <input type="text" placeholder="location" value="<?php echo $location; ?>" name="location">
  81.             <?php
  82.                 if($update == true)
  83.                 {
  84.                     echo "<button type='submit' name='update'>Update</button>";
  85.                 }
  86.                 else
  87.                 {
  88.                     echo "<button type='submit' name='save'>Save</button>";
  89.                 }
  90.             ?>
  91.         </form>
  92.        
  93.         <table>
  94.             <thead>
  95.                 <tr>
  96.                     <td>Name</td>
  97.                     <td>Location</td>
  98.                     <td>Action</td>
  99.                 </tr>
  100.             </thead>
  101.             <tbody>
  102.                 <?php
  103.                     while($row = $result->fetch_assoc())
  104.                     {
  105.                         echo "<tr>";
  106.                         echo "<td>{$row['name']}</td>";
  107.                         echo "<td>{$row['location']}</td>";
  108.                         echo "<td><a href='index.php?edit={$row['id']}'>Edit</a>
  109.                                   <a href='process.php?delete={$row['id']}'>Delete</a></td>";
  110.                         echo "</tr>";
  111.                     }
  112.                 ?>
  113.             </tbody>
  114.         </table>
  115.     </body>
  116.    
  117. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement