2012-09-06 45 views
1

我有一个网络论坛,只允许一些选定的用户访问它,即他们必须有$_SEESION['username']。现在MySQL查询不会取得正确的结果

的资料页上 - 它看起来像这样

http://127.0.0.1/mysite/profile.php?username=john 

我想显示或者上载按钮或编辑概况按钮, 取决于他们是否有图像上传。

我检查,看看是否$ _GET设置,首先那么如果不是重定向到 主页:

if(isset($_GET['username'])) { 
$username = mysql_real_escape_string($_GET['username']); 

$profile = mysql_query(SELECT * 
         FROM forum 
         WHERE username = '$username') or DIE(mysql_error()); 

         $row = mysql_fetch_array($profile); 

        echo" 
        Name : $row[name]; 
        Country : $row[country]; 

    if($_GET['username'] == $_SESSION['username'] && $row['imagepath'] == '') { 
    echo "Upload Button"; 
} 
else { 
     echo"Edit Button"; 
} 
} 

上传按钮显示在屏幕上的时候,居然有一个图像 路径指定。

请在哪里我错了。

+2

不要使用'mysql_ *'功能在你的代码。这些功能不再维护,并[已被弃用](http://news.php.net/php.internals/53799)。相反,您应该使用[MySQLi](http://php.net/mysqli)或[PDO](http://php.net/pdo)。不知道使用哪个? [这篇文章](http://php.net/manual/en/mysqlinfo.api.choosing.php)应该有所帮助。 –

+1

我喜欢你如何让脚本DIE以大写字母。除此之外,关于如何回答这个问题的信息并不多。马上回复atcha:*“你要去哪里?”* –

+1

要使用$ _SESSION,你需要设置session_start(); – jtheman

回答

1

你应该在你的mysql_query参数中加双引号。我复制了你的代码,那就是我的解决方案。

你回声太多了。它应该如下面的代码。

echo "Name: ".$row['name']; 
echo "Country: ".$row['country']; 

if($_GET['username'] == $_SESSION['username'] && $row['imagepath'] == '') { 
    echo "Upload Button"; 
} 
else { 
    echo"Edit Button"; 
} 
0
  • mysql_query("QUERY HERE"),要不然也不会在所有的工作。
  • $row['name']$row['country']:您需要在引号之间加上名称。
  • echo 'Text'。您没有关闭引号。
  • empty($row['imagepath'])。有一个功能,所以使用它。

如果上述更正仍然不工作,尝试回声-ING您需要的所有变量:$_GET['username']$_POST['username']$row['imagepath']。也许PHP不能读取它们。

-1

固定从这里,你可以开始调试所有的语法错误:

if (isset($_GET['username'])) { 
    $username = mysql_real_escape_string($_GET['username']); 

    $profile = mysql_query("SELECT * 
    FROM forum 
    WHERE username = '$username'") or DIE(mysql_error()); 

    $row = mysql_fetch_array($profile); 

    echo "Name : " . $row['name']; 
    echo "Country : ". $row['country']; 

    if ($_GET['username'] == $_SESSION['username'] && $row['imagepath'] == '') { 
    echo "Upload Button"; 
    } 
    else { 
    echo"Edit Button"; 
    } 
} 
+0

为什么要投票?努力清理代码..非常好的人! – JvdBerg

+1

虽然这不是一个真正的答案。 – desimusxvii