2015-07-05 68 views
-1

我有一个更新现有数据的update.php表单。我的问题是当我想提交时,我的表单的一部分是动态的,所以他们可以在更新时向mysql添加一个新行,所以我一直在MySQL中获取重复的动态行。在更新期间向MySQL插入多行。 [不要插入重复记录]

$i = 0; 
while($i<count($fromhours)){ 
$fromhours[$i]= $fromhours[$i]; 
$fromminutes[$i]= $fromminutes[$i]; 
$tohours[$i]= $tohours[$i]; 
$tominutes[$i]= $tominutes[$i]; 
$resulthours[$i]= $resulthours[$i]; 
$resultminutes[$i]= $resultminutes[$i]; 
$hrscode[$i]= $hrscode[$i]; 
$gremark[$i]= $gremark[$i]; 
$query = "INSERT INTO `generalreport` ( 
    `ID`, `date`, `fromhours`, `fromminutes`, `tohours`, `tominutes`, `resulthours`, `resultminutes`, `code`, `remark` 
    ) VALUES (
    '".$id."', 
    '".$date."', 
    '".$fromhours[$i]."', 
    '".$fromminutes[$i]."', 
    '".$tohours[$i]."', 
    '".$tominutes[$i]."', 
    '".$resulthours[$i]."', 
    '".$resultminutes[$i]."', 
    '".$hrscode[$i]."', 
    '".$gremark[$i]."' 
    );"; 

mysql_query($query);   
$i++; 
}; 

如何使用此代码进行更新,只需在MySQL中插入新数据,旧数据就不会被复制。

+0

威力与此类似http://stackoverflow.com/questions/5171328/how-to-insert-distinct-records-from-table-a-to-table-b-both-tables-have-same-st – Jigar

+0

或这个http://stackoverflow.com/questions/8756689/avoiding-inserting-duplicate-rows-in-mysql – Jigar

回答

0

我认为你有索引的表中的MySQL这样,如果有任何新的数据输入有任何类似的日期和条目中的MySQL将自动弹出重复输入错误

+1

谢谢我已经添加了UNIQUE索引,它现在工作正常。 –

0

使用

INSERT INTO `generalreport` ( 
`ID`, `date`, `fromhours`, `fromminutes`, `tohours`, `tominutes`, `resulthours`, `resultminutes`, `code`, `remark` 
) VALUES (...),(...),(...)