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.3 kB

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

No Description

Contributors (1)