2017-10-10 116 views
-2

我遇到了我的Php文檔的問題。該項目是做一個工作登錄/註冊頁面 錯誤MESSAGE-Php未知數據庫?

Warning: mysqli_connect(): (HY000/1049): Unknown database 'registration' in 
C:\xampp\htdocs\registration\server.php on line 11 

拼寫正確的文件。此外,它就不記得註冊(我認爲這連接到 「未知數據庫」 的問題?)

server.php代碼 -

<?php 
 
\t session_start(); 
 

 
\t // variable declaration 
 
\t $username = ""; 
 
\t $email = ""; 
 
\t $errors = array(); 
 
\t $_SESSION['success'] = ""; 
 

 
\t // connect to database 
 
\t $db = mysqli_connect('localhost', 'root', '', 'registration'); 
 

 
\t // REGISTER USER 
 
\t if (isset($_POST['reg_user'])) { 
 
\t \t // receive all input values from the form 
 
\t \t $username = mysqli_real_escape_string($db, $_POST['username']); 
 
\t \t $email = mysqli_real_escape_string($db, $_POST['email']); 
 
\t \t $password_1 = mysqli_real_escape_string($db, $_POST['password_1']); 
 
\t \t $password_2 = mysqli_real_escape_string($db, $_POST['password_2']); 
 

 
\t \t // form validation: ensure that the form is correctly filled 
 
\t \t if (empty($username)) { array_push($errors, "Username is required"); } 
 
\t \t if (empty($email)) { array_push($errors, "Email is required"); } 
 
\t \t if (empty($password_1)) { array_push($errors, "Password is required"); } 
 

 
\t \t if ($password_1 != $password_2) { 
 
\t \t \t array_push($errors, "The two passwords do not match"); 
 
\t \t } 
 

 
\t \t // register user if there are no errors in the form 
 
\t \t if (count($errors) == 0) { 
 
\t \t \t $password = md5($password_1);//encrypt the password before saving in the database 
 
\t \t \t $query = "INSERT INTO users (username, email, password) 
 
\t \t \t \t \t VALUES('$username', '$email', '$password')"; 
 
\t \t \t mysqli_query($db, $query); 
 

 
\t \t \t $_SESSION['username'] = $username; 
 
\t \t \t $_SESSION['success'] = "You are now logged in"; 
 
\t \t \t header('location: index.php'); 
 
\t \t } 
 

 
\t } 
 

 
\t // ... 
 

 
\t // LOGIN USER 
 
\t if (isset($_POST['login_user'])) { 
 
\t \t $username = mysqli_real_escape_string($db, $_POST['username']); 
 
\t \t $password = mysqli_real_escape_string($db, $_POST['password']); 
 

 
\t \t if (empty($username)) { 
 
\t \t \t array_push($errors, "Username is required"); 
 
\t \t } 
 
\t \t if (empty($password)) { 
 
\t \t \t array_push($errors, "Password is required"); 
 
\t \t } 
 

 
\t \t if (count($errors) == 0) { 
 
\t \t \t $password = md5($password); 
 
\t \t \t $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; 
 
\t \t \t $results = mysqli_query($db, $query); 
 

 
\t \t \t if (mysqli_num_rows($results) == 1) { 
 
\t \t \t \t $_SESSION['username'] = $username; 
 
\t \t \t \t $_SESSION['success'] = "You are now logged in"; 
 
\t \t \t \t header('location: index.php'); 
 
\t \t \t }else { 
 
\t \t \t \t array_push($errors, "Wrong username/password combination"); 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 

 
?>

文件樹

xampp -
htdocs -
registration -
errors.php,index.php,login.php,register.php,server.php,style.css

+2

那麼,**是**那裏有一個名爲'registration'的數據庫? – ceejayoz

回答

0

您可能還沒有創建名爲「registration」的數據庫與「users」表?正如你在本地主機上,你可能有PHPMyAdmin,它可以幫助你做到這一點。

+0

啊,謝謝。現在我懂了! – RedstoneMaina