PHP和MySql登录错误

时间:2013-06-02 09:47:50

标签: php html 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 @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@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 个答案:

答案 0 :(得分:0)

这是我用来连接和检查,更改som代码以适合您的编码,随意复制它。

<?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";
}
?>