Fernanditos
asked on
Mysql - Insert if specific field does not exists
Hi.
I have an INSERT query but I'm getting duplicate content every time it's executed.
How can I avoid to get duplicated content in the column "videourl" ?
I want "videourl" column with unique value so just ignore at insertion if value already exist?
Could you please use my real code attached in order to conform the right fix?
Thanks.
I have an INSERT query but I'm getting duplicate content every time it's executed.
How can I avoid to get duplicated content in the column "videourl" ?
I want "videourl" column with unique value so just ignore at insertion if value already exist?
Could you please use my real code attached in order to conform the right fix?
Thanks.
while ($row_rsMainData = mysql_fetch_assoc($rsMainData))
{
$sql_command = " INSERT INTO `video` (`videoid`, `title`, `description`, `videourl`, `videocategoryid`, `dateline`, `userid`, `videoservice`, `videoidservice`, `views`, `timelength`, `commentcount`, `ratingtotal`, `ratingnum`, `rating`, `reportthreadid`, `username`, `cachetags`)
VALUES(1, '".$row_rsMainData['title']."', '".$row_rsMainData['description']."', '".$row_rsMainData['link']."', ".$row_rsMainData['rss_data_id'].", 1239699262, 1, 'YouTube', '$codelink', ".$row_rsMainData['num_views'].", 373, 0, 0, 0, 0, 0, 'Reportero', '<em>None...</em>')";
$ok= mysql_query($sql_command, $vb_videos_conn);
}
Sorry, a small typo there, "vide" whould be "video":
ALTER TABLE video ADD UNIQUE(videourl);
ALTER TABLE video ADD UNIQUE(videourl);
ASKER
Thanks. Could you please tell me how would be the implementation in my code:
{
$sql_command = " INSERT INTO `video` (`videoid`, `title`, `description`, `videourl`, `videocategoryid`, `dateline`, `userid`, `videoservice`, `videoidservice`, `views`, `timelength`, `commentcount`, `ratingtotal`, `ratingnum`, `rating`, `reportthreadid`, `username`, `cachetags`)
VALUES(1, '".$row_rsMainData['title' ]."', '".$row_rsMainData['descri ption']."' , '".$row_rsMainData['link'] ."', ".$row_rsMainData['rss_dat a_id'].", 1239699262, 1, 'YouTube', '$codelink', ".$row_rsMainData['num_vie ws'].", 373, 0, 0, 0, 0, 0, 'Reportero', '<em>None...</em>')";
$ok= mysql_query($sql_command, $vb_videos_conn);
}
Thanks
{
$sql_command = " INSERT INTO `video` (`videoid`, `title`, `description`, `videourl`, `videocategoryid`, `dateline`, `userid`, `videoservice`, `videoidservice`, `views`, `timelength`, `commentcount`, `ratingtotal`, `ratingnum`, `rating`, `reportthreadid`, `username`, `cachetags`)
VALUES(1, '".$row_rsMainData['title'
$ok= mysql_query($sql_command, $vb_videos_conn);
}
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Define an unique index on the column:
ALTER TABLE vide ADD UNIQUE(videourl);
The php code will be unchanged, the INSERT will fail when the videourl allready exists in the table.