gpt4 book ai didi

php - PDO 搜索表单损坏的链接

转载 作者:行者123 更新时间:2023-11-29 23:18:25 25 4
gpt4 key购买 nike

我构建了一个获取信息的表单,效果很好,但我遇到了一个问题,URL 最后没有改变。无法仅显示该搜索词的链接。

假设我搜索了“Google 搜索”一词

我的网址仍然

http://localhost/search.php

当我在搜索一个术语后使用 mysql 时,它看起来会像这样

http://localhost/search.php?k=google+search

我需要改变什么才能实现这一点?

搜索引擎形式:

<form name="frmSearch" method="post" action="../search.php">
<input class="inp" name="var1" type="text" id="var1">
<input class="btn" type="submit" value="Search">
</form>

搜索引擎页面:

<?php
$nameofdb = 'xxxxxx';
$dbusername = 'xxxxxx';
$dbpassword = 'xxxxxx';


// Connect to MySQL via PDO
try {
$dbh = new PDO("mysql:dbname=$nameofdb;host=xxxxxx", $dbusername, $dbpassword);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

$var1 = $_POST['var1'];


$query = "SELECT * FROM pages WHERE title LIKE :search OR keywords LIKE :search";
$stmt = $dbh->prepare($query);
$stmt->bindValue(':search', '%' . $var1 . '%', PDO::PARAM_INT);
$stmt->execute();

/* Fetch all of the remaining rows in the result set
print("Fetch all of the remaining rows in the result set:\n"); */


$result = $stmt->fetchAll();

foreach( $result as $row ) {
/* echo */ $row["title"];
/* echo */ $row["keywords"];
/* echo */ $row["photo"];
/* echo */ $row["link"];

echo "<a href=$row[link]> $row[photo] </a>";

}



if ($stmt->rowCount() > 0) {
$result = $stmt->fetchAll();

foreach( $result as $row ) {
echo $row["id"];
echo $row["title"];
}
} else {
echo 'There is nothing to show';
}

?>

最佳答案

由于您使用“POST”方法进行请求,所有表单数据都会添加到 HTTP 正文中。

尝试使用method="GET"

这里有一个简短的文档:HTML form method Attribute

关于php - PDO 搜索表单损坏的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27561949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com