我想按名称搜索城市和州,并想在该表中搜索插入城市和州ID的

时间:2019-06-10 12:07:15

标签: php mysql

我想通过搜索查询显示主表中的全部数据,并按名称搜索城市和州,并在主表中输入城市和州ID,而不是名称。

核心php API中的phpmyadmin mysql数据库。


if(!empty($state_id))
{
$condition .="sangh_directory.state like '$state_id%'";

}   
if(!empty($city_id))
{
$condition .="and sangh_directory.city like '$city_id%'";
}
if(!empty($searchsangh))
{
$condition .="sangh_directory.sangh_name like '$searchsangh%'";
}
$DirectoryDetailsQuery=$con->select_query("sangh_directory","*","inner join city on city.city_id=sangh_directory.city inner join country ON country.country_id=sangh_directory.country inner join state ON state.state_id=sangh_directory.state where $condition");

选择查询表名称为:sangh_directory时出现错误您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的'and sangh_directory.city之类的'Ahme%'附近使用

1 个答案:

答案 0 :(得分:0)

尝试一下:

$condition = "1=1";

if(!empty($state_id)) {
    $condition .=" and sangh_directory.state like '$state_id%'";
}   
if(!empty($city_id)) {
    $condition .=" and sangh_directory.city like '$city_id%'";
}
if(!empty($searchsangh)) {
    $condition .=" and sangh_directory.sangh_name like '$searchsangh%'";
}
$DirectoryDetailsQuery=$con->select_query("sangh_directory","*","inner join city on city.city_id=sangh_directory.city inner join country ON country.country_id=sangh_directory.country inner join state ON state.state_id=sangh_directory.state where $condition");

很抱歉,如果这在语法上不正确,希望您能理解要点。

如果您要建立一个公开的网站,请观看this video或阅读有关SQL注入的其他内容,如@Dharman所建议的那样。

并阅读有关确保网站安全的其他主题。