PDO注册表错误

  • 本文关键字:错误 注册表 PDO php
  • 更新时间 :
  • 英文 :


我收到此错误:Uncaught exception 'PDOException'在我的php代码中。

这是我的代码:

<?php
session_start();
require 'connect.php';
if(isset($_POST['register'])){
$username = !empty($_POST['username']) ? trim($_POST['username']) : null;
$pass = !empty($_POST['password']) ? trim($_POST['password']) : null;
//Construct the SQL statement and prepare it.
$sql = "SELECT COUNT(username) AS num FROM user  WHERE username = :username";
$stmt = $pdo->prepare($sql);
//Bind the provided username to our prepared statement.                   $stmt->bindValue(':username', $username);
//Execute.
$stmt->execute();
//Fetch the row.
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($row['num'] > 0){
die('That username already exists!');
}
//Prepare our INSERT statement.
//Remember: We are inserting a new row into our users table.
$sql = "INSERT INTO users (username, password) VALUES (:username, :password)";
$stmt = $pdo->prepare($sql);
//Bind our variables.
$stmt->bindValue(':username', $username);
$stmt->bindValue(':password', $passwordHash);
//Execute the statement and insert the new account.
$result = $stmt->execute();
//If the signup process is successful.
if($result){
//What you do here is up to you!
echo 'Thank you for registering with our website.';
}
}
?>

这是完整的错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'user.user' doesn't exist' in C:xampphtdocssimpleregregister.php:13 Stack trace: #0 C:xampphtdocssimpleregregister.php(13): PDO->prepare('SELECT COUNT(us...') #1 {main} thrown in C:xampphtdocssimpleregregister.php on line 13.

我将不胜感激任何帮助。

这里你说表名是user

$sql = "SELECT COUNT(username) AS num FROM user  WHERE username = :username";

但这里你说表名是users

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

我猜其中一个是不正确的。

正如错误所暗示的那样,"Base table or view not found: 1146 Table 'user.user' doesn't exist".您使用了错误的表名。修复它。

从您的代码示例中,我想表名将users.

最新更新