gpt4 book ai didi

php - PHP 数据库搜索表单的问题

转载 作者:行者123 更新时间:2023-11-30 22:54:07 24 4
gpt4 key购买 nike

我有一个包含五个字段的数据库

ValueA
ValueB
ValueC
ValueD
ValueE

我正在尝试制作一个搜索表单,可以通过每个单独的字段进行搜索,例如,如果 ValueB 中的值为“Blue”,则从下拉列表中选择 ValueB,然后输入“Blue”以打印出所有值在 Blue 所属的行中。到目前为止,我已经创建了一个名为“findme.html”的 html 文件:

<html>

<head>
<title>Search</title>
</head>

<body bgcolor=#ffffff>

<h2>Search</h2>

<form name="search" method="post" action="findme2.php">
Search for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="ValueA">Value A</option>
<Option VALUE="ValueB">Value B</option>
<Option VALUE="ValueC">Value C</option>
<Option VALUE="ValueD">Value D</option>
<Option VALUE="ValueE">Value E</option>
</Select>

<input type="submit" name="search" value="Search" />
</form>

</body>

</html>

并且还创建了一个名为“findme2.php”的 php 文件:

<html>

<head>
<title>Searching through Database Table mytablename</title>
</head>

<body bgcolor=#ffffff>


<?php

include "config.php";

echo "<h2>Search Results:</h2><p>";

if(isset($_POST['search']))
{
$find =$_POST['find'];
}
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}

// Otherwise we connect to our Database
$username="xxxxxxxx";
$password="xxxxxxxx";
$database="xxxxxx_xxxxxxx";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");



// We perform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified
$iname = mysql_query("SELECT * FROM mytablename WHERE upper($field) LIKE '%$find%'")
or die(mysql_error());

//And we display the results
while($result = mysql_fetch_array( $iname ))
{
echo "id :" .$result['ValueA'];
echo "<br> ";
echo "name :".$result['ValueB'];
echo "<br>";
echo "name :".$result['ValueC'];
echo "<br>";
echo "name :".$result['ValueD'];
echo "<br>";
echo "name :".$result['ValueE'];
echo "<br>";
echo "<br>";
}

$anymatches = mysql_num_rows($iname);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;



?>

</body>
</html>

我相信我的问题出在查询命令上,但我不确定如何调整语法。谁能帮帮我?

最佳答案

您忘记设置 $field 变量。

在您的 if 语句中,您应该将其更改为

if(isset($_POST['search']))            
{
$find =$_POST['find'];
$field =$_POST['field'];
}

它应该可以工作了。

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

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