0
我想插入一些文章。我想先做個裁判,如果圖像值已經在數據庫中,插入一個空值。 詳細解釋: 如果有這些數據:php mysql插入(當有東西重複時,插入一個空值)
title1,image1
title2,image2 //this is image2 first appear, so insert it
title3,image2 //image2 has already in the database, so insert an empty value for just image field
title4
title5,image3
最終數據插入到數據庫應該是這樣的:
title | image
title1,image1
title2,image2
title3
title4
title5,image3
這裏是我的代碼,但它仍然插入重複值成數據庫。那麼有沒有人可以幫助我?謝謝。
foreach{//first here has a foreach to make all the articles as a queue
...
if(!empty($image)){ //first judge if the insert article had a image value
$query1 = mysql_query("select * from articles where .image = '".$image."' ");
while ($row = mysql_fetch_array($query1)) {
$image1 = $row['image'];
}
}
if(!empty($image1)){ //make a query first if the image has already in the database
mysql_query("INSERT INTO articles (title,image) SELECT '".$title."','' ");
}else{
mysql_query("INSERT INTO articles (title,image) SELECT '".$title."','".$image."' ");;
}
}
權,一些寫入錯誤......它傳遞,而工作,我是這麼認爲的。 – cj333 2011-03-16 11:57:39