开发者

php mysql insert (when something is repeart, insert an empty value)

I want insert some articles. I want make a judge first, if the image value has already in the database, insert a null value. For more explain: If there have these data:

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  

the final data insert into the database should like this:

 title | image
title1,image1
title2,image2 
title开发者_如何学运维3
title4
title5,image3 

Here is my code, but it still insert the repeat value into the database. So is there any one can help me? thanks.

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."' ");;
}
}


Your first query contains an error. You have to remove the dot before "image":

where **.**image 


As Francesco mentioned that dot could be a problem for that query.

Not really clear what you are trying to do here. But if you are trying to stop same image file being stored twice then you should use something like this below:

function is_same_file( $image1, $image2 ){

    if( filesize($image1) != filesize($image2)
       return false;

    if( hash_file('md5', $image1) != hash_file('md5', $image2) )
       return false;

    return true;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜