-1

How can I insert data to database without duplicate of the name for example if the name found in database show message how can do this??

<?php
$username ="root";
$password ="";
$hostname="localhost";
$db="a";
$dbhandle=mysql_connect($hostname ,$username ,$password,$db)or die('not connect to the database because:'.mysql_error());
 mysql_select_db($db,$dbhandle);
 $myusername=$_POST['user'];
$mypassword=$_POST['pass'];
$mypassword_conf=$_POST['Password_conff'];
if($mypassword==$mypassword_conf)
{
$sql="INSERT INTO aa( username, password,pass_con) VALUES 
('$myusername','$mypassword','$mypassword_conf')";
   if(! mysql_query($sql,$dbhandle))
      echo "not insert";
      else
         echo "insert is Done";

          mysql_close();

           }
else
{
    echo "not insert to db found error";
}

?>

lena lena
  • 49
  • 1
  • 1
  • 6

2 Answers2

0

Set the field as a unique key in the database. The database system will not let you insert duplicates in that case. Handle this error in your php code to alert the user. If you post some of your code, I could add a snippet

navjotk
  • 818
  • 1
  • 9
  • 15
0

You can safeguard against this by making fields unique by assigning them a UNIQUE index which upon inserting a duplicate would return an error which you could handle within PHP.

nblackburn
  • 268
  • 2
  • 14