JS PHP automatic return to previous page when if is false

i am sending some data to a mysql db.
when the primary key is already in use, it should give an alert and then go back to the previous page with all input already filled as before.

It is working fine, but when the primary key is already in use, i got both alerts.
First the else alert and then always the other alert too.

what am i missing?

thx

            if($wert_vorher != $wert_nachher )
            {
                echo '<script>alert("Daten wurden eingefügt")</script>';
                exit(1);        
            }           
            else 
            {
                echo '<script>alert("Daten schon vorhanden")</script>';
                echo '<script language="javascript">
                            javascript:history.back()
                            </script>'; 
                exit(1);            
            }