MySQL-Error:where子句中的未知列

时间:2014-01-13 21:53:34

标签: php mysql

<div class="ui-widget">

<form method="POST">
<?php

if(isset($_POST['search'])){

$connection = mysql_connect('localhost', 'root', '1234');
mysql_select_db('hoppers');

    $query = "SELECT user_name FROM users WHERE {$_POST['username']} = user_name";
    $result = mysql_query($query) or die ("MySQL-Error: " . mysql_error());
    $row = mysql_fetch_array($result);


if($row = TRUE){

        $present = $row['user_name'];
        echo $present;

     }else{
            echo "This User does not exist!";
        }
}
?>

<label for="tags">Username: </label>
<input type="text" id="tags" name = "username">
<input type="submit" name="search" value="Suchen!">
</form>
</div>

所以我总是得到这个错误消息: MySQL-Error:'where子句'中的未知列'dompol171'。
因此,它将我输入的用户名与“user_name”列本身进行比较。我一直吓坏了......因为我不知道如何以不同的方式比较它!

6 个答案:

答案 0 :(得分:0)

尝试使用:

 $query = "SELECT user_name FROM users WHERE user_name = '{$_POST['username']}'";

答案 1 :(得分:0)

查询需要稍微调整,来自:

$query = "SELECT user_name FROM users WHERE {$_POST['username']} = user_name";

为:

$query = "SELECT user_name FROM users WHERE user_name = '{$_POST['username']}'";

用单引号包装值。

此更新的PHP将验证传回的数据:

<?php
if(isset($_POST['search'])){

$connection = mysql_connect('localhost', 'root', '1234');
mysql_select_db('hoppers');

$query = "SELECT user_name FROM users WHERE user_name = '{$_POST['username']}'";
$result = mysql_query($query) or die ("MySQL-Error: " . mysql_error());

// count # of rows returned by DB query
$num_users = mysql_num_rows( $result );

// If more than 0
if ( $num_users > 0 ){
    // Success!
    $row = mysql_fetch_array($result);
    var_dump( $row );
} else {
    // No results returned
    echo "This User does not exist!";
}


}

答案 2 :(得分:0)

你在where子句中犯了一个错误,它是:

WHERE user_name = {$_POST['username']}";

语法为:Where "column" = "value"

答案 3 :(得分:0)

你的参数倒退了。列名始终在值之前:

$query = "SELECT user_name FROM users WHERE user_name = {$_POST['username']}";

这不是PHP中的比较运算符,其中操作数可以位于等式的任何一侧。

答案 4 :(得分:0)

这里发生的事情是$_POST['username']的值被认为是一个列,因为你没有引用它,即将它设置为字符串。所以说用户名是musa,它试图将列musa与列user_name进行比较(并且该列不存在)。它应该是

 $query = "SELECT user_name FROM users WHERE 'some username' = user_name";

答案 5 :(得分:0)

尝试使用此:

$username = $_POST['username'];
$query = "SELECT user_name FROM users WHERE user_name='" . $username . "'";