2016-06-21 79 views
-1

我得到這個錯誤:Uncaught exception 'PDOException'在我的PHP代碼。PDO註冊表錯誤

這裏是我的代碼:

<?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:\xampp\htdocs\simplereg\register.php:13 Stack trace: #0 C:\xampp\htdocs\simplereg\register.php(13): PDO->prepare('SELECT COUNT(us...') #1 {main} thrown in C:\xampp\htdocs\simplereg\register.php on line 13. 

我希望得到任何幫助。

+0

你的代碼不整潔,你應該更好地使用Control + K –

+0

正確的代碼表示代碼來顯示你的代碼更加美觀和整齊,爲更清晰的解釋添加了一些更多的註釋。 – theblindprophet

回答

0

這裏你說的表名是user

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

但在這裏你說的表名是users

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

我猜其中之一就是不正確。

0

如錯誤提示,"Base table or view not found: 1146 Table 'user.user' doesn't exist".您正在使用錯誤的表名稱。修理它。

從你的代碼示例中,我猜表名將是users