I'm trying to make a database check in my webpage that will check to make sure two columns (first_name and last_name) are unique. In MySql I already have those two columns as UNIQUE.
I want to have this check performed on the webpage, and return an error message if a first_name and last_name are already listed in the table. ex. If Tom Jones is in the table already, an error will show up if Tom Jones is input into the form. My statement below checks for this uniqueness, but it checks across all rows in the table. I need it to check row by row. member_no is the primary key, and I thought about searching with this, however, I want to check each row for uniqueness and I don't have a specific number to search.
This is just on the website/form side, since the UNIQUE statement for first_name and last_name is already active on MySql.
Or is there a more direct way to check the uniqueness in the table?
This is my statement:
$command = "SELECT * FROM member_info";
$member_result = mysql_query($command);
$mdata = mysql_fetch_object($member_result);
if ($first_name == $mdata->first_name && $last_name == $mdata->last_name) {
$error_message = "The first and last names must be unique. ";
}
This will check the table but will not discriminate between the rows. ex. if row 1 is Tom Jones, and row 2 is Bob Smith, and if I put into the form 'Tom Smith' the error will come back as not unique.