You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SQL_Injection.php 2.0 kB

2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. // 1. Create a database connection
  3. $dbhost = "localhost";
  4. $dbuser = "root";
  5. $dbpass = "dakota02";
  6. $dbname = "UMUC";
  7. $connection = mysqli_connect($dbhost, $dbuser, $dbpass,
  8. $dbname);
  9. //you could just place the variables directly into the arugments also, but
  10. //it's easier to understand this way.
  11. //Once we call the mysqli_connect function, the value that it returns is what we
  12. //have assigned to the variable $connection. It is what is referred to as a handle for the connection.
  13. //will see if connected, if not will quit and display error messages with error.
  14. //test if connection occured.
  15. if(mysqli_connect_errno()){
  16. die("Database connection failed: " .
  17. mysqli_connect_error() .
  18. "(" . mysqli_connect_errno() . ")"
  19. );
  20. }
  21. ?>
  22. <?php
  23. if (isset($_POST['submit'])){
  24. //assign post data to variables
  25. $first_name = $_POST["first_name"];
  26. $last_name = $_POST["last_name"];
  27. $student_id = $_POST["student_id"];
  28. }
  29. //2. perform database query
  30. //This way is called assembling a query. Easier to read and work with.
  31. //could also wrap parts in if statements. ex. if something is true,
  32. //append the WHERE clause.
  33. $query = "INSERT INTO students (";
  34. $query .= " first_name, last_name, student_id";
  35. $query .= ") VALUES (";
  36. $query .= " '{$first_name}', '{$last_name}', '{$student_id}' ";
  37. $query .= ")";
  38. $result = mysqli_query($connection, $query);
  39. //test if there was a query error
  40. if($result){
  41. //success
  42. //could do a redirect. ex redirect to("somepage.php");
  43. echo "Success! Student added to database!";
  44. }else{
  45. //failure
  46. //$message = "Student creation failed";
  47. die("Database query failed. " . mysqli_error($connection));
  48. }
  49. ?>
  50. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  51. "http://www.w3.org/TR/html4/loose.dtd">
  52. <html lang="en">
  53. <head>
  54. <title>SQL Injection</title>
  55. </head>
  56. <body>
  57. </body>
  58. </html>
  59. <?php
  60. //5.close database connection
  61. mysqli_close($connection);
  62. ?>

No Description

Contributors (1)