gpt4 book ai didi

php - 无法通过插入查询来使用 php 插入 id

转载 作者:行者123 更新时间:2023-11-29 08:29:54 24 4
gpt4 key购买 nike

我有 2 张 table

  • district_id
  • 地区名称

和村庄

  • id

  • 村庄名称

  • district_id

    我想向表中插入值..查询可以很好地插入村庄表的值,但district_没有获得插入到地区表中的id值,而是显示0。

谁能帮帮我???

代码:

if(isset($_POST['add']))
{
if ((!isset($_POST['city']) || $_POST['city'] == "")
&& (!isset($_POST['lat'])|| $_POST['lat'] == "" )
&& (!isset($_POST['long'])|| $_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());

}
}

}

最佳答案

你需要这样做。

      $sqld = mysql_query("INSERT INTO districts (district_id, district_name, governorate_id)VALUES('', '$dist', '$gov')") or die(mysql_error());

$dist_id = mysql_insert_id();

$sql = mysql_query("INSERT INTO village (id, village_name, district_id, lattitude, longitude)VALUES('', '$city', '$dist_id' ,'$lat', '$long')")or die(mysql_error());

关于php - 无法通过插入查询来使用 php 插入 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16956950/

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