You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

提交HTML表单时出现Error 405错误的技术求助

问题

提交HTML表单时触发Error 405错误,表单action属性指向login.php,请求方法为POST,使用本地MySQL数据库。相关代码如下:

HTML表单代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="login.css">
    <title>Login</title>
</head>
<body>
    <div class="pane">
        <div class="LeftPane">
            <img id="TrafficImage"src="./Images/TrafficJPG.jpg">
        </div>
        <div class="RightPane">
            <h2>Sign In</h2>
            <form action="login.php" method="post">
                <input type="text" id="Name" name="Name" placeholder="Full name :" required><br>
                <input type="text" id="Address" name="Address" placeholder="Address :" required><br>
                <input type="text" id="UserName" name="UserName" placeholder="Username :"required><br>
                <input type="email" id="Email" name="Email" placeholder="email :" required><br>
                <input type="password" id="Password" name="Passsword" placeholder="Passsword:"  required><br>
                <input type="password" id="RePassword" name="RePasssword" placeholder="Re-Passsword :" required><br>
                <input type="submit" value="Submit">
            </form>
            
        </div>
    </div>

</body>
</html>

PHP处理代码

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["Name"];
    $address = $_POST["Address"];
    $username = $_POST["UserName"];
    $email = $_POST["Email"];
    $password = $_POST["Password"];
    $repassword = $_POST["RePassword"];

    if (empty($name) || empty($address) || empty($username) || empty($email) || empty($password) || empty($repassword)) {
        echo "All fields are required.";
        die();
    }

    if ($password != $repassword) {
        echo "The passwords do not match.";
        die();
    }

    $host = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbName = "trafiic_light";

    // Create a connection to the database
    $conn = new mysqli($host, $dbUsername, $dbPassword, $dbName);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Prepare the statement
    $stmt = $conn->prepare("SELECT email FROM login WHERE email = ? LIMIT 1");
    $stmt->bind_param("s", $email);

    // Execute the statement
    $stmt->execute();

    // Bind the results
    $stmt->bind_result($email);

    // Check if the user exists
    $stmt->fetch();
    if ($email) {
        echo "The user already exists.";
    } else {
        // Insert the user into the database
        $stmt = $conn->prepare("INSERT INTO login (name,address,username,email,password,repassword) VALUES (?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("ssssss", $name, $address, $username, $email, $password, $repassword);
        $stmt->execute();

        echo "You are successfully registered.";
    }

    // Close the statement
    $stmt->close();

    // Close the connection
    $conn->close();
}
?>

错误原因排查与解决办法

Error 405表示请求方法不被目标资源允许,结合场景整理排查方向和解决方式:

1. 文件路径或大小写问题

  • 检查login.php是否和HTML表单文件在同一目录,路径不一致则修正action属性(比如./backend/login.php)。
  • 若服务器为Linux系统,文件名区分大小写,确保表单里的login.php和实际文件名完全一致(比如不要写成Login.php)。

2. 服务器配置限制POST请求

  • Apache环境:检查.htaccess文件是否限制了POST方法,可临时移除该文件测试;同时确认Apache已启用mod_php或mod_fcgid模块,确保PHP文件能被正确处理。
  • Nginx环境:检查站点配置文件,确保location ~ \.php$块中通过fastcgi_pass正确转发请求,且未添加limit_except GET之类限制POST的规则。

3. 代码自身问题修复(附带功能修正)

即使解决了405,你的代码还有几处逻辑错误,需同步修复:

  • 表单密码字段的name为Passsword(多一个s),但PHP中取的是$_POST["Password"],会导致密码值为空,修正表单代码:
    <input type="password" id="Password" name="Password" placeholder="Password:"  required><br>
    <input type="password" id="RePassword" name="RePassword" placeholder="Re-Password :" required><br>
    
  • PHP查询用户时,绑定结果到$email会覆盖原输入的邮箱变量,导致逻辑判断错误,重命名结果变量:
    $stmt->bind_result($existing_email);
    $stmt->fetch();
    if ($existing_email) {
        echo "The user already exists.";
    }
    

4. 服务器未正确解析PHP文件

如果服务器未配置PHP解析,会直接返回PHP源码,也可能触发405。检查服务器是否安装PHP,且.php文件的MIME类型已正确配置。

内容的提问来源于stack exchange,提问作者Anjana Dep

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 19:27:48