Use insert ... where not exists
Example code
//prevent those pesky SQL injection attacks
$rand_no = mysql_real_escape_string($rand_no);
$name = mysql_real_escape_string($name);
//Only insert if group is not already used
$db->query("insert into user(name, `group`)
VALUES('$name',$rand_no)
where not exists
(select `group` from user where `group` = '$rand_no') )";
I've added the first two lines to remind you no to forget to escape those values to prevent SQL-injection attacks. (if $rand_no
is generated by the php code, there's no need to escape it of course, but if a user can manipulate it then you should)
Secondly group
is a reserved word, if you want to use it in a query you need to bracket it in backquotes ' ` '.
EDIT:
Enforce it in the database
Set the field group
to be a unique field using ALTER TABLE
ALTER TABLE CHANGE COLUMN `group` `group` UNIQUE