PHP - Prevent duplicate entries into MySQL
by
Jeremy Canfield |
Updated: March 17 2020
| PHP articles
When using PHP to insert records into our MySQL table, if / else statements can be used to prevent duplicate entries from being entered. For this tutorial, let's say we are using inserting error codes into our MySQL table. We will assume there is already a record in MySQL with error code ERR134. The following code will display an error if we try to insert ERR134 into MySQL.
$data_being_entered = $GET['data'];
$insert = "insert into tablename $data_being_entered";
if($data_being_entered == 'ERR134') {echo 'There is already a record in MySQL with error message ERR134';}
else{$insert};
Did you find this article helpful?
If so, consider buying me a coffee over at
Comments
June 25 2022 by sadsadsasdsd