2014-12-07 55 views
0

我想在我的网站上创建一个简单的登录体验。这些数据来自phpmyadmin。我遇到了麻烦,并不确定我要去哪里。我希望尽可能保持现在简单,只是为了让它开始。简单登录使用会话/ SQL

HTML

<div class="container"> 
     <div class="row"> 
      <div class="col-md-8 col-md-offset-2"> 
      <div class="well well-sm"> 
       <form class="form-horizontal" action="" method="post"> 
       <fieldset> 
       <legend class="text-center">Sign In</legend> 


       <!-- Message body --> 
       <div class="form-group"> 
        <label class="col-md-4 control-label" for="Username">Username</label> 
        <div class="col-md-8"> 
        <input id="username" name="username" type="text" placeholder="Your email" class="form-control"> 
        </div> 
       </div> 

       <div class="form-group"> 
        <label class="col-md-4 control-label" for="Password">Password</label> 
        <div class="col-md-8"> 
        <input id="password" name="password" type="text" placeholder="Your email" class="form-control"> 
        </div> 
       </div> 

       <!-- Form actions --> 
       <div class="form-group"> 
        <div class="col-md-12 text-right"> 
        <button onClick="return validateForm()" type="submit" class="btn btn-primary btn-lg">Submit</button> 
       ` </div> 
       </div> 
       </fieldset> 
       </form> 
      </div> 
      </div> 
     </div> 
    </div> 

PHP

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

// Connection 
$conn = mysql_connect("localhost", "root", "MIS42520!$") or die (mysql_error()); 

//Select the database to use 
mysql_select_db ("cookie", $conn); 

// SQL query to fetch information of registerd users and finds user match. 
$sql = mysql_query("select * from login where password='$password' AND username='$username'",  $connection); 

$result = mysql_query($sql, $conn) or die(mysql_error()); 

$row = mysql_fetch_array($sql); 

if(is_array($row)) { 
$_SESSION["username"] = $row[username]; 
$_SESSION["password"] = $row[password]; 
} else { 
$message = "Invalid Username or Password!"; 
} 

if(isset($_SESSION["user_id"])) { 
header("Location:user_dashboard.php"); 
} 
+0

你得到错误? – 2014-12-07 18:01:09

+3

'mysql_ *'函数[已弃用](http://php.net/manual/en/migration55.deprecated.php),请停止使用它们。 – 2014-12-07 18:02:21

+2

你为什么认为数据是来自phpmyadmin?你的问题与phpMyAdmin完全无关。以及你的问题与你以前的问题有什么不同[从SQL中获取信息并将其放在表单中](http://stackoverflow.com/questions/26937084/get-information-out-of-sql-and-put-它功能于一形式)。此外,您应该首先修复基本错误,如'$ row [username]' – 2014-12-07 18:06:48

回答

0

我觉得这是你的问题,因为我注意到你的表格操作是同一个页面上的目标。

if(isset($_POST['username']) && isset($_POST['password'])){ 

//grab all your php code here 
} 

而且,请不要使用mysql_ *功能,因为它已被废弃