|
|
|
| У меня вопрос по поиску на сайте. Этот скрипт взят из книги PHP 5 - практика создания web-сайтов (глава 8, стр. 384-393). У меня немного другая база данных (но смысл один и тот же!).
Есть 3 поля: name, author, price.
По полю price ищет - всё правильно. А по name и author - не ищет.
А это то, что в каталогах:
CREATE TABLE `photo` (
`id_photo` int(11) NOT NULL auto_increment,
`name` tinytext NOT NULL,
`author` tinytext NOT NULL,
`year` int(4) NOT NULL default '0',
`id_catalog` int(11) NOT NULL default '0',
PRIMARY KEY (`id_photo`)
) TYPE=MyISAM;
| А это каталоги (категории):
CREATE TABLE `photocat` (
`id_catalog` int(8) NOT NULL auto_increment,
`name` tinytext NOT NULL,
`description` tinytext NOT NULL,
`pos` smallint(3) NOT NULL default '0',
`hide` enum('show','hide') NOT NULL default 'show',
`id_parent` int(8) NOT NULL default '0',
PRIMARY KEY (`id_catalog`)
) TYPE=MyISAM ;
| Сам файл называется searchform2.php. Вот мой код поиска:
<?php
require_once("../../config.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1251" />
<title>Poisk</title>
</head>
<body>
<form action=searchform2.php method=post>
<input type="hidden" name="id_parent" value="<? echo $id_parent ?>">
<table border="0" align="center"><tr valign="top"><td>
<table border="0">
<tr valign="top">
<td><p>Name</td>
<td><input class="input" size="35" type=text name=name value=<?php echo $_POST['name']; ?>></td>
</tr>
<tr valign="top">
<td><p>Author</td>
<td><input class="input" size="35" type=text name=author value=<?php echo $_POST['author']; ?>></td>
</tr>
<tr>
<td><p>Price</td>
<td><p>from <input class="input" size=6 type=text name=price_min maxlength=4 value=<?php echo $_POST['price_min']; ?>>
to <input class="input" size=6 type=text name=price_max maxlength=4 value=<?php echo $_POST['price_max']; ?>>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan=2>
<input class=button type=submit value=Find>
</td>
</tr>
</table>
<input type=hidden name=search value=search>
</form>
<?php
if(isset($_POST['search']))
{
?>
<a name=table></a>
<?php
$is_query = false;
$tmp1 = $tmp2 = $tmp3 = $tmp4="";
if(!empty($_POST['name'])) $tmp1 = " and name=".$_POST['name'];
if(!empty($_POST['author'])) $tmp2 = " and author=".$_POST['author'];
if(!empty($_POST['price_min']))
$tmp3 = " and price>".$_POST['price_min'];
if(!empty($_POST['price_max']))
$tmp4 = " and price<".$_POST['price_max'];
$query = "SELECT * FROM photo
WHERE hide='show'
".$tmp1.$tmp2.$tmp3.$tmp4."
ORDER BY pos";
$prt = mysql_query($query);
if(!$prt) puterror("Error");
if (mysql_num_rows($prt)>0)
{
?>
<table border="1" cellpadding="3" cellspacing="0" width="100%">
<tr align="center">
<td height="20">Name</td>
<td>Author</td>
<td>Price</td>
</tr>
<?
while($par = mysql_fetch_array($prt))
{
echo "<tr>
<td>".$par['name']."</td>
<td>".$par['author']."</td>
<td>".$par['price']."</td>
</tr>";
}
}
else echo "No.";
echo "</table>";
}
?>
</body>
</html>
|
Если ввожу в поле author букву: T
то выдаёт такую ошибку:
Error
Error: Unknown column 't' in 'where clause'
Как мне сделать, чтобы искалось по всем 3 полям? Большое спасибо. | |
|
|
|
|
|
|
|
для: jurij_83
(17.09.2006 в 17:00)
| | Исправьте строки
<?php
if(!empty($_POST['name'])) $tmp1 = " and name=".$_POST['name'];
if(!empty($_POST['author'])) $tmp2 = " and author=".$_POST['author'];
?>
|
следующим образом
<?php
if(!empty($_POST['name'])) $tmp1 = " and name='".$_POST['name']."'";
if(!empty($_POST['author'])) $tmp2 = " and author='".$_POST['author']."'";
?>
|
а лучше вообще так
<?php
if(!empty($_POST['name'])) $tmp1 = " and name like'%".$_POST['name']."%'";
if(!empty($_POST['author'])) $tmp2 = " and author like '%".$_POST['author']."%'";
?>
|
| |
|
|
|
|
|
|
|
для: cheops
(17.09.2006 в 17:07)
| | Всё заработало!
Я вставил:
<?php
if(!empty($_POST['name'])) $tmp1 = " and name like'%".$_POST['name']."%'";
if(!empty($_POST['author'])) $tmp2 = " and author like '%".$_POST['author']."%'";
?>
|
Супер. То, что мне и нужно было.
Большое спасибо. | |
|
|
|