Advertisement
Guest User

Untitled

a guest
May 18th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. <?php
  2.  
  3. // Let's make a connection to a database
  4.  
  5. // Database information
  6. $host = 'localhost'; // This means the database is on your computer
  7. $db_name = 'assignment'; // This is the table in your database
  8. $db_user = 'root'; // The username you log into for the database
  9. $db_pass = '1234'; // The respective password
  10.  
  11. $db = new PDO('mysql:host='.$host.';dbname='.$db_name, $db_user, $db_pass);
  12.  
  13. // Now we're connected. If we want to make sure we are, we can do:
  14.  
  15. if ($db) {
  16.  
  17. // Now we're definitely connected, so let's grab the information the user put in
  18. $username = $_POST['username'];
  19. $email = $_POST['email'];
  20. $password = $_POST['password'];
  21.  
  22. // Now we have all the fields for the user, let's insert into the database
  23.  
  24. $result = $db->query("INSERT INTO " . $db_name . " (username, email, password) VALUES ('".$username."', '".$email."', '".$password."');");
  25.  
  26. // A few things to note:
  27.  
  28. // The order is important (username, email, password) says the stuff in VALUES will be in that order
  29.  
  30. // We can check if that was successful by:
  31.  
  32. if ($result) {
  33. // Inserted correctly.
  34. } else {
  35. // Failed for some reason
  36. }
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement