2014-12-07 108 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_ *功能,因爲它已被廢棄