×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: William Davies
Added: Dec 5, 2014 10:54 PM
Views: 32
Tags: no tags
PHP template to help get started
  1. <?php
  2. //connection to database
  3. $conn = mysqli_connect('localhost', 'root', 'root', 'users');
  4. //check connection
  5.   {
  6.   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  7.   }
  8. //data entered from form stored in variables
  9. $firstName = mysqli_escape_string($_POST['fname']);
  10. $lastName = mysqli_escape_string($_POST['lname']);
  11. $message = mysqli_escape_string($_POST['msg']);
  12. //for everything posted output both parts the associate array
  13. foreach ($_POST as $key => $post) {
  14.         echo "$key $post <br>";
  15. }
  16. //query to insert posted data
  17. $sqlInsert = "INSERT into entries values('','$firstName', '$lastName', '$message')";
  18. //perform query insert
  19. mysqli_query($con, $sqlInsert);
  20. //query to select all data
  21. $sqlSelect= "SELECT * from entries";
  22. //variable to store result of select query
  23. $result = mysqli_query($con, $sqlSelect);
  24. echo "<ul>";
  25. //uses a while loop to echo out elements of array using $row as the temporary variable
  26. while ($row = mysqli_fetch_assoc($result)) {
  27.         echo "<li>" . $row['first_name'] . "</li>";
  28.         echo "<li>" . $row['last_name'] . "</li>";
  29.         echo "<li>" . $row['message'] . "</li>";
  30.         echo "<br />";
  31. }
  32. echo "</ul>";
  33. //free memory
  34. //close connection
  35. ?>