Advertisement
Guest User

APEX

a guest
Dec 13th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. <?php
  2.  
  3. $servername = "localhost";
  4.  
  5. //these should be changed to the values set up when building the lamp server
  6. //the username will be 'root' unless you changed it
  7. //the password will be whatever you chose
  8. $username = "username";
  9. $password = "password";
  10. //this is the name of the database
  11. $dbname = "db";
  12.  
  13. // Create conn
  14. $conn = new mysqli($servername, $username, $password);
  15. if ($conn->connect_error) {
  16. die("Connection failed: " . $conn->connect_error);
  17. }
  18.  
  19. // Create database
  20. $sql = "CREATE DATABASE " . $dbname;
  21. if ($conn->query($sql) === TRUE) {
  22. } else {
  23. echo "<script>console.log('error creating db, probably exists')</script>";
  24. }
  25.  
  26. $conn->close();
  27.  
  28.  
  29. //create table
  30. $conn = new mysqli($servername, $username, $password, $dbname);
  31.  
  32. if ($conn->connect_error) {
  33. die("Connection failed: " . $conn->connect_error);
  34. }
  35.  
  36. $sqltable = "CREATE TABLE users (name VARCHAR(100) NOT NULL, id VARCHAR(50), info VARCHAR(50), dob VARCHAR(50) )";
  37.  
  38. if ($conn->query($sqltable) === TRUE) {
  39. } else {
  40. echo "<script>console.log('error creating table, probably exists')</script>";
  41. }
  42.  
  43.  
  44.  
  45. $conn->close();
  46.  
  47. function test_input($data) {
  48. $data = trim($data);
  49. $data = stripslashes($data);
  50. $data = htmlspecialchars($data);
  51. return $data;
  52. }
  53.  
  54. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  55. $name = test_input($_POST["name"]);
  56. $id = test_input($_POST["id"]);
  57. $info = test_input($_POST["info"]);
  58. $dob = test_input($_POST["dob"]);
  59.  
  60. //insert from form
  61. $conninsert = new mysqli($servername, $username, $password, $dbname);
  62.  
  63. if ($conninsert->connect_error) {
  64. die("Connection failed: " . $conninsert->connect_error);
  65. }
  66.  
  67. $sqlinsert = "INSERT INTO users (name, id, info, dob) VALUES ('" . $name . "', '" . $id . "', '" . $info . "', '" . $dob ."')";
  68.  
  69. if ($conninsert->query($sqlinsert) === TRUE) {
  70. echo "New record created successfully";
  71. } else {
  72. echo "Error: " . $sql . "<br>" . $conninsert->error;
  73. }
  74.  
  75. $conninsert->close();
  76.  
  77.  
  78. }
  79.  
  80.  
  81. ?>
  82.  
  83. <h2>db info</h2>
  84. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  85. Name: <input type="text" name="name">
  86. <br><br>
  87.  
  88. id <input type="text" name="id">
  89. <br><br>
  90.  
  91. info <input type="text" name="info">
  92. <br><br>
  93.  
  94. dob <input type="text" name="dob">
  95. <br><br>
  96.  
  97. <input type="submit" name="submit" value="Submit">
  98. </form>
  99.  
  100. <?php
  101.  
  102.  
  103. //select rows and display them
  104. $conn = new mysqli($servername, $username, $password, $dbname);
  105.  
  106. if ($conn->connect_error) {
  107. die("Connection failed: " . $conn->connect_error);
  108. }
  109.  
  110. $sql = "SELECT name, id, info, dob FROM users";
  111. $result = $conn->query($sql);
  112.  
  113. if ($result->num_rows > 0) {
  114. while($row = $result->fetch_assoc()) {
  115. echo "name: " . $row["name"]. " - id: " . $row["id"]. " - info: " . $row["info"]. " - dob: " . $row["dob"]."<br>";
  116. }
  117. } else {
  118. echo "0 results";
  119. }
  120. $conn->close();
  121.  
  122. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement