Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 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. <?php
  21. $servername = "localhost";
  22. $username = "root";
  23. $password = "";
  24. $dbname = "test";
  25.  
  26. // Create connection
  27. $conn = new mysqli($servername, $username, $password, $dbname);
  28.  
  29. // Check connection
  30. if ($conn->connect_error) {
  31. die("Connection failed: " . $conn->connect_error);
  32. }
  33.  
  34. // Insert
  35.  
  36. if (isset($_POST['insert'])) {
  37.  
  38. $name = $_POST['firstname'];
  39. $email = $_POST['email'];
  40. $text = $_POST['message'];
  41. $ip = $_SERVER['REMOTE_ADDR'];
  42.  
  43.  
  44. 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))) {
  45.  
  46. $sql = "INSERT INTO mantas_zukauskas(ip, vardas, epastas, zinute) VALUES('$ip', '$name', '$email', '$text')";
  47. if ($conn->query($sql) === TRUE) {
  48. echo "New record created successfully";
  49. } else {
  50. echo "Error: " . $sql . "<br>" . $conn->error;
  51. }
  52. } else {
  53. die ("Form filled incorrectly");
  54. }
  55. }
  56.  
  57. // Delete
  58.  
  59. if (isset($_POST['delete'])) {
  60. $id = $_POST['deleteEdit'];
  61.  
  62. $check = mysqli_query($conn, "SELECT * FROM mantas_zukauskas WHERE id = '$id'");
  63.  
  64. if (mysqli_num_rows($check) > 0) {
  65. } else {
  66. die ("ID does not exist in table");
  67. }
  68.  
  69. if (preg_match('/[0-9]{1,4}/', $id)) {
  70. $sql ="DELETE FROM mantas_zukauskas WHERE id = '$id'";
  71. if ($conn->query($sql) === TRUE) {
  72. echo "Record deleted successfully";
  73. } else {
  74. echo "Error: " . $sql . "<br>" . $conn->error;
  75. }
  76. } else {
  77. die ("Error, not deleted");
  78. }
  79. }
  80.  
  81. // Edit
  82.  
  83. if (isset($_POST['edit'])) {
  84.  
  85. $id = $_POST['deleteEdit'];
  86. $name = $_POST['firstname'];
  87. $email = $_POST['email'];
  88. $text = $_POST['message'];
  89. $ip = $_SERVER['REMOTE_ADDR'];
  90.  
  91.  
  92. 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))) {
  93.  
  94. $check = mysqli_query($conn, "SELECT * FROM mantas_zukauskas WHERE id = '$id'");
  95.  
  96. if (mysqli_num_rows($check) > 0) {} else {
  97. die ("ID does not exist in table");
  98. }
  99.  
  100. $sql = "UPDATE mantas_zukauskas SET vardas ='$name', epastas = '$email', zinute = '$text', ip = '$ip' WHERE id='$id'";
  101.  
  102. if ($conn->query($sql) === TRUE) {
  103. echo "Record updated successfully";
  104. } else {
  105. echo "Error: " . $sql . "<br>" . $conn->error;
  106. }
  107. } else {
  108. die ("Form filled incorrectly");
  109. }
  110. }
  111.  
  112. // Show data on page
  113.  
  114. if (isset($_POST['display'])) {
  115.  
  116. $id = $_POST['deleteEdit'];
  117. if ($id == TRUE) {
  118. $check = mysqli_query($conn, "SELECT * FROM mantas_zukauskas WHERE id = '$id'");
  119. } else {
  120. $check = mysqli_query($conn, "SELECT * FROM mantas_zukauskas");
  121. }
  122. if ($check->num_rows > 0) {
  123. while ($row = $check->fetch_assoc()) {
  124. echo "ID: " . $row["id"]. " Vardas: " . $row["vardas"]. " E-mail: " . $row["epastas"]. " Zinute: " . $row["zinute"]. "<br>";
  125. }
  126. }
  127. }
  128.  
  129. $conn->close();
  130.  
  131. ?>
  132. </body>
  133. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement