2013-06-02 133 views
-3

我是一个新手,我的代码有点麻烦。当我运行它说,“无效的登录信息。请返回到上一页。数据库未选中。”但通过我的代码我有。PHP和MySql登录错误

<?php 

// my MySQL database login information 
$host = "localhost"; // my host 
$user = "****Can't tell you due to security reasons"; // my username 
$pass = "****Can't tell you due to security reasons"; // my password 
$db = "Login"; // The database name that i will be connecting to 

// Connecting to the MySQL database 
mysql_connect($host, $user, $pass); 
mysql_select_db($db); 

// Checking to see if the login form has been submitted 
// Protect myself from mysql injection 
if (isset($_POST['username'])) { 
    $username = mysql_real_escape_string($_POST['username']); 
    $password = mysql_real_escape_string($_POST['password']); 
    // Query to check to see if the username and password supplied match the database records 
    $sql = "SELECT * FROM login_table WHERE username='".$username."' AND password='".$password."' LIMIT 1"; 
    $res = mysql_query($sql); 
    // If login information is correct 
    if (mysql_num_rows($res) == 1) { 
     echo "You have successfully logged in."; 
     exit(); 
    } 
    // If login information is invalid 
    else { 
     echo "Invalid login information. Please return to the previous page.".mysql_error(); 
     exit(); 
    } 
} 

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Login</title> 
</head> 

<body> 
<h1>Login</h1> 
<form method="post" action="login.php"> 
Username: <input type="text" name="username" /><br /><br /> 
Password: <input type="password" name="password" /><br /><br /> 
<input type="submit" name="submit" value="Log In" /> 
</form> 
</body> 
</html> 

MySQL的代码(我不认为这是错误):

-- phpMyAdmin SQL Dump 
-- version 3.5.8.1deb1 
-- http://www.phpmyadmin.net 
-- 
-- Host: localhost 
-- Generation Time: Jun 02, 2013 at 10:37 AM 
-- Server version: 5.5.31-0ubuntu0.13.04.1 
-- PHP Version: 5.4.9-4ubuntu2 

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; 
SET time_zone = "+00:00"; 


/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; 
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; 
/*!40101 SET @[email protected]@COLLATION_CONNECTION */; 
/*!40101 SET NAMES utf8 */; 

-- 
-- Database: `Login` 
-- 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `login_table` 
-- 

CREATE TABLE IF NOT EXISTS `login_table` (
    `Id` int(11) NOT NULL AUTO_INCREMENT, 
    `username` varchar(65) NOT NULL, 
    `password` varchar(65) NOT NULL, 
    PRIMARY KEY (`Id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; 
+1

您的用户名或通是不正确的 – user4035

+0

不,他们是正确的 – audittxl

+0

请向我们提供错误代码 – Gimmy

回答

0

这是我用来连接和检查,改变索姆代码以适应您的编码,随意复制。

<?php 
session_start(); 
require('connect.php'); 

$username = mysql_real_escape_string($_POST['username']); 
$password = mysql_real_escape_string($_POST['password']); 

if (isset($username) && isset($password)) 
{ 
    $query = mysql_query("SELECT * FROM login WHERE username='$username' AND password='$password'"); 
    $result = mysql_num_rows($query); 

    if($result > 0) 
    { 
     header("Location: members.php"); 
     $_SESSION['username'] = $_POST['username']; 
    } 
    else 
    { 
     echo "Password is incorrect. Try again."; 
    } 
} 
else 
{ 
    echo "You have to enter your username and password. Try again"; 
} 
?> 
+0

谢谢require()是什么意思? – audittxl

+0

这意味着它从connect.php中获取连接信息....它与MySQL相关:) – 2013-06-02 23:08:09