Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.24 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Untitled</title>
  5.     <meta charset='utf-8'>
  6.     <link rel="stylesheet" type="text/css" href="css/style.css">
  7. </head>
  8. <body>
  9.     <form method='post'>
  10.         <p> Table info</p>
  11.         <input type='text' name='firstname' placeholder='Vardas' pattern='[a-zA-Z]{5,30}'><br>
  12.         <input type='text' name='email' placeholder='Email' pattern='[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z]{2,5}'><br>
  13.         <input type='text' name='message' placeholder='Tekstas' pattern='[a-zA-Z0-9\s]{5,}'><br>
  14.         <button type='submit' name='insert'>Siusti</button><br>
  15.         <input type='text' name='deleteEdit' placeholder='ID' pattern='[0-9]{1,4}'><br>
  16.         <button type='submit' name='edit'>Redaguoti</button>
  17.         <button type='submit' name='delete'>Trinti</button>
  18.         <button type='submit' name='display'>Rodyti duombaze</button>
  19.     </form>
  20.  
  21. <?php
  22.     $servername = "localhost";
  23.     $username = "root";
  24.     $password = "";
  25.     $dbname = "test";
  26.  
  27.     // Create connection
  28.     $conn = new mysqli($servername, $username, $password, $dbname);
  29.  
  30.     // Check connection
  31.     if ($conn->connect_error) {
  32.             die("Connection failed: " . $conn->connect_error);
  33.     }
  34.  
  35.     // Insert
  36.    
  37.     if (isset($_POST['insert'])) {
  38.  
  39.       $name = $_POST['firstname'];
  40.       $email = $_POST['email'];
  41.       $text = $_POST['message'];
  42.       $ip = $_SERVER['REMOTE_ADDR'];
  43.  
  44.    
  45.          if ((preg_match('/[a-zA-Z]{5,30}/', $name)) and (preg_match('/[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z]{2,5}/', $email)) and (preg_match('/[a-zA-Z0-9\s]{5,}/', $text))) {
  46.  
  47.             $sql = "INSERT INTO table(ip, vardas, epastas, zinute) VALUES('$ip', '$name', '$email', '$text')";
  48.                 if ($conn->query($sql) === TRUE) {
  49.             echo "New record created successfully";
  50.                 } else {
  51.             echo "Error: " . $sql . "<br>" . $conn->error;
  52.           }
  53.           } else {
  54.             die ("Form filled incorrectly");
  55.           }
  56.         }
  57.  
  58.     // Delete
  59.  
  60.     if (isset($_POST['delete'])) {
  61.         $id = $_POST['deleteEdit'];
  62.  
  63.         $check = mysqli_query($conn, "SELECT * FROM table WHERE id = '$id'");
  64.  
  65.         if (mysqli_num_rows($check) > 0) {  
  66.              } else {
  67.             die ("ID does not exist in table");
  68.              }
  69.  
  70.         if (preg_match('/[0-9]{1,4}/', $id)) {
  71.             $sql ="DELETE FROM table WHERE id = '$id'";
  72.             if ($conn->query($sql) === TRUE) {
  73.                 echo "Record deleted successfully";
  74.             } else {
  75.                 echo "Error: " . $sql . "<br>" . $conn->error;
  76.             }
  77.         } else {
  78.             die ("Error, not deleted");
  79.             }
  80.         }
  81.  
  82.     // Edit
  83.  
  84.     if (isset($_POST['edit'])) {
  85.  
  86.         $id = $_POST['deleteEdit'];
  87.         $name = $_POST['firstname'];
  88.         $email = $_POST['email'];
  89.         $text = $_POST['message'];
  90.         $ip = $_SERVER['REMOTE_ADDR'];
  91.  
  92.        
  93.             if ((preg_match('/[a-zA-Z]{5,30}/', $name)) and (preg_match('/[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-z]{2,5}/', $email)) and (preg_match('/[a-zA-Z0-9\s]{5,}/', $text)) and (preg_match('/[0-9]{1,4}/', $id))) {
  94.  
  95.                 $check = mysqli_query($conn, "SELECT * FROM table WHERE id = '$id'");
  96.  
  97.                 if (mysqli_num_rows($check) > 0) {} else {
  98.                     die ("ID does not exist in table");
  99.                 }
  100.  
  101.                 $sql = "UPDATE table SET vardas ='$name', epastas = '$email', zinute = '$text', ip = '$ip' WHERE id='$id'";
  102.  
  103.                 if ($conn->query($sql) === TRUE) {
  104.                     echo "Record updated successfully";
  105.                 } else {
  106.                     echo "Error: " . $sql . "<br>" . $conn->error;
  107.                 }
  108.             } else {
  109.                  die ("Form filled incorrectly");
  110.             }
  111.     }
  112.  
  113.     // Show data on page
  114.  
  115.     if (isset($_POST['display'])) {
  116.  
  117.         echo "<table>
  118.                <tr>
  119.                     <th>ID</th>
  120.                     <th>Vardas</th>
  121.                     <th>E-pastas</th>
  122.                     <th>Zinute</th>
  123.                 </tr>";
  124.  
  125.         $id = $_POST['deleteEdit'];
  126.         if ($id == TRUE) {
  127.             $check = mysqli_query($conn, "SELECT * FROM table WHERE id = '$id'");
  128.             if ($check->num_rows > 0) {
  129.             } else {
  130.                 $check = mysqli_query($conn, "SELECT * FROM table");
  131.             }
  132.         } else {
  133.             $check = mysqli_query($conn, "SELECT * FROM table");
  134.         }
  135.         if ($check->num_rows > 0) {
  136.             while ($row = $check->fetch_assoc()) {
  137.                 echo "<tr>
  138.                         <td>" . $row["id"] . "</td>
  139.                         <td>" . $row["vardas"] . "</td>
  140.                         <td>" . $row["epastas"] . "</td>
  141.                         <td>" . $row["zinute"] . "</td>
  142.                         </tr>";
  143.             }
  144.         }
  145.         echo "</table>";
  146.     }
  147.  
  148. $conn->close();
  149.  
  150. ?>
  151. </body>
  152. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement