无法使用插入查询来使用php插入id

时间:2013-06-06 08:15:32

标签: php mysql insert-update

我有2张桌子

  • district_id
  • district_name

和村庄

  • ID

  • village _name

  • district_id

    我想在表中插入值..查询工作得很好,插入了村表的值,但是group_没有得到在分区表中插入的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());

           }
       }

   }

3 个答案:

答案 0 :(得分:1)

如果您使用的是AUTO_INCREMENT,则应该解决您的问题

INSERT INTO districts (district_name, governorate_id)VALUES('$dist', '$gov')

我还建议添加一些保护剂再次注入

答案 1 :(得分:1)

你需要这样做。

      $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());

答案 2 :(得分:0)

您正在将区名称插入区 ID 字段。因为你试图在ID中插入一个随机字符串(数字......我希望),所以它会增加一个0。

您应该找到在district_id字段中插入的最后一个ID,并在第二个插入中插入而不是$ dist。