Hello everyone! I recently started learning PHP and wanted to create a simple form where user information could be stored in a database using phpMyAdmin. However, when I pressed the “Submit” button, I encountered an HTTP 405 Error. If you’re facing a similar issue, don’t worry I’ll walk you through the problem and how I resolved it.
Understanding the Error
The HTTP 405 Error means “Method Not Allowed.” This typically occurs when the server (in this case, your localhost) receives a request using an HTTP method that it doesn’t support for the requested resource. In my case, the issue was related to how the form data was being submitted and processed.
The Code
Here’s the HTML form and PHP script I was working with:
HTML Form
<!DOCTYPE html> <html> <!-- Head --> <head> <title>Info Form</title> </head> <!-- Body --> <body> <h1 id="title">Info Form</h1> <form action="info.php" method="POST"> <section id="firstName"> First Name: <input type="text" placeholder="First Name" name="userFirstName" required> <br><br> </section> <section id="lastName"> Last Name: <input type="text" placeholder="Last Name" name="userLastName" required> <br><br> </section> <section id="genderChoice"> Male: <input type="radio" name="userGender" value="m" required> Female: <input type="radio" name="userGender" value="f" required> <br><br> </section> <section id="submit"> <input type="submit" value="Submit"> </section> </form> </body> </html>
PHP Script (info.php)
<?php $userFirstName = $_POST['userFirstName']; $userLastName = $_POST['userLastName']; $userGender = $_POST['userGender']; $host = "127.0.0.1"; $dpUsername = "root"; $dpPassword = ""; $dpname = "form"; $conn = new mysqli($host, $dpUsername, $dpPassword, $dpname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $INSERT = "INSERT INTO info (userFirstName, userLastName, userGender) VALUES ('$userFirstName', '$userLastName', '$userGender')"; if ($conn->query($INSERT) { echo "Data inserted successfully!"; } else { echo "Error: " . $INSERT . "<br>" . $conn->error; } $conn->close(); ?>
Identifying the Problem
The HTTP 405 Error was caused by one of the following issues:
- Incorrect Form Action or Method:
- The form’s
action
attribute points toinfo.php
, but if the file doesn’t exist or is inaccessible, the server might reject the request. - The
method
attribute is set toPOST
, which is correct, but if the server doesn’t allow POST requests for this resource, it will throw a 405 error.
- The form’s
- Database Connection Issues:
- If the database connection fails or the query is invalid, the script might not execute properly, leading to unexpected behavior.
- Improper SQL Query:
- The SQL query in the PHP script was missing single quotes around the values, which would cause a syntax error.
Fixing the Code
Ensure the PHP File Exists
Make sure the info.php
file exists in the same directory as your HTML file. If it’s in a different directory, update the action
attribute in the form tag to point to the correct path.
Fix the SQL Query
The SQL query should have single quotes around the values to avoid syntax errors. Here’s the corrected query:
$INSERT = "INSERT INTO info (userFirstName, userLastName, userGender) VALUES ('$userFirstName', '$userLastName', '$userGender')";
Debug the Database Connection
Add error handling to check if the database connection is successful:
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
Validate Form Data
Always validate and sanitize user input to prevent SQL injection and other security issues. For example:
$userFirstName = mysqli_real_escape_string($conn, $_POST['userFirstName']); $userLastName = mysqli_real_escape_string($conn, $_POST['userLastName']); $userGender = mysqli_real_escape_string($conn, $_POST['userGender']);
Final Working Code
HTML Form
<!DOCTYPE html> <html> <head> <title>Info Form</title> </head> <body> <h1 id="title">Info Form</h1> <form action="info.php" method="POST"> <section id="firstName"> First Name: <input type="text" placeholder="First Name" name="userFirstName" required> <br><br> </section> <section id="lastName"> Last Name: <input type="text" placeholder="Last Name" name="userLastName" required> <br><br> </section> <section id="genderChoice"> Male: <input type="radio" name="userGender" value="m" required> Female: <input type="radio" name="userGender" value="f" required> <br><br> </section> <section id="submit"> <input type="submit" value="Submit"> </section> </form> </body> </html>
PHP Script (info.php)
<?php $userFirstName = mysqli_real_escape_string($conn, $_POST['userFirstName']); $userLastName = mysqli_real_escape_string($conn, $_POST['userLastName']); $userGender = mysqli_real_escape_string($conn, $_POST['userGender']); $host = "127.0.0.1"; $dpUsername = "root"; $dpPassword = ""; $dpname = "form"; $conn = new mysqli($host, $dpUsername, $dpPassword, $dpname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $INSERT = "INSERT INTO info (userFirstName, userLastName, userGender) VALUES ('$userFirstName', '$userLastName', '$userGender')"; if ($conn->query($INSERT)) { echo "Data inserted successfully!"; } else { echo "Error: " . $INSERT . "<br>" . $conn->error; } $conn->close(); ?>
Conclusion
The HTTP 405 Error was resolved by ensuring the PHP file existed, fixing the SQL query, and validating the database connection. Always double-check your code for syntax errors and ensure your server is configured to handle the HTTP methods you’re using.