父母与父母子表唯一ID字段

时间:2012-01-22 15:27:37

标签: mysql

我想知道是否有人可以帮助我。

我有一个表单,允许用户将信息保存到下表:

父表

   CREATE TABLE `finds` (
  `userid` int(6) NOT NULL,
  `locationid` int(6) NOT NULL,
  `findid` int(6) NOT NULL auto_increment,
  `findosgb36lat` float(10,6) NOT NULL,
  `findosgb36lon` float(10,6) NOT NULL,
  `dateoftrip` varchar(10) NOT NULL,
  `findcategory` varchar(15) NOT NULL,
  `findname` varchar(35) NOT NULL,
  `finddescription` varchar(150) NOT NULL,
  `detectorid` int(6) NOT NULL,
  `searchheadid` int(6) NOT NULL,
  `detectorsettings` varchar(600) default NULL,
  `pasref` varchar(30) default NULL,
  `findimage` varchar(200) default NULL,
  `additionalcomments` varchar(600) default NULL,
  `makepublic` varchar(3) NOT NULL default 'no',
  PRIMARY KEY  (`findid`),
  KEY `userid` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

表单的一部分将涉及保存图像,其中用户提供的信息将填充下表。

子表

    CREATE TABLE `images` (
      `imageid` int(6) NOT NULL auto_increment,
      `userid` int(6) NOT NULL,
      `locationid` int(6) NOT NULL,
      `findid` int(6) NOT NULL,
      `filepath` varchar(50) NOT NULL,
      PRIMARY KEY  (`imageid`),
      KEY `findid` (`findid`)
    ) ENGINE=InnoDB 

DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

连接两者的字段是'findid'。请问有人告诉我,有没有办法在父表中创建记录,从而分配唯一的'findid'值,同时将同一个唯一的'findid'值复制到子表,以便我可以链接记录。

非常感谢和亲切的问候

表格

 <form enctype="multipart/form-data" action="add.php" method="POST"> 
 Photo: <input type="file" name="photo"><br> 
 <input type="submit" value="Add"> 
 </form>

保存PHP

<?php 

 //This is the directory where images will be saved 
 $target = "images2/"; 
 $target = $target . basename( $_FILES['photo']['name']); 

 //This gets all the other information from the form 
 $pic=($_FILES['photo']['name']); 

 // Connects to your Database 
 mysql_connect("host","user","password") or die(mysql_error()) ; 
 mysql_select_db("database") or die(mysql_error()) ; 

 //Writes the information to the database 
 mysql_query("INSERT INTO `test` VALUES ('$pic')") ; 

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 
 ?> 

1 个答案:

答案 0 :(得分:0)

我不完全确定这是你问的问题,但如果你问如何插入一个表,从该表中获取自动递增的ID,然后右转并在查询中使用它下一个表,然后查看PHP函数mysql_insert_id()以在两个INSERT查询之间获取该值。