gpt4 book ai didi

php - MYSQL 搜索 - PHP 表单

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

<div class="searchbox">
<form action="Search.php" method="get">
<fieldset>
<input id="$search" placeholder="Search for a Product" type="text" />
<input id="submit" type="hidden" />
</fieldset>
</form>
</div>
<div id="content">
<ul>
<?php

$host = "xxx";
$user = "xxx";
$pass = "xxx";
$db = "xxx";

mysql_connect($host, $user, $pass);
mysql_select_db($db);
unset($host,$user,$pass,$db);


//$query = "SELECT produkt FROM checklist WHERE id IS "1";
$query = "SELECT produkt FROM checklist WHERE id LIKE '%$search%'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{

echo "<li><span class='name'><b>$row[produkt]</b><br /></span><span class='comment'><i>Tierische Stoffe: </i>$row[tierisch]</span><i> Alkohol: </i>$row[alkohol]</span></span></li>";
}
?>

我的搜索框似乎有问题。目前,我只看到数据库中的所有项目,并且我想查看我正在搜索的项目。

就这样吗?

<div class="searchbox">
<form action="Search.php" method="get">
<fieldset>
<input id="search" placeholder="Search for a Product" type="text" />
<input id="submit" type="hidden" />
</fieldset>
</form>
</div>
<div id="content">
<ul>
<?php

$host = "xxx";
$user = "xxx";
$pass = "xxx";
$db = "xxx";

mysql_connect($host, $user, $pass);
mysql_select_db($db);
unset($host,$user,$pass,$db);

$search = $_POST['search'];

$query = "SELECT produkt FROM checklist WHERE id LIKE '%".$_POST['search']."%'";
//$query = "SELECT produkt FROM checklist WHERE id LIKE '%$search%'";
$result = mysql_query($query);

最佳答案

<input id="$search" placeholder

这里需要$吗?它应该变成:

<input id="search" placeholder

要从搜索查询内部访问 GET 变量,请使用 $_GET 数组,如下所示:

$query = "SELECT produkt FROM checklist WHERE id LIKE '%{$_GET['search']}%'";  

但为了安全起见并避免 mysql 注入(inject),您可能需要这样做:

$search = mysql_real_escape_string($_GET['search']);
$query = "SELECT produkt FROM checklist WHERE id LIKE '%{$search}%'";

编辑:忘记输入中的名称属性:

<input name="search" id="search" placeholder="Search for a Product" type="text" />

然后在你的 php 中

$search = mysql_real_escape_string($_GET['search']);
$query = "SELECT produkt FROM checklist WHERE id LIKE '%{$search}%'";

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
echo "<li><span class='name'><b>{$row['produkt']}</b></span></li>";
}

关于php - MYSQL 搜索 - PHP 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6913710/

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