插入数据库时​​,在1-1000之间的两个不同字段中插入一个数字,但不存在

Hi I need to be able to insert a random value between 1-1000 in to the "left" field and a random value between 1-1000 in the "top" field or a database row - however I want it to check if the "left" and "top" dont already have the 2 proposed values?

lets say left has been randmomly assigned the value 23 and top has been randomly assigned the value 967, if in the database it has already got an entry with left being 23 and top being 967 then randomly assign 2 new values that are not taken?

Cheers

This is pseudocode, but it shows the sequence of events:

$exists = 1;

do
{
  // GENERATE $left and $top

  $query_results = /* SELECT * FROM table WHERE left = $left and top = $top */

  $exists = /* number of rows on $query_results */;

  if ($exist == 0)
  {
    // INSERT $left, $top INTO table
  }

}
while ($exists > 0); 

Maybe something like

$left = mt_rand(1,1000);

$top = mt_rand(1,1000);

while ($top == $left) {

    $top = mt_rand(1,1000);
}

mysql_query("INSERT INTO table (left,top) VALUES (" . check($left, 'left') . ", " . check($top, 'top') . ")");

function check($input, $field) {

    $result = mysql_query("SELECT COUNT(*) as count FROM table WHERE {$field} = {$input}");

    $row = mysql_fetch_assoc($result);

    if ($row['count'] != 0) {

        check(mt_rand(1,1000), $field)
    }
    else {

        return $input;
    }
}

Then again recursive queries are no-no...

This is assuming left and top shouldn't equal each other.

$n1 = rand(1,1000);
$n2 = rand(1,1000);

$link = mysql_connect(..);

$q = "select * from tbl where left = $n1 AND top = $n2";
$res = mysql_query($q,$link);

if(mysql_num_rows($res)==0)
{
   $q = "insert into tbl (left, top) values ($n1, $n2)";
   mysql_query($q, $link);
}

mysql_close($link);