Php 无法添加到本地数据库,无法运行查询:SQLSTATE[HY093]:无效参数编号:未定义参数

Php 无法添加到本地数据库,无法运行查询:SQLSTATE[HY093]:无效参数编号:未定义参数,php,mysql,Php,Mysql,我正在尝试连接到本地主机,但不断出现以下错误: 无法运行查询:SQLSTATE[HY093]:无效的参数编号: 未定义参数 在将我的详细信息添加到注册表表单并将其发布到index.php后,我出现了此错误 我看不出我的代码有任何错误,就我所知,参数都是正确的,但显然不是 所需的行为是正确执行查询 INDEX.php require("config.inc.php"); //if posted data is not empty if (!empty($_POST)) { if (em

我正在尝试连接到本地主机,但不断出现以下错误:

无法运行查询:SQLSTATE[HY093]:无效的参数编号: 未定义参数

在将我的详细信息添加到注册表表单并将其发布到index.php后,我出现了此错误 我看不出我的代码有任何错误,就我所知,参数都是正确的,但显然不是

所需的行为是正确执行查询

INDEX.php

require("config.inc.php");

//if posted data is not empty
if (!empty($_POST)) {

    if (empty($_POST['username']) || empty($_POST['password'])) {



        $response["success"] = 0;
        $response["message"] = "Please Enter Both a Username and Password.";


        die(json_encode($response));
    }

    $query        = " SELECT 1 FROM users WHERE username = :username";
    $query_params = array(
        ':user' => $_POST['username']
    );

    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        die("Failed to run query: " . $ex->getMessage());

        $response["success"] = 0;
        $response["message"] = "Database Error. Please Try Again!";
        die(json_encode($response));
    }


    $row = $stmt->fetch();
    if ($row) {
        die("This username is already in use");
        $response["success"] = 0;
        $response["message"] = "I'm sorry, this username is already in use";
        die(json_encode($response));
    }

    $query = "INSERT INTO users ( username, password ) VALUES ( :username, :password ) ";

    $query_params = array(
        ':username' => $_POST['username'],
        ':password' => $_POST['password']
    );

    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        die("Failed to run query: " . $ex->getMessage());
        $response["success"] = 0;
        $response["message"] = "Database Error. Please Try Again!";
        die(json_encode($response));
    }

    $response["success"] = 1;
    $response["message"] = "Username Successfully Added!";
    echo json_encode($response);


} else {
?>
    <h1>Register</h1> 
    <form action="index.php" method="post"> 
        Username:<br /> 
        <input type="text" name="username" value="" /> 
        <br /><br /> 
        Password:<br /> 
        <input type="password" name="password" value="" /> 
        <br /><br /> 
        <input type="submit" value="Register New User" /> 
    </form>
    <?php
}
那些台词是错误的

$query        = " SELECT 1 FROM users WHERE username = :username";
$query_params = array(
    ':user' => $_POST['username']
);

:user不匹配:username

您介意说出所涉及的页面并提供您的表架构吗?现在可以重新打开了,我为他编辑了它。可能会链接此链接以供将来参考OP:Hey,如果可以,我会接受此答案,非常感谢!够愚蠢的了,这是我以前遇到的问题,我忘了我必须改变它。我丢失了更新的版本,所以我使用的是旧版本。