我有三个 if 语句查询 3 个表,并且每个表都有一个表中的外键,但是系统不采用另一个表(id)的外键而不是使值 0 的问题
此代码背后的想法是用户输入 gov 名称并单击添加,然后系统插入 gov 名称......如果用户输入 dist 名称而不输入 gov 名称,则系统显示错误消息,否则系统插入两者值(但在省 ID 字段的 dist 表中,系统输入 0 而不是省 ID 的正确 ID)所以当我尝试在第三个中输入 $dist 时,系统显示Undefined variable: dist in。
那么如何解决这个错误?
代码:
if(isset($_POST['add']))
{
if(isset($_POST['gov']) || isset( $_POST['dist']) || isset($_POST['city']) || isset( $_POST['lat']) || isset($_POST['long']) == "" )
{
$errorMSG = "you must fill one of these fields befor you submit!!";
}
if($_POST['gov'])
{
$gov = $_POST['gov'];
$sql = mysql_query("INSERT INTO governorate (governorate_id, governorate_name)VALUES('', '$gov')")or die(mysql_error());
echo $gov;
}
//******for adding district*********************//
if($_POST['dist'])
{
$dist = $_POST['dist'];
$gov = $_POST['gov'];
if($_POST['gov'] !=="")
{
$sql = mysql_query("INSERT INTO districts (district_id, district_name, governorate_id)VALUES('', '$dist', '$gov')")or die(mysql_error());
echo $dist;
}
else{ $errorMSG = "You can not add District Without relate a Governorate for this district";}
}
//********************for adding city****************************//
if($_POST['city'])
{
$city = $_POST['city'];
$lat = $_POST['lat'];
$long = $_POST['long'];
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";}
else
{
$sql = mysql_query("INSERT INTO village (id, village_name, district_id, lattitude, longitude)VALUES('', '$city', '$dist' ,'$lat', '$long')")or die(mysql_error());
echo $city;
}
}
}