PHP 新手 想问一个站内搜索的问题
我刚学PHP 想做一个很简单站内搜索。 页面是这样的:
--------------------------
Pestcide_trade_name: (一个下来菜单)
Company name: (一个下拉菜单)
Active ingredient:(一个下拉菜单)
Application:(一个textarea)
Enviroment:(一个textarea)
-------------------------------------(上面的在数据库的一个表里insecticide, 下面的在表crop里)
Crops: (一个下拉菜单)
REI:(一个下拉菜单)
Note: (一个textarea)
Search button
------------------------------
Pestcide_trade_name和ID 是主键。
数据库一共两张表, 上面5个在表insecticide里,下面三个在crop里。
我想做到的就是 Pestcide_trade_name和crop是必须选的,没选择要给提示要求选择。 其他的条件可选可不选,然后结果就是把所有符合选中条件的结果显示出来放在一个表格里打出来。
我写的search.php:
----------------------------------------------------------------------
if (isset($_POST['TN'])&&!empty($_POST['TN'])&&isset($_POST['Crops'])&&!empty($_POST['Crops'])){
$searchp = "select ID, Pestcide_trade_name, Crops from insecticide, crop where insecticide.Pestcide_trade_name='".$_POST['TN']."' and crop.Crops='".$_POST['Crops']."'and insecticide.ID =crop.ID ";
if(isset($_POST['CN'])&&!empty($_POST['CN']))
$searchp.="and insecticide.Company_name='".$_POST['CN']."'";
if(isset($_POST['AI'])&&!empty($_POST['AI']))
$searchp.="and insecticide.Active_ingredient='".$_POST['AI']."'";
if(isset($_POST['PPE'])&&!empty($_POST['PPE']))
$searchp.="and insecticide.PPE='".$_POST['PPE']."'";
if(isset($_POST['REI'])&&!empty($_POST['REI']))
$searchp.="and crop.REI='".$_POST['REI']."'";//and crop.Pestcide_trade_name='".$_POST['TN']."' and crop. ID = insecticide.ID";
$resultp=mysql_query($searchp);
echo"<table class='table1' style= 'table-layout:fixed; width:900px; '><tr><td style= 'width:50px;'>ID</td><td style= 'width:80px;'>Company Name</td><td style= 'width:80px;'>Trade name</td><td style= 'width:80px;'>Active Ingredient</td><td style= 'width:50px;'>PPE</td><td>Applicators Must Wear</td><td>Mixers/loaders Must Wear</td><td>Environmental Hazards</td><td>CROPS</td><td>REI</td></tr>";
($rs=mysql_fetch_object($resultp)){
echo '<tr><td >'.$rs->ID.'</td><td>'.$rs->Company_name.'</td><td>'.$rs->Pestcide_trade_name.'</td><td>'.$rs->Active_ingredient.'</td><td width:50px>'.$rs->PPE.'</td>';
echo '<td style="word-break: keep-all;"><div style="word-break:break-all;word-wrap:break-word;"> <pre>'.$rs->AMW.'</pre></div></td><td style="word-break: keep-all;width:300px"><div style="word-break:break-all;word-wrap:break-word"> <pre>'.$rs->MMW.'</pre></div></td><td style="word-break: keep-all;"><div style="word-break:break-all;word-wrap:break-word"> <pre>'.$rs->EH.'</pre></div></td><td style="word-break: keep-all;"><div style="word-break:break-all;word-wrap:break-word;"> <pre>'.$rs->Crops.'</pre></div></td><td style="word-break: keep-all;"><div style="word-break:break-all;word-wrap:break-word;"> <pre>'.$rs->REI.'</pre></div></td></tr>';
}
echo'</table>';
--------------------------------------------------------------------------
代码总报错Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in d:\apm_setup\htdocs\search.php on line 67
哪位能帮我看看我写要怎么改。 万分感谢啊!!!!!!!!!!!!
[解决办法]
$resultp=mysql_query($searchp) or die(mysql_error()); //报什么错
[解决办法]
[解决办法]
最后看了看你的sql
可能出在这里的问题
select insecticide.ID, 省略中间 and insecticide.ID =crop.ID
新手不懂php噢、
[解决办法]
亲, sql语句这里insecticide.ID =crop.ID 有问题把? 如果你的insecticide的ID要跟crop的ID匹配, 这条语句应该要使用表连接吧? 具体请查看left join等表连接语句的用法, 估计你的问题就是出现在这里