Record inserting twice

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepaks85
    New Member
    • Aug 2006
    • 114

    Record inserting twice

    Dear Sir,

    Whenever I am trying to insert data into my database it inserts twice. Please help me.

    The code is given below:

    THE SQL TABLE:

    CREATE TABLE `user` (
    `id` int(11) NOT NULL auto_increment,
    `iata` int(14) NOT NULL default '0',
    `name` varchar(255) NOT NULL default '',
    `email` varchar(255) NOT NULL default '',
    `username` varchar(255) NOT NULL default '',
    `password` varchar(255) NOT NULL default '',
    `companyname` varchar(255) NOT NULL default '',
    `date` datetime NOT NULL default '0000-00-00 00:00:00',
    `city` varchar(255) NOT NULL default '',
    `state` varchar(255) NOT NULL default '',
    `province` varchar(255) NOT NULL default '',
    `country` varchar(255) NOT NULL default '',
    `phone` int(255) NOT NULL default '0',
    `fax` int(255) NOT NULL default '0',
    `cemail` varchar(255) NOT NULL default '',
    PRIMARY KEY (`id`)
    ) TYPE=MyISAM AUTO_INCREMENT= 3031 ;


    The PHP code:


    <?php

    $iata = $_POST['iata'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $companyname = $_POST['companyname'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $province = $_POST['province'];
    $country = $_POST['country'];
    $phone = $_POST['phone'];
    $fax = $_POST['fax'];
    $cemail = $_POST['cemail'];

    $query = ("INSERT INTO $SQL_USR_TBL (iata, name, email, username, password, companyname, date, city, state, province, country, phone, fax, cemail) values('$iata', '$name', '$email', '$username', '$password', '$companyname', now(), '$city', '$state','$prov ince', '$country','$ph one','$fax','$c email')");

    ?>

    Please help me Sir.

    Thanks
    Deepak
  • theRamones
    New Member
    • Nov 2006
    • 13

    #2
    really, what is the result of mysql_affected_ rows() when you execute that query?

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      This cannot be all the code you have in your script!
      Usually this problem has to do with the program flow and sequence statements leading up to the execution of the insert command.

      So, in order to be able to help here, show the code of the script you use and that causes this problem.

      And show the code within the code tags!!!

      Ronald :cool:

      Comment

      • b1randon
        Recognized Expert New Member
        • Dec 2006
        • 171

        #4
        Originally posted by ronverdonk
        This cannot be all the code you have in your script!
        Usually this problem has to do with the program flow and sequence statements leading up to the execution of the insert command.

        So, in order to be able to help here, show the code of the script you use and that causes this problem.

        And show the code within the code tags!!!

        Ronald :cool:
        An easy way to get multiple rows is with the mysql result object. If you're using one result to insert into another table and doing foreach that could be your problem. PHP actually puts two records for each column into the array. One record is at the column index (i.e. result[0][0]) and one is keyed by the name of the column (i.e. result[0][ColName]). Check for that problem.

        Comment

        • subash
          New Member
          • Sep 2006
          • 32

          #5
          Hi Deepak,

          This problem can be resolved on checking a session flag, Let me give you a brief description :

          1. After the insertion has happen, just set a session variable "true"

          2.Before inserting the command just check whether variable set as not-true


          Example of the flow:

          // Check the session variable value
          // If it is not true - allow to the loop
          if (condition)
          {
          // Insert statement

          // Set the session variable to "true"

          }


          - Just try it and let me know how it works

          Subash

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            And don't forget to set it off again, otherwise you run into problems on subsequent (i.e. legal) inserts!

            Aprt from that, I still think it is a logic/sequence problem in the code. So deepak, why don't you show us the code?

            Ronald :cool:

            Comment

            • subash
              New Member
              • Sep 2006
              • 32

              #7
              As Ronald said the session should be destroyed in the beginning of the file

              Subash

              Comment

              Working...