我试图将用户输入关键字插入搜索的结果网址中。 我通过将method = post更改为method = get来实现它,但它会中断搜索并且我没有得到任何结果。我尝试过更改各种内容,但似乎没有任何效果,因此我已恢复原来正在运行的代码(减去网址中的关键字)
搜索后,网址为:/imagesearch?go
我希望成为:/imagesearch?kewords+keyword or similar.
谢谢你能指出我正确的方向。
这是我的原始代码:
<div align="center">
<p>Search by keyword..</p>
<form method="post" action="imagesearch?go" id="search-form">
<input type="text" name="q">
<input type="submit" name="sa" value="Search">
</form> </div>
if(isset($_POST['sa'])){
if(isset($_GET['go'])){
if(preg_match("/^[ a-zA-Z]+/", $_POST['q'])){
$name=$_POST['q'];
$sql="SELECT ID FROM Image WHERE Name LIKE '%" . $name . "%' Order by Saleable Desc LIMIT 50";
//-run the query against the mysql query function
$result=mysql_query($sql);
$content .= ' <p><div id="wrapper">';
// Loop
while($row=mysql_fetch_array($result)){
$id=$row['ID'];
$img = new Image($id);
答案 0 :(得分:1)
$name=$_POST['q'];
$sql="SELECT ID FROM Image WHERE Name LIKE '%" . $name . "%' Order by Saleable Desc LIMIT 50";
您只能从POST变量获得q
。这应该如何处理GET请求?
此外,永远不会直接在SQL查询中使用用户输入。想象一下q == "'; DROP TABLE Image;"
! Whoever has been teaching you website development should have told you before even starting explaining how you issue SQL queries.
答案 1 :(得分:0)
关于你想要的,这应该有所帮助。
<html>
<form method="get">
<input type="text" name="q">
<button type="submit">Search</button>
</form>
</html>
<?php
if (isset($_GET['q'])) {
echo "<p>" . $_GET['q'] . "</p>";
}
?>