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

2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. $stmt = $connection->prepare("INSERT INTO students (first_name, last_name, student_id) VALUES (?, ?, ?)");
  30. $stmt->bind_param('sss', $_POST['first_name'], $_POST['last_name'], $_POST['student_id']);
  31. $stmt->execute();
  32. $stmt->close();
  33. //test if there was a query error
  34. if($stmt){
  35. //success
  36. echo "Success! Student added to database!";
  37. }else{
  38. //failure
  39. die("Database query failed. " . mysqli_error($connection));
  40. }
  41. ?>
  42. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  43. "http://www.w3.org/TR/html4/loose.dtd">
  44. <html lang="en">
  45. <head>
  46. <title>SQL Injection</title>
  47. </head>
  48. <body>
  49. </body>
  50. </html>
  51. <?php
  52. //5.close database connection
  53. mysqli_close($connection);
  54. ?>

No Description

Contributors (1)