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

        } 

        }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.