Advertisement
Guest User

Untitled

a guest
Mar 7th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. <?php
  2. $servername = "Localhost";
  3. $username = "root";
  4. $password = "";
  5. $dbname = "form";
  6.  
  7.  
  8. function TimsDump($v)
  9. {
  10. echo "<pre style='background-color:#fff;'>";
  11. var_dump($v);
  12. echo "</pre>";
  13. }
  14.  
  15. // Create connection
  16. $conn = new mysqli($servername, $username, $password, $dbname);
  17. // Check connection
  18. if ($conn->connect_error) {
  19. die("Connection failed: " . $conn->connect_error);
  20. }
  21.  
  22. // check to see if form is submitted
  23. if(isset($_POST['save']))
  24. {
  25. // error messages
  26. $errorMessages = array();
  27.  
  28. // assign posted form data to some new variables. Makes it easier.
  29. $lastname = $_POST["lastname"];
  30. $firstname = $_POST['firstname'];
  31. $email = $_POST['email'];
  32. $message = $_POST['message'];
  33.  
  34. if($firstname == "")
  35. {
  36. $errorMessages[] = "Hey dude, please fill your first name!";
  37. }
  38.  
  39. if($lastname == "")
  40. {
  41. $errorMessages[] = "Hey dude, please fill your last name!";
  42. }
  43.  
  44. if($message == "")
  45. {
  46. $errorMessages[] = "Hey dude, please fill a message!";
  47. }
  48.  
  49. // now count number of keys or indexes in $errorMessages
  50. // if zero, all's fine
  51. // else, dont run query
  52. if(count($errorMessages) == 0)
  53. {
  54. $sql = "INSERT INTO users (firstname, lastname, email, message)
  55. VALUES ('".$_POST["firstname"]."','".$_POST["lastname"]."','".$_POST["email"]."','".$_POST["message"]."')";
  56. $result = mysqli_query($conn,$sql);
  57. }
  58.  
  59. }
  60.  
  61. // the SQL string that fetches data....
  62. $selectSQL = "SELECT firstname, lastname, email, message FROM users";
  63. // This is a prepared statement, not necessary with this simple query with no variables, but anyway...
  64.  
  65. // run query and store the resource into variable
  66. $result = $conn->query($selectSQL);
  67.  
  68. // messages array to store data into
  69. $messages = array();
  70. while($row = $result->fetch_assoc())
  71. {
  72. $messages[] = $row;
  73. }
  74.  
  75. TimsDump($errorMessages);
  76. ?>
  77.  
  78. <!Doctype html>
  79. <html>
  80. <head>
  81. <meta charset="utf-8">
  82. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  83. <meta name="description" content="$1">
  84. <meta name="viewport" content="width=device-width, initial-scale=1">
  85. <link rel="stylesheet" type="text/css" href="css/style-index.css">
  86. <title>test</title>
  87. </head>
  88. <body>
  89. <?php
  90. if(!empty($errorMessages))
  91. {
  92. echo "<h3>There are some errors:</h3>";
  93. echo "<ul>";
  94. // array is filled with ...
  95. foreach($errorMessages as $a => $b)
  96. {
  97. ?>
  98. <li><?php echo $b; ?></li>
  99. <?php
  100. }
  101. echo "</ul>";
  102. }
  103. ?>
  104. <form action="index.php" method="POST">
  105. <label id="first"> First name</label><br/>
  106. <input type="text" name="firstname"><br/>
  107.  
  108. <label id="first">Last name</label><br/>
  109. <input type="text" name="lastname"><br/>
  110.  
  111. <label id="first">Email</label><br/>
  112. <input type="text" name="email"><br/>
  113.  
  114. <label id="first">Message</label><br/>
  115. <textarea name="message"></textarea>
  116.  
  117. <button type="submit" name="save">save</button>
  118. </form>
  119.  
  120. <table>
  121. <thead>
  122. <tr>
  123. <th>Firstname</th>
  124. <th>Lastname</th>
  125. <th>E-mail</th>
  126. <th>Message</th>
  127. </tr>
  128. </thead>
  129. <tbody>
  130. <!--Use a while loop to make a table row for every ìndex in $messages-->
  131. <?php
  132. foreach($messages as $k => $v)
  133. {
  134. ?>
  135. <tr>
  136. <!--Each table column is echoed in to a td cell-->
  137. <td><?php echo $v['lastname']; ?></td>
  138. <td><?php echo $v['lastname']; ?></td>
  139. <td><?php echo $v['email']; ?></td>
  140. <td><?php echo $v['message']; ?></td>
  141. </tr>
  142. <?php
  143. // end of loop
  144. }
  145. ?>
  146. </tbody>
  147. </table>
  148. </body>
  149. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement