gpt4 book ai didi

php - 如果值不存在则在 3 个表中插入值,如果存在则更新它

转载 作者:行者123 更新时间:2023-11-30 01:35:42 28 4
gpt4 key购买 nike

我有 3 个表省区村庄,我希望能够在每个表中插入值...但我需要首先检查用户输入是否不为空,以便省字段能够插入区并检查用户输入的区字段是否不为空,以允许用户输入村庄值及其坐标..

所以我写了这段代码,但它不能按我想要的方式工作,任何人都可以帮助我???看看我在哪里犯了错误。

到目前为止,我只是编写要添加的村庄和要删除的省的代码

即使插入了值,系统也会显示错误消息:您必须在提交之前填写这些字段之一!!

代码

 if(isset($_POST['add']))
{
if(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*********************
elseif($_POST['dist'])
{
$dist = $_POST['dist'];
$gov = $_POST['gov'];
if($gov)
{
$sql = mysql_query("INSERT INTO districts (district_id, district_name, governorate_id)VALUES('', '$dist', '$gov')")or die(mysql_error());

$sql = mysql_query("INSERT INTO governorate (governorate_id, governorate_name)VALUES('', '$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 = mysql_real_escape_string( $_POST['city']);
$lat = mysql_real_escape_string($_POST['lat']);
$long = mysql_real_escape_string($_POST['long']);
$dist = mysql_real_escape_string($_POST['dist']);
$gov = mysql_real_escape_string( $_POST['gov']);
if(!$dist)
{
$errorMSG = "you can not add city without having relation with district";
}
elseif($lat =="" || $long ==""){ $errorMSG = "You can not add village Without its coordinations";}
else
{
$sqld = mysql_query("INSERT INTO districts (district_id, district_name, governorate_id)VALUES('', '$dist', '$gov')") or die(mysql_error());
$sql = mysql_query("INSERT INTO village (id, village_name, district_id, lattitude, longitude)VALUES('', '$city', '$dist' ,'$lat', '$long')")or die(mysql_error());

}
}

}
/////******************for remove****************************************************//
/////********************************************************************************//
if(isset($_POST['remove']))
{
if($_POST['gov'])
{
$gov = $_POST['gov'];
$sql = mysql_query("DELETE FROM governorate WHERE governorate_name = '$gov'")or die(mysql_error());
}
}

最佳答案

就您的错误消息而言,'...即使插入了值,系统也会显示错误消息:您必须在提交之前填写这些字段之一!!。 ..'

你的条件是错误的。改变

if(isset($_POST['city']) || isset( $_POST['lat']) || isset($_POST['long']) == "" )

if ((!isset($_POST['city']) || $_POST['city'] == "") 
&& (!isset($_POST['lat']) || $_POST['lat'] == "" )
&& (!isset($_POST['long']) || $_POST['long'] == "")) {

附带说明:您的代码容易受到 sql 注入(inject)的攻击。切换到 mysqli 或 PDO 并学习准备好的语句。

关于php - 如果值不存在则在 3 个表中插入值,如果存在则更新它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16925559/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com