php - 无效查询:'where子句'中的某个未知列

时间:2017-06-03 08:07:51

标签: php mysql

我正在尝试为已登录的用户创建更新功能,但是我遇到了这种错误:

无效查询:'where子句'中的未知列'johnny'

有人可以帮我通知问题吗?

<?php
$result = mysql_query("select * from admin where username_admin =".$_SESSION['username_admin']);

if($result) { 
while($row = mysql_fetch_array($result)) {
$_SESSION["username_admin"] = $row['id_admin'];                             

    ?>
   <li>   <a tabindex="-1" href="edit-user.php?id=<?php echo $row['id_admin']; ?>">Edit Account</a>
   </li>

        <?php
        }
        }
        else {
        echo 'invalid query: '.mysql_error(). "\n";

        }

        ?>

1 个答案:

答案 0 :(得分:0)

要将字符串发送到sql,必须用单引号括起来:

<?php
$result = mysql_query("select * from admin where `username_admin` ='".$_SESSION['username_admin']."'");

if($result) { 
    while($row = mysql_fetch_array($result)) {
        $_SESSION["username_admin"] = $row['id_admin'];                             

        ?>
        <li>   <a tabindex="-1" href="edit-user.php?id=<?php echo $row['id_admin']; ?>">Edit Account</a>
        </li>

        <?php
    }
}
else {
    echo 'invalid query: '.mysql_error(). "\n";

}

?>