I want to check is same value inserted in this field when I update a field.
ex: this is my table
id region region_manager_id
1 test1 rm1
2 test2 rm7
3 test3 rm9
4 test4 rm8
5 test5 rm3
Now if want to update region_manager_id = rm1 Where id = 3 this should not be because rm1 already assigned for id=1. so how to check same value is already inserted for another row when I update a field. is it possible in a single query?.
Use unique Key on region_manager_id , You can check duplicate id by error code. When mysql error code 1062 it means duplicate entry.
if(mysqli_errno($Conn)==1062){
error = "duplicate entry error message";
}
In your place, I would try two aprroaches, first with PHP and second directly on your database.
1st: in your PHP code, you can try to fetch a query like this: "SELECT id FROM table WHERE region_manager_id = 'rm1';", and if this query will return with NULL, then your region_manager_id is not used already in other row of your table.
2nd: you can use foreign keys and unique key statement, with this solution, your database will raise an error if you try to alter your table with already existing region_magnager_id key.
I hope this will help. :-)