Advertisement
Guest User

Untitled

a guest
Oct 31st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. <?php
  2. //Set up the database access credentials
  3. $hostname = 'localhost';
  4. $username = 'root';
  5. $password = '';
  6. $databaseName = 'C3459845';
  7. //Open the database connection - exit with error message otherwise
  8. $connection = mysqli_connect($hostname, $username, $password, $databaseName) or exit("Unable to connect to database!");
  9. ?>
  10. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  11. <?php
  12. //Make connection to database
  13. include 'connection.php';
  14. //(a)Gather from $_POST[]all the data submitted and store in variables
  15.  
  16. if(isset($_POST['custdetailsSubmit'])){
  17. $firstName = $_POST['txtfname'];
  18. $surName = $_POST['txtsname'];
  19. $email = $_POST['txtemail'];
  20. $passwd = $_POST['passtxt'];
  21. $gender = $_POST['gen'];
  22. $age = $_POST['age'];
  23. }
  24. else{
  25. //DO NOTHING
  26. }
  27.  
  28. //(b)Construct INSERT query using variables holding data gathered
  29.  
  30. $query = "INSERT INTO Customer (FirstName, LastName, Email,
  31. Password, Gender, Age) VALUES ('$firstName', '$surName', '$email', '$passwd', '$gender', '$age')";
  32.  
  33. mysqli_query($connection, $query);
  34.  
  35. if ($connection->query($sql) === TRUE) {
  36. echo "New record created successfully";
  37. } else {
  38. echo "Error: " . $sql . "<br>" . $conn->error;
  39. }
  40.  
  41. $conn->close();
  42.  
  43. ?>
  44. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45. <?php
  46. //Make connection to database
  47. include 'connection.php';
  48.  
  49. //Display heading
  50. echo '<h2>Select ALL from the Customer Table</h2>';
  51.  
  52. //run query to select all records from customer table
  53. $query="SELECT * FROM Customer";
  54.  
  55. //store the result of the query in a variable called $result
  56.  
  57. $result=mysqli_query($connection, $query);
  58.  
  59. //Use a while loop to iterate through your $result array and display
  60. //the first name, last name and email for each record
  61.  
  62. while ($row=mysqli_fetch_assoc($result)){
  63. echo $row['FirstName'].' '.$row['LastName'].' '.$row['Email'].'<br />';
  64. }
  65. echo '<h2>Select ALL from the Customer Table with Age > 22</h2>';
  66.  
  67. $query = "SELECT * FROM Customer WHERE Age > 22";
  68. $result = mysqli_query($connection, $query);
  69.  
  70. while ($row=mysqli_fetch_assoc($result)){
  71. echo $row['FirstName'].' '.$row['LastName'].' '.$row['Email'].' '.$row['Age']. '<br />';
  72.  
  73. }
  74.  
  75. echo '<h2>Select ALL from the Customer Table with Age >= 22</h2>';
  76.  
  77. $query = "SELECT * FROM Customer WHERE Age >= 22 AND Gender = 'M'";
  78. $result = mysqli_query($connection, $query);
  79.  
  80. while ($row=mysqli_fetch_assoc($result)){
  81. echo $row['FirstName'].' '.$row['LastName'].' '.$row['Email'].' '.$row['Age']. '<br />';
  82.  
  83. }
  84.  
  85. echo '<h2>Select Males from the Customer Table - List by Age descending</h2>';
  86.  
  87. $query = "SELECT * FROM Customer WHERE Gender = 'M' GROUP BY Age DESC";
  88. $result = mysqli_query($connection, $query);
  89.  
  90. while ($row=mysqli_fetch_assoc($result)){
  91. echo $row['FirstName'].' '.$row['LastName'].' '.$row['Email'].' '.$row['Age']. '<br />';
  92.  
  93. }
  94.  
  95. echo '<h2>Select ALL from the Customer Table with "a" in First Name > 22</h2>';
  96.  
  97. $query = "SELECT * FROM Customer WHERE FirstName LIKE '%a%'";
  98. $result = mysqli_query($connection, $query);
  99.  
  100. while ($row=mysqli_fetch_assoc($result)){
  101. echo $row['FirstName'].' '.$row['LastName'].' '.$row['Email'].' '.$row['Age']. '<br />';
  102.  
  103. }
  104. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement