Currently Ive been just making a SQL column UNIQUE to block duplicate entries but now I need to allow duplicate entries if another column is different. I am not sure on the best way to do this. Essentially I would like to do something as follows but it just inserts the same values multiple times.
// $link is being outputted from a external RSS feed //
// $name and $location are account holder names //
$name = 'Bob';
$sql = "SELECT link FROM `table` WHERE name='$name'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$db_link = $row['link'];
if($link == $db_link){
// Pass and do nothing
}else{
// Insert into database if 3 values do not exist already
$sql2 = "INSERT INTO `table` (link, name) VALUES ('$link','$name')";
if (mysqli_query($conn, $sql2)) {
echo "record created successfully";
} else {
//echo "Error: " . $sql2 . "<br>" . mysqli_error($conn);
}
}
}
}
So basically I am trying to avoid showing duplicates per account holder, but a different account holder can have the same link in the database. Sorry if this is confusing.. If the name is different then allow a link to be a duplicate. I have looked at similar questions asked but do not understand the methods explained and if they would be the right solution I am looking for. Thanks!
CREATE UNIQUE INDEX index_name ON table_name ( column1, column2,...);