php - 连接到 mySQL 时出错:SQLSTATE[HY093]: Invalid parameter number
问题描述
我已经搜索了多个线程,但在我的情况下,我看不出问题出在哪里。
它在第 30 行显示错误: $findU->bindParam(':uid', $uid);
。不确定这是否是这里的真正问题。
完整代码:
<?php
try {
$pdo = new \PDO('mysql:host=localhost;dbname=project;charset=utf8', 'root', '', [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
if($_POST && isset($_POST['submit'])) {
$first = trim($_POST['first']);
$last = trim($_POST['last']);
$email = trim($_POST['email']);
$uid = trim($_POST['uid']);
$pwd = trim($_POST['pwd']);
//checks if there's an empty field
if(empty($first) || empty($last) || empty($email) || empty ($uid) || empty ($pwd)) {
header('Location: sign_up.php?signup=empty');
exit();
} else {
//checks inputs for invalid symbols through Regex
if(!preg_match("/^[a-zA-Z]/", $first) || !preg_match("/^[a-zA-Z]/", $last)) {
header('Location: sign_up.php?signup=invalid');
exit();
} else {
//checks if the email is in valid format
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header('Location: sign_up.php?signup=invalidemail');
exit();
} else {
//checks if the username is already in use
$findU = $pdo->prepare("SELECT * FROM `users` WHERE user_uid = ':uid'");
$findU->bindParam(':uid', $uid);
$result = $findU->execute();
if($result->fetchColumn() > 0) {
header("Location: sign_up.php?signup=usertaken");
exit();
} else {
//creating hash for password
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
//inserts new users into DB
$newUser = $pdo->prepare("INSERT INTO `users` (user_first, user_last, user_email,
user_uid, user_pwd) VALUES (':first', ':last', ':email', ':uid', ':hashedPwd')");
$newUser->execute(array(':first' => $first, ':last' => $last, ':email' => $email,
':uid' => $uid, ':hashedPwd' => $hashedPwd));
header("Location: sign_up.php?signup=success");
exit();
}
}
}
}
}
} catch(\PDOException $e) {
echo "Error connecting to mySQL: " . $e->getMessage();
echo "<code><pre>".print_r($e)."</pre></code>";
exit();
}
?>
解决方案
推荐阅读
- framemaker - “过滤器遇到错误,无法完成翻译”是什么意思?
- reactjs - 视频无法在 React 中的移动设备上播放
- android - 如何使用云函数在 FCM 中正确指定 channel_id
- reactjs - Electron React 应用程序白屏 + 在构建中断开了开发工具但在开发中很好?
- spring-data-jpa - JPA 映射 OneToMany 与部分嵌入的 id 孩子
- python - PyInstaller 未处理的异常
- web-scraping - r.html.find 仅获得 1 项而不是全部
- loops - 无限循环VS。溢出的循环?
- python-3.x - 将avi转换为mp4时出现ffmpeg查找错误
- python - 为什么仅在运行 pytest 时主源文件中出现导入错误?