-1

The question has been answered, thanks

Barry McDaid1982
  • 157
  • 1
  • 1
  • 14

1 Answers1

2

You can do something like that :

ALTER TABLE  `table_name` ADD UNIQUE ( `column_that_should_be_unique` )

Or you can check it in PHP using :

$query = mysqli_query($con, "SELECT * FROM table_name WHERE column_name='".$user_input."'");

if(mysqli_num_rows($query) > 0){
    //block user from inserting data
}

Or you can do both to ensure that the database will have no duplicates

  • Second example opens OP to sql injections. – chris85 Aug 02 '15 at 21:09
  • Thank you, can I ask how does that check to see if it is duplicated? – Barry McDaid1982 Aug 02 '15 at 21:10
  • Would I insert the php line before or inside the while statement? – Barry McDaid1982 Aug 02 '15 at 21:12
  • When you add a new drug, the query search your database to see how many drug with the name $user_input it has. If there is one or more then block the user from inserting a new drug. The best way is to prevent the user from ever inserting duplicate content when inserting new drugs instead of displaying only unique drugs. – GitCommit Victor B. Aug 02 '15 at 21:14
  • Or you can do like that : array_unique($r1['drug_name']) to remove duplicates from your array. See : http://php.net/manual/fr/function.array-unique.php – GitCommit Victor B. Aug 02 '15 at 21:16
  • Also use prepared statements don't use the SQL provided here. User input should never be inputted directly into SQL. http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – chris85 Aug 02 '15 at 21:16