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 1.6 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. // 1. Create a database connection
  3. // 1. Create a database connection
  4. $connection = mysqli_connect('localhost', 'root', 'dakota02', 'UMUC');
  5. //Once we call the mysqli_connect function, the value that it returns is what we
  6. //have assigned to the variable $connection. It is what is referred to as a handle for the connection.
  7. //will see if connected, if not will quit and display error messages with error.
  8. //test if connection occured.
  9. if(mysqli_connect_errno()){
  10. die("Database connection failed: " .
  11. mysqli_connect_error() .
  12. "(" . mysqli_connect_errno() . ")"
  13. );
  14. }
  15. ?>
  16. <?php
  17. if (isset($_POST['submit'])){
  18. //assign post data to variables
  19. $first_name = $_POST["first_name"];
  20. $last_name = $_POST ["last_name"];
  21. $student_id = $_POST["student_id"];
  22. }
  23. $stmt = $connection->prepare("INSERT INTO students (first_name, last_name, student_id) VALUES (?, ?, ?)");
  24. $stmt->bind_param('sss', $_POST['first_name'], $_POST['last_name'], $_POST['student_id']);
  25. $stmt->execute();
  26. $stmt->close();
  27. //test if there was a query error
  28. if($stmt){
  29. //success
  30. echo "Success! Student added to database!";
  31. }else{
  32. //failure
  33. die("Database query failed. " . mysqli_error($connection));
  34. }
  35. ?>
  36. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  37. "http://www.w3.org/TR/html4/loose.dtd">
  38. <html lang="en">
  39. <head>
  40. <title>SQL Injection</title>
  41. </head>
  42. <body>
  43. </body>
  44. </html>
  45. <?php
  46. //5.close database connection
  47. mysqli_close($connection);
  48. ?>

No Description

Contributors (1)