Hi this code makes two column on a row to be unique:
ALTER TABLE 'tableName' ADD UNIQUE( 'column1', 'column2');
With the code above, if you insert value to column1
and column2
identically, it will not be inserted.
For example:
$value1 = 1;
$value2 = 2;
$value3 = 3;
mysql_query("INSERT INTO tableName (column1, column2) VALUES ('$value1', '$value2')"); //This will work.
mysql_query("INSERT INTO tableName (column1, column2) VALUES ('$value1', '$value3')"); //This will work.
mysql_query("INSERT INTO tableName (column1, column2) VALUES ('$value1', '$value2')"); //This WILL NOT work THE SECOND time around because of the unique constraint.
Or refer to this code:
$value1 = 1;
$value2 = 2;
$resultSet = mysql_query("SELECT column1 FROM tableName WHERE column1 = '$value1'");
$rows = mysql_num_rows($resultSet);
if($rows >= 1){
echo $value1 . " already exist.";
}
else{
$query = mysql_query("INSERT INTO tableName (column1, column2) VALUES('$value1','$value2')");
if(){
echo "Value inserted.";
}else echo mysql_error();
}
The code above checks the value of $value1
before inserting the two values to determined if $value1
already exist.
Hope this help you. :-)