2017-04-23 149 views
0

php代碼無法正常工作?

"$query = ("SELECT * FROM `accounts` WHERE username = '$username'")or die(mysql_error());" 

如果我不加accountsid然後"$_SESSION['userid'] = $row['id'];"不會工作,但如果我不加它,然後登錄不會工作也accounts是數據庫和id是它的內部。

<?php 
 
session_start(); 
 
if(isset($_SESSION['users']) != ""){ 
 
echo '<script type="text/javascript">','index();','</script>'; 
 
} 
 

 
require '../php/dbConnect.php'; 
 
    $username = $_POST['username']; 
 
\t $password = $_POST['password']; 
 
    $query = ("SELECT * FROM `accounts` WHERE username = '$username'")or die(mysql_error()); 
 
    $response = mysql_query($query); 
 

 
\t $row = mysql_fetch_array($response); 
 

 
    if($row['password'] == md5($password)) 
 
    { 
 
     $_SESSION['online'] = true; 
 
     $_SESSION['users'] = $username; 
 
     $_SESSION['userid'] = $row['id']; 
 
     echo '<script type="text/javascript">','redirect();','</script>'; 
 
    } 
 
    else{ 
 
     echo("Wrong Credentials"); 
 
\t } 
 
?> 
 

 

 
<div id="friend-request-title" class="overlay round-edge panel-left"> 
 
\t <label class="w3-text-white "><h2><b>Friend Requests</b></h2></label> 
 
</div> 
 
<div id="friend-request-panel" class="overlay round-edge panel-up"> 
 
\t <?php 
 
\t require 'php/dbConnect.php'; 
 
\t  $query = ("SELECT * FROM `accounts` WHERE `id` <> '".$_SESSION['userid']."'"); 
 
\t  $response = mysql_query($query); 
 

 
\t  while($row = mysql_fetch_assoc($response)) { 
 
\t  echo ' 
 
\t  <div class="lesson-section"> 
 
\t  \t \t <div class="container"> 
 
\t  \t \t \t <img id="profile-image" src="img/profile2.png" class="big-circle float" style="margin: 4% 0 0 0;"> 
 
\t  \t \t </div> 
 
\t  \t \t <label id="" class="w3-text-white"><h4><b>You have a new friend request.</b></h4></label> 
 
\t  \t \t <label id="" class="w3-text-white">Friend request from '. $row['username'] .'.</label> 
 
\t  \t \t <br> 
 
\t  \t \t <button id="" class="w3-btn w3-text-white light-overlay border-remove round-edge" style="margin: 2% 2% 0 0;" type="button"><b>Accept</b></button> 
 
\t  \t \t <button id="" class="w3-btn w3-text-white light-overlay border-remove round-edge" style="margin: 2% 0 0 0;" type="button"><b>Decline</b></button> 
 
\t  \t </div> 
 
\t  \t <hr> 
 
\t  '; 
 
\t  } 
 
\t ?>

+0

請檢查[這] (http://stackoverflow.com/help/how-to-ask) – getl0st

+0

Md5。真的嗎? – Akintunde007

+0

對不起我是一個noob〜 –

回答

0

您有錯誤在你的PHP的這一部分:

if(isset($_SESSION['users']) != ""){ 
echo '<script type="text/javascript">','index();','</script>'; 
} 

您應該使用isset再檢查,如果不爲空

if(isset($_SESSION['users']) && $_SESSION['users'] != ""){ 
    echo '<script type="text/javascript">','index();','</script>'; 
} 
+1

爲什麼不'echo'';'? – FrankerZ

+0

我不喜歡改變什麼是沒有錯的:) –