2012-08-22 42 views
0

我有一个下拉框,其中包含名称和搜索字段的列表。字符串%0D%0A导致SQL中的比较问题?

下拉列表中填充了数据库中的名称列表,搜索字段允许您执行通配符搜索。

目前通配符搜索按预期工作,但从下拉列表中选择名称不起作用。

我相信,因为我看到了下面的浏览器我的地址栏已经从下拉列表中选择一个名称,点击搜索按钮,这可能是因为一些不想要的字符的可能:

http://localhost:81/connect/players/?name=%0D%0A3&text=&action=search 

我想,我的代码如下所示上面(0D%0A%)的文本导致问题:

if (isset($_GET['action']) and $_GET['action'] == 'search') 
{ 
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php'; 

    $id = $_GET['name']; // name slightly confusing but does return the id 
    $text = $_GET['text']; 

try 
{ 

$sql = "SELECT id, name, age FROM player 
    WHERE player.id = '$id' 
    OR player.name LIKE '%$text%' 
    GROUP BY player.id"; 

$s = $pdo->query($sql); 

} 
catch (PDOException $e) 
{ 
    $error = 'Error fetching names.' . $e->getMessage();; 
    include 'error.html.php'; 
    exit(); 
} 
// This is responsible for populating the new player info underneath all 
foreach ($s as $row) 
{ 
    $names[] = array('id' => $row['id'], 'name' => $row['name'], 'age' => $row['age']); 
} 
include 'searchprofiles.html.php'; 
exit(); 

}

,我相信这是防止它FR om将数据库中的id与存储在变量$ id中的id进行比较。

但是我也只是从地址栏中手动去除了%0D%0A,它仍然不起作用,所以可能会有另一个问题呢?

还应该注意的是,如果从下拉列表中没有选择任何值并且没有输入通配符,那么将返回所有行。

HTML如下:

SEARCHPROFILES.HTML.PHP

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?> 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="utf-8"> 
<title>Manage Jokes: Search Results</title> 
</head> 

<body> 

<h1>Search Results</h1> 

<?php if (isset($names)): ?> 

<table> 
<tr><th>Name</th><th>Options</th></tr> 
<?php foreach ($names as $name): ?> 
<tr> 
<td><?php htmlout($name['name']); ?></td> 
<td><?php htmlout($name['age']); ?></td> 
<td> 
<form action="?" method="post"> 
<div> 
<input type="" name="id" value="<?php 
htmlout($name['id']); ?>"> 
<input type="submit" name="action" value="Edit"> 
<input type="submit" name="action" value="Delete"> 
</div> 
</form> 

</td> 
</tr> 
<?php endforeach; ?> 
</table> 
<?php endif; ?> 
<p><a href="?">New search</a></p> 
<p><a href="..">Return to JMS home</a></p> 
</body> 
</html> 

下面是对于其中值相加的形式HTML。

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?> 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="utf-8"> 
<title>Manage Profiles</title> 
</head> 

<body> 

<h1>Manage Profile</h1> 

<p><a href="?add">Add new profile</a></p> 

<form action="" method="get"> 

<p>View player profiles satisfying the following criteria:</p> 

<div> 

<label for="name">By name:</label> 

<select name="name" id="name"> 

<option value="">Any name</option> 

<!-- populates the drop down with names --> 
<?php foreach ($names as $name): ?> 
<option value=" 
<?php htmlout($name['id']); ?>"> 
<?php htmlout($name['name']); ?> 
</option> 
<?php endforeach; ?> 

</select> 

</div> 

<div> 
<label for="text">Containing text:</label> 
<input type="text" name="text" id="text"> 
</div> 

<div> 
<input type="hidden" name="action" value="search"> 
<input type="submit" value="Search"> 
</div> 

</form> 

</body> 
</html> 

任何帮助,非常感谢。

感谢

+0

告诉你的html文件 –

+0

现在已经添加了此, 感谢您的快速回复。 – Johnny

+2

在'

回答

0

的原因是你在你的表单控件有换行符:

变化

<option value=" 
<?php htmlout($name['id']); ?>"> 
<?php htmlout($name['name']); ?> 
</option> 

<option value="<?php htmlout($name['id']); ?>"> 
    <?php htmlout($name['name']); ?> 
</option> 
+0

嗨真相,我已经删除了上面的评论,现在字符串看起来更像我所期望的: http:// localhost:81/connect/players /?name = 1&text =&action = search 但是,功能仍然不起作用。所有行都会从下拉列表中选择一个名称并单击搜索,而不仅仅是我希望返回的那一行。 – Johnny

+0

Hmmmmmmmm我可以从运行cmd(SELECT * FROM player WHERE name =“John Smith”或名称LIKE'%%';)中的db中运行该查询来看到所有行都返回,因此显然存在LIKE'% %”。有任何想法吗? – Johnny

+0

@Johnny:LIKE'%%'的意思是“一切”,所以显然当你选择约翰史密斯或一切时,你会得到一切。你需要AND。 –