我的代码中使用了if
个参数,但代码的逻辑不起作用,因为我想要的是当用户输入城市名而不输入区名时系统必须显示错误消息
当用户输入城市名称和地区名称而不输入lat和long时,系统必须显示错误消息。
我的代码中的含义是,即使用户进入城市并且拉长了系统显示错误消息,通知用户输入lat和long
if($_POST['city'])
{
$city = $_POST['city'];
$lat = $_POST['lat'];
$long = $_POST['long'];
if($_POST['dist'] =="")
{
$errorMSG = "you can not add city without having relation with district";
}
if($lat || $long ==""){ $errorMSG = "You can not add village Without its coordination";}
else
{
$sql = mysql_query("INSERT INTO village (id, village_name, district_id, lattitude, longitude)VALUES('', '$city', , '$lat', '$long')")or die(mysql_error());
echo $city;
}
}
答案 0 :(得分:3)
if($lat || $long =="")
应为if($lat=="" || $long =="")
答案 1 :(得分:2)
更改: -
if($lat || $long =="")
到
if($lat =="" || $long =="")
说明: -
你在做的是检查$ lat是否为空或$ long为空。了解运营商的优先级。 ==具有比||
更高的优先级答案 2 :(得分:2)
您的代码容易受到SQL注入 并且存在一些逻辑错误。
使用PDO的正确代码将是
if($_POST['city'])
{
$errorMSG = '';
$city = $_POST['city'];
$lat = $_POST['lat'];
$long = $_POST['long'];
$dist = $_POST['dist']
if(!$dist)
{
$errorMSG .= "you can not add city without having relation with district";
}
if(!$lat || !$long)
{
$errorMSG .= "You can not add village Without its coordination";
}
if (!$errorMSG)
{
$sql = "INSERT INTO village VALUES(NULL, ?, ?, ?, ?)";
$pdo->prepare($sql);
$pdo->execute(array($city,$dist,$lat,$long));
}
}