How to use PHP to prevent duplicate entries into our MySQL database

Home > Search > How-to
  by

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};



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments