So, for sake of simplicity, let's say I've got a table like this (f1,f2,f3 are fields):
f1 f2 f3
a b c
d e f
And say I've got a PHP array like this I want to add in to the table:
a g h
Now, I want to be able to add this one in, but I don't want to be able to add something like this:
a e f
Field f1 can have duplicates. Fields f2 & f3 cannot. I've tried adding a unique index to f2, and used REPLACE INTO
, but it's not what I want. I need there to be a way to only insert if the f2 & f3 field entries are not already found on the same row in the database table.
$query = "REPLACE INTO table1 (title, description, start, end, location, affiliation)
VALUES ('{$my_title_php_array[$count]}',
'{$my_description_php_array[$count]}',
'{$my_start_php_array[$count]}',
'{$my_end_php_array[$count]}',
'{$my_location_php_array[$count]}',
'{$my_affiliation_php_array[$count]}')”;
description
is of data type 'TEXT', which can't be used as an INDEX
(too big). Ideally, I'd like to have all be necessarily checked in comparison with the incoming PHP array entries.
Let's say I have these things in my new example's PHP array:
Title1 Desc1 1:00pm 2:00pm Loc1 Aff1
Title1 Desc1 3:00pm 4:00pm Loc1 Aff1
Title2 Desc2 1:00pm 2:00pm Loc2 Aff2
Title2 Desc1 1:00pm 2:00pm Loc3 Aff3
These should all be considered unique and entered in. I'd like to be able to enter them only when they don't exactly match a row in my database already. Desc1 is long. The descriptions are usually the most unique. Is there a way of shortening?
I had to look up this information myself recently and here is the answer I found: Use multiple columns as unique identifier for mysql
I believe it's what you're needing.
Globally, you want a constraint on a pair of entry? In MySQL, I think you can't. Just, do a select (something like SELECT COUNT(*) FROM _table WHERE f1 = ? AND f2 = ?
replace with your values) check the result.
I personally would use PHP and mysql to check the database table for existing rows for "f2" and "f3".
For sake of simplicity, I will use standard mysql and will assume the table name is "alphabet". Example:
$array = array('a', 'e', 'f');
$checkTable = mysql_query("SELECT * FROM `alphabet` WHERE `f2` = '".$array[1]."' OR `f3` = '".$array[2]."'");
if(!mysql_num_rows($checkTable)){
mysql_query("INSERT INTO `alphabet` VALUES('".$array[0]."', '".$array[1]."', '".$array[2]."')");
}
Something like the above. Lets go through it together.
We first set the array containing the letters "a", "e", and "f".
$array[0] = "a", $array[1] = "e", and $array[2] = "f".
We check in the database for any rows where the field "f2" or "f3" is equal to the second and third parameter of our array.
Using mysql_num_rows() if there is no duplicates it will go ahead and add the array into the database table "alphabet".
Correct me if I am wrong or if anybody has a simpler solution. Hope I helped.
<?php
$data[] = 'f1 value';
$data[] = 'f2 value';
$data[] = 'f3 value';
for ($i = 0; $i < count($data); $i++) {
for ($j = $i+1; $j < count($data); $j++) {
$value_a = $data[$i];
$value_b = $data[$j];
$query = "SELECT *FROM mytable WHERE ";
$query.= "(f1=".$value_a." AND f2=".$value_b.") OR";
$query.= "(f1=".$value_a." AND f3=".$value_b.") OR";
$query.= "(f2=".$value_a." AND f3=".$value_b.")";
$result = mysql_query($query);
if (mysql_num_rows($result)) {
//duplicated
}
}
}
?>