Database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken

    Database

    I have been working on this problem for a few days.

    When I write data into a sql database, the results are duplicate listings:

    PHP Code:
    <?php
    session_start() ;
    $dbh=mysql_conn ect ("localhost" , "username", "password") or die ('Cannot
    connect to the database: ' . mysql_error());
    mysql_select_db ("database name") or die ("Cannot locate database:
    ".mysql_error() );

    mysql_query("IN SERT INTO tablename (website) Values('website _es')");
    echo "Data Transfer Complete";
    ?>

    After emptying the table and running the PHP script:
    The results in the database:
    id website
    130 website_es
    131 website_es

    As you can see, I have been working on this for some time, id=131.

    Any suggestions for stopping the insertion of the duplicate rows?

    Thanks.

    Ken


  • Eric Bohlman

    #2
    Re: Database

    "Ken" <kkrolski@wi.rr .com> wrote in
    news:Nl1cc.1339 1$YC5.9800@twis ter.rdc-kc.rr.com:
    [color=blue]
    > I have been working on this problem for a few days.
    >
    > When I write data into a sql database, the results are duplicate
    > listings:
    >
    > PHP Code:
    > <?php
    > session_start() ;
    > $dbh=mysql_conn ect ("localhost" , "username", "password") or die
    > ('Cannot connect to the database: ' . mysql_error());
    > mysql_select_db ("database name") or die ("Cannot locate database:
    > ".mysql_error() );
    >
    > mysql_query("IN SERT INTO tablename (website) Values('website _es')");
    > echo "Data Transfer Complete";
    > ?>
    >
    > After emptying the table and running the PHP script:
    > The results in the database:
    > id website
    > 130 website_es
    > 131 website_es
    >
    > As you can see, I have been working on this for some time, id=131.[/color]

    The most obvious possibility is that the script is somehow getting executed
    multiple times. Change that mysql_query into a simple echo and see how
    many of them you get.

    BTW, there's obviously more to your code than you've shown us (you call
    session_start() but don't show any use of session variables, you don't
    close your database connection, etc.). It's possible the problem lies
    elsewhere. If you haven't already done so, strip out *everything* but the
    insertion code and see if you can duplicate the problem. Very often you'll
    get an "aha!" when you do that; if not, post the complete but stripped-down
    code, *copying and pasting it* into your post rather than retyping it (code
    invariably mutates when you retype it, and sometimes the mutation actually
    fixes the problem because you typed what you meant to write rather than
    what you really did write).

    Comment

    • Geoff Berrow

      #3
      Re: Database

      I noticed that Message-ID: <Nl1cc.13391$YC 5.9800@twister. rdc-kc.rr.com>
      from Ken contained the following:
      [color=blue]
      >
      >mysql_query("I NSERT INTO tablename (website) Values('website _es')");
      >echo "Data Transfer Complete";[/color]

      Where are the values coming from?

      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • Ken

        #4
        Re: Database

        There is more code but it has all been commented out.
        I left the session_start() in case it was an underlying cause. But I also
        commented out session_start, and had the same problem.

        I agree the page seems to be running twice but I cannot figure out why.

        Good thought about using echo.

        I just did that and the statement only displays once per run. "But a good
        idea for future problems." Notice that I have an
        echo "Data Transfer Complete"; statement. That only displays once and the
        page does not seem to rewrite(refresh ) between rows in the database.

        I actual copied the code but changed the username, password and database &
        table names as you would expect.

        I will copy the active code into a test.php file and see what happens.

        Thanks for the suggestions.

        Ken
        [color=blue][color=green]
        > > When I write data into a sql database, the results are duplicate
        > > listings:
        > >
        > > PHP Code:
        > > <?php
        > > session_start() ;
        > > $dbh=mysql_conn ect ("localhost" , "username", "password") or die
        > > ('Cannot connect to the database: ' . mysql_error());
        > > mysql_select_db ("database name") or die ("Cannot locate database:
        > > ".mysql_error() );
        > >
        > > mysql_query("IN SERT INTO tablename (website) Values('website _es')");
        > > echo "Data Transfer Complete";
        > > ?>
        > >
        > > After emptying the table and running the PHP script:
        > > The results in the database:
        > > id website
        > > 130 website_es
        > > 131 website_es
        > >
        > > As you can see, I have been working on this for some time, id=131.[/color]
        >
        > The most obvious possibility is that the script is somehow getting[/color]
        executed[color=blue]
        > multiple times. Change that mysql_query into a simple echo and see how
        > many of them you get.
        >
        > BTW, there's obviously more to your code than you've shown us (you call
        > session_start() but don't show any use of session variables, you don't
        > close your database connection, etc.). It's possible the problem lies
        > elsewhere. If you haven't already done so, strip out *everything* but the
        > insertion code and see if you can duplicate the problem. Very often[/color]
        you'll[color=blue]
        > get an "aha!" when you do that; if not, post the complete but[/color]
        stripped-down[color=blue]
        > code, *copying and pasting it* into your post rather than retyping it[/color]
        (code[color=blue]
        > invariably mutates when you retype it, and sometimes the mutation actually
        > fixes the problem because you typed what you meant to write rather than
        > what you really did write).[/color]


        Comment

        • Ken

          #5
          Re: Database

          To troubleshoot the problem, I am only using one variable, website, and the
          value for the variable is text, website_es, listed as ' website_es ' without
          the spaces after ' and before '.

          Ken

          "Geoff Berrow" <blthecat@ckdog .co.uk> wrote in message
          news:ep9170tjng 3ovk17uv7le6p8q dthibj7p0@4ax.c om...[color=blue]
          > I noticed that Message-ID: <Nl1cc.13391$YC 5.9800@twister. rdc-kc.rr.com>
          > from Ken contained the following:
          >[color=green]
          > >
          > >mysql_query("I NSERT INTO tablename (website) Values('website _es')");
          > >echo "Data Transfer Complete";[/color]
          >
          > Where are the values coming from?
          >
          > --
          > Geoff Berrow (put thecat out to email)
          > It's only Usenet, no one dies.
          > My opinions, not the committee's, mine.
          > Simple RFDs http://www.ckdog.co.uk/rfdmaker/[/color]


          Comment

          • Jochen Daum

            #6
            Re: Database

            Hi Ken,

            On Sun, 4 Apr 2004 19:02:07 -0500, "Ken" <kkrolski@wi.rr .com> wrote:
            [color=blue]
            >I have been working on this problem for a few days.
            >
            >When I write data into a sql database, the results are duplicate listings:
            >
            >PHP Code:
            ><?php
            >session_start( );
            >$dbh=mysql_con nect ("localhost" , "username", "password") or die ('Cannot
            >connect to the database: ' . mysql_error());
            > mysql_select_db ("database name") or die ("Cannot locate database:
            >".mysql_error( ));
            >
            >mysql_query("I NSERT INTO tablename (website) Values('website _es')");
            >echo "Data Transfer Complete";
            >?>
            >[/color]
            In addition to the others comments, are you mayeb using a header
            ("Location: "...)?

            Note that the code after the header will be excuted if you don't use
            die();

            HTH, Jochen
            --
            Jochen Daum - Cabletalk Group Ltd.
            PHP DB Edit Toolkit -- PHP scripts for building
            database editing interfaces.
            Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

            Comment

            • Ken

              #7
              Re: Database

              I copied the code just needed to transfer the data into another file. It
              still produced the problem.

              But 30 minutes later, the transfer was correct. I think the problem is in
              the db manager I am using DB Manager Professional 2.2.0 from DB Tools. If I
              run the code in a fast sequence numerous times, the data display locks up.
              I need to close the program and then reopen it to show the new data.
              BDManager is probably causing the duplicate entries.

              I was going to use phpmyadmin but the readme said the script must be in the
              root directory and I did not want it intermixed with my code.

              What are you using to monitor the database?

              Thanks for the help.

              Ken

              I am using
              "Eric Bohlman" <ebohlman@earth link.net> wrote in message
              news:Xns94C1C5E B54818ebohlmano msdevcom@130.13 3.1.4...[color=blue]
              > "Ken" <kkrolski@wi.rr .com> wrote in
              > news:Nl1cc.1339 1$YC5.9800@twis ter.rdc-kc.rr.com:
              >[color=green]
              > > I have been working on this problem for a few days.
              > >
              > > When I write data into a sql database, the results are duplicate
              > > listings:
              > >
              > > PHP Code:
              > > <?php
              > > session_start() ;
              > > $dbh=mysql_conn ect ("localhost" , "username", "password") or die
              > > ('Cannot connect to the database: ' . mysql_error());
              > > mysql_select_db ("database name") or die ("Cannot locate database:
              > > ".mysql_error() );
              > >
              > > mysql_query("IN SERT INTO tablename (website) Values('website _es')");
              > > echo "Data Transfer Complete";
              > > ?>
              > >
              > > After emptying the table and running the PHP script:
              > > The results in the database:
              > > id website
              > > 130 website_es
              > > 131 website_es
              > >
              > > As you can see, I have been working on this for some time, id=131.[/color]
              >
              > The most obvious possibility is that the script is somehow getting[/color]
              executed[color=blue]
              > multiple times. Change that mysql_query into a simple echo and see how
              > many of them you get.
              >
              > BTW, there's obviously more to your code than you've shown us (you call
              > session_start() but don't show any use of session variables, you don't
              > close your database connection, etc.). It's possible the problem lies
              > elsewhere. If you haven't already done so, strip out *everything* but the
              > insertion code and see if you can duplicate the problem. Very often[/color]
              you'll[color=blue]
              > get an "aha!" when you do that; if not, post the complete but[/color]
              stripped-down[color=blue]
              > code, *copying and pasting it* into your post rather than retyping it[/color]
              (code[color=blue]
              > invariably mutates when you retype it, and sometimes the mutation actually
              > fixes the problem because you typed what you meant to write rather than
              > what you really did write).[/color]


              Comment

              • Jan Pieter Kunst

                #8
                Re: Database

                In article <Nl1cc.13391$YC 5.9800@twister. rdc-kc.rr.com>,
                "Ken" <kkrolski@wi.rr .com> wrote:
                [color=blue]
                > When I write data into a sql database, the results are duplicate listings:[/color]

                [...]
                [color=blue]
                > After emptying the table and running the PHP script:
                > The results in the database:
                > id website
                > 130 website_es
                > 131 website_es[/color]


                This may not address the underlying problem (insert happening multiple
                times) but would it not also be a good idea to define the 'website'
                column as UNIQUE if it isn't supposed to contain duplicate values?

                JP

                --
                Sorry, <devnull@cauce. org> is een "spam trap".
                E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

                Comment

                • Ido

                  #9
                  Re: Database

                  -----BEGIN PGP SIGNED MESSAGE-----
                  Hash: SHA1

                  On Mon, 05 Apr 2004 00:45:52 GMT
                  "Ken" <kkrolski@wi.rr .com> wrote:
                  [color=blue]
                  > To troubleshoot the problem, I am only using one variable, website,
                  > and the value for the variable is text, website_es, listed as '
                  > website_es ' without the spaces after ' and before '.[/color]

                  Ken, it looks like you're dictating code from another project. The
                  reference to 'website_es' is not a variable, but rather a literal. Try
                  this out for size:

                  // make sure $website_es exists beforehand!
                  if (isset($website _es)) {
                  $website_es = base64_encode($ website_es) // to make it SQL-safe
                  mysql_query("IN SERT INTO tablename (website) Values ('$website_es') ");
                  echo "Data Transfer Complete";
                  }

                  You were missing the dollar sign next to website_es...th is isn't ASP, in
                  PHP you refer to variables by prefixing their name with a $.

                  When reading the value, use base64_decode to return the SQL-safe form to
                  its original form. For example:

                  if ($result = @mysql_query("S ELECT website FROM tablename"))
                  {
                  while($row = @mysql_fetch_as soc($result))
                  {
                  $website_es = base64_decode($ row['website']);
                  $website_id = $row['id'];
                  echo "id = $id, website = $website_es <br>";
                  }
                  }

                  There are some features I've used above that you will probably not
                  immediately be familiar with, but they are all fully explained in the
                  php manual at www.php.net. RTFM, as it were. :)

                  Hope this helps,

                  Ido
                  [color=blue]
                  >
                  > Ken
                  >
                  > "Geoff Berrow" <blthecat@ckdog .co.uk> wrote in message
                  > news:ep9170tjng 3ovk17uv7le6p8q dthibj7p0@4ax.c om...[color=green]
                  > > I noticed that Message-ID:
                  > > <Nl1cc.13391$YC 5.9800@twister. rdc-kc.rr.com> from Ken contained the
                  > > following:
                  > >[color=darkred]
                  > > >
                  > > >mysql_query("I NSERT INTO tablename (website)
                  > > >Values('websit e_es')"); echo "Data Transfer Complete";[/color]
                  > >
                  > > Where are the values coming from?
                  > >
                  > > --
                  > > Geoff Berrow (put thecat out to email)
                  > > It's only Usenet, no one dies.
                  > > My opinions, not the committee's, mine.
                  > > Simple RFDs http://www.ckdog.co.uk/rfdmaker/[/color]
                  >
                  >[/color]
                  -----BEGIN PGP SIGNATURE-----
                  Version: GnuPG v1.2.4 (GNU/Linux)

                  iD8DBQFAcP10mhQ sAkXAJP0RAn9VAK C35RTxWrv4FSLKL 5ieNogdE0ZE5gCe IcPT
                  8H0IWyg4qevhiZJ 5uGJkF2c=
                  =gEx5
                  -----END PGP SIGNATURE-----

                  Comment

                  • Ken

                    #10
                    Re: Database

                    More information on the problem:

                    I am now up to id =411

                    -------------------------------
                    I just had a break through on determining what code is causing the duplicate
                    data.

                    If you run the code listed below without the session_start() ; a single set
                    of data is transferred.

                    If you add: session_start() ; to the code under the <?php and run the
                    page once, a single set of data is transferred.

                    Click on edit in the editor and then run, two sets of data are transferred.
                    Repeat the edit and run, two sets of data is transferred. Interesting!!!!

                    Summary: the results is caused by the session_start() ; code.
                    Interesting!!!!

                    Has anyone else experience this error?

                    Suggestions?
                    -------------------------------------
                    "Ken" <kkrolski@wi.rr .com> wrote in message
                    news:Nl1cc.1339 1$YC5.9800@twis ter.rdc-kc.rr.com...[color=blue]
                    > I have been working on this problem for a few days.
                    >
                    > When I write data into a sql database, the results are duplicate listings:
                    >
                    > PHP Code:
                    > <?php
                    > session_start() ;
                    > $dbh=mysql_conn ect ("localhost" , "username", "password") or die ('Cannot
                    > connect to the database: ' . mysql_error());
                    > mysql_select_db ("database name") or die ("Cannot locate database:
                    > ".mysql_error() );
                    >
                    > mysql_query("IN SERT INTO tablename (website) Values('website _es')");
                    > echo "Data Transfer Complete";
                    > ?>
                    >
                    > After emptying the table and running the PHP script:
                    > The results in the database:
                    > id website
                    > 130 website_es
                    > 131 website_es
                    >
                    > As you can see, I have been working on this for some time, id=131.
                    >
                    > Any suggestions for stopping the insertion of the duplicate rows?
                    >
                    > Thanks.
                    >
                    > Ken
                    >
                    >[/color]


                    Comment

                    • Geoff Berrow

                      #11
                      Re: Database

                      I noticed that Message-ID: <aU7cc.65711$4B 1.55565@twister .rdc-kc.rr.com>
                      from Ken contained the following:
                      [color=blue]
                      >I just had a break through on determining what code is causing the duplicate
                      >data.
                      >
                      >If you run the code listed below without the session_start() ; a single set
                      >of data is transferred.
                      >
                      >If you add: session_start() ; to the code under the <?php and run the
                      >page once, a single set of data is transferred.
                      >
                      >Click on edit in the editor and then run, two sets of data are transferred.
                      >Repeat the edit and run, two sets of data is transferred. Interesting!!!!
                      >
                      >Summary: the results is caused by the session_start() ; code.
                      >Interesting!!! ![/color]

                      Eh?

                      It sounds like your editor is running the code. Try adding a button and
                      a form and making the insert conditional on pressing the button.

                      --
                      Geoff Berrow (put thecat out to email)
                      It's only Usenet, no one dies.
                      My opinions, not the committee's, mine.
                      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                      Comment

                      • Garp

                        #12
                        Re: Database


                        "Ken" <kkrolski@wi.rr .com> wrote in message
                        news:RG3cc.4202 6$z%1.40647@twi ster.rdc-kc.rr.com...
                        <snip>[color=blue]
                        > I was going to use phpmyadmin but the readme said the script must be in[/color]
                        the[color=blue]
                        > root directory and I did not want it intermixed with my code.
                        >[/color]
                        <snip>

                        What? Where does it say that? That's not the case (why would it be?). You
                        might have read "root user" for the database and got it mixed up. I'm sure
                        it's installable anywhere you see fit.

                        I can't praise phpMyAdmin enough for day-to-day MySQL administration
                        (properly configured).

                        Garp


                        Comment

                        • Ken

                          #13
                          Re: Database

                          Doesn't it say that phpMyAdmin must be installed in the root directory?

                          Ken

                          "Garp" <garp7@no7.blue yonder.co.uk> wrote in message
                          news:K7gcc.3357 $jU6.32324838@n ews-text.cableinet. net...[color=blue]
                          >
                          > "Ken" <kkrolski@wi.rr .com> wrote in message
                          > news:RG3cc.4202 6$z%1.40647@twi ster.rdc-kc.rr.com...
                          > <snip>[color=green]
                          > > I was going to use phpmyadmin but the readme said the script must be in[/color]
                          > the[color=green]
                          > > root directory and I did not want it intermixed with my code.
                          > >[/color]
                          > <snip>
                          >
                          > What? Where does it say that? That's not the case (why would it be?). You
                          > might have read "root user" for the database and got it mixed up. I'm sure
                          > it's installable anywhere you see fit.
                          >
                          > I can't praise phpMyAdmin enough for day-to-day MySQL administration
                          > (properly configured).
                          >
                          > Garp
                          >
                          >[/color]


                          Comment

                          Working...