在 PHP 中创建搜索表单来搜索数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12747650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a search form in PHP to search a database?
提问by LiamHorizon
I am currently trying to complete a project where the specifications are to use a search form to search through a packaging database. The database has lots of variables ranging from Sizes, names, types and meats. I need to create a search form where users can search using a number of different searches (such as searching for a lid tray that is 50 cm long).
我目前正在尝试完成一个项目,其中规范是使用搜索表单来搜索包装数据库。该数据库有许多变量,包括尺寸、名称、类型和肉类。我需要创建一个搜索表单,用户可以在其中使用多种不同的搜索进行搜索(例如搜索 50 厘米长的盖子托盘)。
I have spent all day trying to create some PHP code that can search for info within a test database I created. I have had numerous amounts of errors ranging from mysql_fetch_array errors, boolean errors and now currently my latest error is that my table doesn't seem to exist. Although i can enter data into it (html and php pages where I can enter data), I don't know what is causing this and I have started again a few times now.
我一整天都在尝试创建一些 PHP 代码,这些代码可以在我创建的测试数据库中搜索信息。我有很多错误,从 mysql_fetch_array 错误到布尔错误,现在我的最新错误是我的表似乎不存在。虽然我可以在其中输入数据(我可以在其中输入数据的 html 和 php 页面),但我不知道是什么导致了这种情况,我现在又开始了几次。
Can anyone give me some idea or tips of what I am going to have to do currently? Here is just my small tests at the moment before I move onto the actual sites SQL database.
任何人都可以给我一些关于我目前必须要做的事情的想法或提示吗?在我进入实际站点 SQL 数据库之前,这只是我目前的小测试。
Creation of database:
创建数据库:
<body>
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE db_test", $con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysql_select_db("db_test", $con);
$sql = "CREATE TABLE Liam
(
Code varchar (30),
Description varchar (30),
Category varchar (30),
CutSize varchar (30),
)";
mysql_query($sql, $con);
mysql_close($con);
?>
</body>
HTML search form page:
HTML 搜索表单页面:
<body>
<form action="form.php" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
The PHP code I am using to attempt to gather info from the database(I have rewritten this a few times, this code also displays the "table.liam doesn't exist")
我用来尝试从数据库收集信息的 PHP 代码(我已经重写了几次,此代码还显示“table.liam 不存在”)
<body>
<?php
$con = mysql_connect ("localhost", "root", "");
mysql_select_db ("db_test", $con);
if (!$con)
{
die ("Could not connect: " . mysql_error());
}
$sql = mysql_query("SELECT * FROM Liam WHERE Description LIKE '%term%'") or die
(mysql_error());
while ($row = mysql_fetch_array($sql)){
echo 'Primary key: ' .$row['PRIMARYKEY'];
echo '<br /> Code: ' .$row['Code'];
echo '<br /> Description: '.$row['Description'];
echo '<br /> Category: '.$row['Category'];
echo '<br /> Cut Size: '.$row['CutSize'];
}
mysql_close($con)
?>
</body>
If anyone has any insight or can help me with this I would be very grateful! Thanks in advance.
如果有人有任何见解或可以帮助我解决这个问题,我将不胜感激!提前致谢。
采纳答案by rackemup420
try this out let me know what happens.
试试这个让我知道会发生什么。
Form:
形式:
<form action="form.php" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
Form.php:
表单.php:
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM liam WHERE Description LIKE '%".$term."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo 'Primary key: ' .$row['PRIMARYKEY'];
echo '<br /> Code: ' .$row['Code'];
echo '<br /> Description: '.$row['Description'];
echo '<br /> Category: '.$row['Category'];
echo '<br /> Cut Size: '.$row['CutSize'];
}
Edit: Cleaned it up a little more.
编辑:再清理一下。
Final Cut (my test file):
Final Cut(我的测试文件):
<?php
$db_hostname = 'localhost';
$db_username = 'demo';
$db_password = 'demo';
$db_database = 'demo';
// Database Connection String
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_database, $con);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM liam WHERE Description LIKE '%".$term."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo 'Primary key: ' .$row['PRIMARYKEY'];
echo '<br /> Code: ' .$row['Code'];
echo '<br /> Description: '.$row['Description'];
echo '<br /> Category: '.$row['Category'];
echo '<br /> Cut Size: '.$row['CutSize'];
}
}
?>
</body>
</html>
回答by pjmorse
You're getting errors 'table liam does not exist' because the table's name is Liamwhich is not the same as liam. MySQL table names are case sensitive.
您收到错误“表 liam 不存在”,因为表的名称Liam与liam. MySQL 表名区分大小写。
回答by David Ferenczy Rogo?an
Are you sure, that specified database and table exists? Did you try to look at your database using any database client? For example command-line MySQL client bundled with MySQL server. Or if you a developer newbie, there are dozens of a GUI and web interface clients (HeidiSQL, MySQL Workbench, phpMyAdmin and many more). So first check, if your table creation script was successful and had created what it have to.
您确定指定的数据库和表存在吗?您是否尝试使用任何数据库客户端查看您的数据库?例如命令行 MySQL 客户端与 MySQL 服务器捆绑在一起。或者,如果您是开发新手,则有许多 GUI 和 Web 界面客户端(HeidiSQL、MySQL Workbench、phpMyAdmin 等等)。因此,首先检查您的表创建脚本是否成功并创建了它必须创建的内容。
BTW why do you have a script for creating the database structure? It's usualy a nonrecurring operation, so write the script to do this is unneeded. It's useful only in case of need of repeatedly creating and manipulating the database structure on the fly.
顺便说一句,为什么你有一个用于创建数据库结构的脚本?它通常是一个不重复的操作,因此不需要编写脚本来执行此操作。它仅在需要重复创建和动态操作数据库结构的情况下才有用。

