2017-04-23 148 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

我不喜欢改变什么是没有错的:) –