Your present code already checks for duplicate entries in the while loop (though it's not very resource efficient).
It will not add duplicate name entries to your table, but will add an extra vote to an already existing name.
PHP Code:
while ($counter < $numLines) {
$line = rtrim($lines[$counter]);
$check = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM nominations WHERE username='$line'"));
if ($check[0] == 0) {
mysql_query("INSERT INTO nominations VALUES ('$line', '1')");
} else {
mysql_query("UPDATE nominations SET num=num+1 WHERE username='$line'");
}
$counter++;
}
However, it does not prevent the user from entering the same name multiple times in the same textfield and hence giving multiple 'votes' to the same candidate. (I assume names are to be entered).
If the user is only allowed to submit the form once, you can load all the textfield entries into an array and check for unique values before submitting it to the while loop.
This minimal alteration to your existing code will do that:
PHP Code:
while ($counter < $numLines) //This while loop loads all textfield entries into the array $names
{
$names[] = rtrim($lines[$counter]);
}
$lines = array_unique($names); //this checks the array for duplicate entries
$numLines = count($lines); //This counts the number of values in the refined array
//The remaining is your original code with the rtim() removed as it's already done
$counter = 0;
while ($counter < $numLines) {
$line = $lines[$counter];
$check = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM nominations WHERE username='$line'"));
if ($check[0] == 0) {
mysql_query("INSERT INTO nominations VALUES ('$line', '1')");
} else {
mysql_query("UPDATE nominations SET num=num+1 WHERE username='$line'");
}
$counter++;
}