2012-01-22 37 views
0

我想知道是否有人能夠幫助我請。父母和子女表唯一ID字段

我已經有一個形式,它允許用戶保存信息如下表:

父表

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."; 
} 
?> 
+0

嗨,非常感謝回覆我的帖子。我已經添加了圖像表單並將PHP腳本保存到了原始文章中,希望能夠提供更多關於我想要實現的信息。我遇到的問題是不保存文件,但我只是不確定,也許是因爲我是新手,如何在獨特的'findid'尚未創建時鏈接父記錄和子記錄父表。親切的問候 – IRHM

+0

總是需要時間的問題...只有一半的問題解決了只有...讀者必須瞭解問題... –

回答

0

我不能完全肯定這是你問什麼,但如果你是詢問如何插入到一個表中,從該表中獲取自動遞增的ID,然後右轉並將其用於下一個表的查詢中,然後查看在PHP函數mysql_insert_id()上獲取兩個INSERT查詢之間的值。

+0

嗨,許多thnaks爲此。讀了更多關於這個,我認爲這將起作用。親切的問候 – IRHM