php mysql insert(当某些内容被重复时,插入一个空值)
我想插入一些文章。我想先做出判断,如果图像值已经存在于数据库中,请插入空值。 欲了解更多信息,请解释: 如果有这些数据:php mysql insert(当某些内容被重复时,插入一个空值),php,mysql,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,imag
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
这是我的代码,但它仍然将repeat值插入数据库。有人能帮我吗?谢谢
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."' ");;
}
}
您的第一个查询包含一个错误。您必须删除“图像”前的圆点:
正如弗朗西斯科所提到的,点可能是这个问题的一个问题 不太清楚你想在这里做什么。但是,如果您试图阻止同一图像文件存储两次,则应使用以下类似方法:
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;
}
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;
}