Link to home
Start Free TrialLog in
Avatar of Fernanditos
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.
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);
}

Open in new window

Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

>> How can I avoid to get duplicated content in the column "videourl" ?

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.
Sorry, a small typo there, "vide" whould be "video":

ALTER TABLE video ADD UNIQUE(videourl);
Avatar of Fernanditos
Fernanditos

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

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial