2017-08-17 261 views
-1

我是PHP,HTML5和CSS的全新软件。我正在建立我的网站。地址是This。当我点击开始免费试用它意味着将用户注册到数据库并让他们访问网站上的其他页面。然后他们登录。问题是,当点击开始免费试用时,用户可以访问其他页面,但我无法通过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

听起来像是你的SQL查询失败。试着在SQL之后立即杀死你的脚本,看看用mysqli_error($ dbh)抛出了什么错误。' – IsThisJavascript

+0

WillParky93谢谢你。会放弃一下。 –

+0

'mysqli_select_db(“db_name”);'失败了。阅读该手册,您将看到它的语法。 –

回答

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");