Php未知数据库?
问题描述:
我遇到了我的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
答
您可能还没有创建名为“registration”的数据库与“users”表?正如你在本地主机上,你可能有PHPMyAdmin,它可以帮助你做到这一点。
+0
啊,谢谢。现在我懂了! – RedstoneMaina
那么,**是**那里有一个名为'registration'的数据库? – ceejayoz