Jump to content

[SOLVED] Stop mysql insert duplicate entry


graham23s

Recommended Posts

Hi Guys,

 

to keep track of who has dfownloaded files on my site i have a table

 

downloads

 

within this table is 3 rows

 

id  :  file_id  :  downloaders_id

 

all are (int) my question is if the same user downloads the file again another entry is made for them (the auto increment id is different) but i didn't want the same user to have more than 1 file_id and downloaders_id the same

 

i know i can query mysql to see if the details are the same but if i find the entry has already been added wheat's the best way to stop that entry going in.

 

thanks for nay help

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/54333-solved-stop-mysql-insert-duplicate-entry/
Share on other sites

The best way to stop the entry from going in is not running the sql IE:

 

<?php

$sql = "SELECT id FROM downloads WHERE file_id = '" . $file_id . "' and downloaders_id = '" . $downloaders_id . "' LIMIT 1";
$qu = mysql_query($sql);

if (mysql_num_rows($qu) < 1) {
     $sql = "INSERT INTO downloads ....";
     mysql_query($sql);
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.