我的选择查询有点问题。如果我输入一串数字(int),并且具有该“用户名”的用户存在,我会得到一个名为$resultw
的结果。但是,如果我在表单中输入一个字母(varchar)的字符串,并且用户在我的数据库中有这个用户名,那么它不起作用,我也没有收到任何(错误)消息。会发生什么?pdo选择查询不适用于字母串
我的PHP:
if(isset($_POST["submit"])){
$hostname='localhost';
$user='root';
$password='';
$uinput = $_POST['username'];
try {
$dbh = new PDO("mysql:host=$hostname;dbname=game",$user,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "SELECT id, email, username
FROM user
WHERE username = $uinput"; //
if ($resi = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$resultw = $resi->fetchAll();
}
if($resultw >= 0) {
//do something
}
else {
echo "The user you are searching for does not exist.\nPlease check your input.";
}
}
catch(PDOException $e) {
//
}
}
不知道它需要的,但 这就是我的形式:
<form style="width:100%" data-ajax="false" action="./username.php" method="post">
<div style="width:100%" class="ui-grid-a">
<div style="width:75%;padding-left:5%" class="ui-block-a">
<div>
<input data-inline="true" type="text" name="username" id="username"/></div></div>
<div style="width:20%" class="ui-block-b"><div>
<input data-inline="true" type="submit" name="submit" value="Go" class="ui-btn"/>
</div></div>
</div>
</form>
我的foreach循环:
<?php foreach ($resultw as $keyres => $rowres): ?>
<li>
<a href="ime.php"><?php echo $rowres['username']; ?></a>
</li>
<?php endforeach; ?>
你很容易受到[sql注入攻击](http://bobby-tables.com)的攻击,并且在你的查询中你的'$ uinput'周围没有引号,这意味着你有一个语法错误。但是,由于您已启用例外,因此您应该**得到错误。你的错误是'username = 123'和'username = foo'之间的差异。一个是字段到整数的比较,另一个是字段到字段的比较,而且你几乎肯定没有一个名为'foo'的字段。 –