我正在尝试连接到本地主机但仍然收到错误:
无法运行查询:SQLSTATE [HY093]:参数号无效: 参数未定义
我将详细信息添加到REGISTER表单并将其发布到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
}
答案 0 :(得分:2)
这些行错了
$query = " SELECT 1 FROM users WHERE username = :username";
$query_params = array(
':user' => $_POST['username']
);
:user
与:username