Krishnadas PC
Krishnadas PC

Reputation: 6539

Disable mysql insert if two row values repeated

enter image description here

I want to disable a mysql insert if two rows have same values. For Example: If at row 1 title="title1" & url="www.example.com" AND also at row 2 title="title1" & url="www.example.com" I want only the first row to be inserted.

FOUND THE ANSWER:

ALTER TABLE `my_table` ADD UNIQUE `unique_index`(`column1`, `column2`);

Upvotes: 0

Views: 190

Answers (1)

rohitr
rohitr

Reputation: 371

Before inserting data fire Select query as

$s1 = "Select * From your table where title = '$title' and URL = '$URL'";

 $res = mysql_query($s1) or die("!query");

 if(mysql_affected_rows ())
 {
   echo "record exists";
  }
 else
 {
 echo "your insert query";
  } 

Upvotes: 1

Related Questions