我是PHP,HTML5和CSS的新手。我正在建立我的网站。 This是地址。当我点击 START FREE TRIAL 时,它意味着在数据库上注册用户并让他们访问网站上的其他页面。从那时起他们就登录了。问题是,当点击 START FREE TRIAL 时,用户可以访问其他页面,但我无法在数据库字段/列中看到注册通过Cpanel中的phpMyAdmin。据我所知,server.php
(下面)连接到数据库服务器,这就是为什么要授予其他页面访问权限。
尝试登录时,框架内会出现错误对话框。下面的代码应该有效吗?或者我做错了什么?希望我已正确发布server.php
。
<?php
session_start();
// variable declaration
$fname = "";
$lname = "";
$username = "";
$email = "";
$password = "";
$errors = array();
$_SESSION['success'] = "";
$dbh = mysqli_connect ("localhost", "usrname", "password")
or die ('I cannot connect to the database.');
mysqli_select_db ("db_name");
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$fname = mysqli_real_escape_string($dbh, $_POST['fname']);
$lname = mysqli_real_escape_string($dbh, $_POST['lname']);
$username = mysqli_real_escape_string($dbh, $_POST['username']);
$email = mysqli_real_escape_string($dbh, $_POST['email']);
$password_1 = mysqli_real_escape_string($dbh, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($dbh, $_POST['password_2']);
// form validation: ensure that the form is correctly filled
if (empty($fname)) { array_push($errors, "First Name is required"); }
if (empty($lname)) { array_push($errors, "Last Name is required"); }
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$query = "INSERT INTO users (fname, lname, username, email, password)
VALUES('$fname','$lname', '$username', '$email', '$password')";
mysqli_query($dbh, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: Continue.php');
}
}
// ...
// LOGIN USER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($dbh, $_POST['username']);
$password = mysqli_real_escape_string($dbh, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($dbh, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: Continue.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
答案 0 :(得分:0)
感谢和WillParky93和Fred -ii指出了我正确的方向。这已通过更改此
解决$dbh = mysqli_connect ("localhost", "usrname", "password")
or die ('I cannot connect to the database.');
mysqli_select_db ("db_name");
到
$dbh = mysqli_connect ("localhost", "usrname", "password")
or die ('I cannot connect to the database.');
mysqli_select_db ($dbh, "db_name");