Jump to content

trying to remove duplicate values while entering it into a database


manjushreekaranth

Recommended Posts

I have written code in php to connect and insert rows into a MSSQL database. i used odbc to connect database.user can enter his details through the form. after submitting the form the details are getting stored into a database. while inserting rows into a database am not trying to insert duplicate values . for this i have given if conditions.these conditions are able to notice the user cname and name exist in the database if the same name exist. but the else part after these conditions are not working i.e rows are not getting inserted. i put everything inside the while loop. how can i correct it? This is my php code.

 

          $connect = odbc_connect('ServerDB','sa', 'pwd');

          //connects database 

        $query2="select count(*) from company";//this is needer for loop through 

        $result2=odbc_exec($connect,$query2); 

        while(odbc_fetch_row($result2)); 

        {    $count=odbc_result($result2,1); 

          echo "</br>","$count";    } 

          $query1="select * from company";   

          $result1 = odbc_exec($connect, $query1);

          # fetch the data from the database       

          while(odbc_fetch_row($result1))

          {          $compar[$count] = odbc_result($result1, 1);     

          $namearray[$count] = odbc_result($result1, 2);

          if($compar[$count]==$_POST['cname']) 

          {          echo "<script> alert(\"cname Exists\") </script>";   

          }         

          else  if($namearray[$count]==$_POST['name'])   

        {        echo "<script> alert(\"Name Exists\") </script>";        } 

        else

        {     

        $query=("INSERT INTO company(cname,name) VALUES ('$_POST[cname]','$_POST[name]') ");   

          $result = odbc_exec($connect, $query); 

          echo "<script> alert(\"Row Inserted\") </script>"; 

        } 

        }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.