Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post

    Should only be adding +1 but is adding more..

    Hey guys... I'm about to pull my hair out here; I've got the code below & I'm sure you can see what it is supposed to do.... but instead of making a new entry with the number "1" in the record or adding +1 to the current result it is adding pretty much what it feels like... sometimes it makes a record starting with the number 2 or 3 or even 4... it even adds on the amounts also.

    I have no idea why this is happening.....

    PHP Code:
            if ($search_method == "title") {

    mysql_connect($dbhost$dbuser$dbpass) or die("Couldn't connect to server.");
    @
    mysql_select_db($dbname) or die("Couldn't connect to database.");

    $fquery="SELECT id,amount FROM no_results WHERE type='title' AND term='$terms' LIMIT 1";
    $fsql=mysql_query($fquery);
    $fnum=mysql_num_rows($fsql);
    $frow=mysql_fetch_row($fsql);

                if (
    $fnum 0) {

    // Add to amount
    $new_amount $frow[1] + 1;

    $fquery="UPDATE no_results SET amount='$new_amount' WHERE id='$frow[0]' LIMIT 1";
    mysql_query($fquery);

                } else {

    // Add new record

    $fquery="INSERT INTO no_results(term,type,amount) VALUE('$terms','title','1')";
    mysql_query($fquery);

                }

    mysql_close();

            } elseif (
    $search_method == "artist") {

    mysql_connect($dbhost$dbuser$dbpass) or die("Couldn't connect to server.");
    @
    mysql_select_db($dbname) or die("Couldn't connect to database.");

    $fquery="SELECT id,amount FROM no_results WHERE type='artist' AND term='$terms' LIMIT 1";
    $fsql=mysql_query($fquery);
    $fnum=mysql_num_rows($fsql);
    $frow=mysql_fetch_row($fsql);

                if (
    $fnum 0) {

    // Add to amount
    $new_amount $frow[1] + 1;

    $fquery="UPDATE no_results SET amount='$new_amount' WHERE id='$frow[0]' LIMIT 1";
    mysql_query($fquery);

                } else {

    // Add new record

    $fquery="INSERT INTO no_results(term,type,amount) VALUE('$terms','artist','1')";
    mysql_query($fquery);

                }

    mysql_close();

            } 

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    I'm confused.
    Is the amount a number or a string?
    You keep putting quotes around it like it's a string, but then you add 1 to it like it's a number.
    I'm still a php newbie myself, but other languages might do odd things when you add a number to a string.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New Coder
    Join Date
    Jul 2005
    Location
    Perth, Australia. Age: 13
    Posts
    81
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could just +1 to the db straight away.
    PHP Code:
    if ($search_method == "title") {

    mysql_connect($dbhost$dbuser$dbpass) or die("Couldn't connect to server.");
    @
    mysql_select_db($dbname) or die("Couldn't connect to database.");

    $fquery="SELECT id,amount FROM no_results WHERE type='title' AND term='$terms' LIMIT 1";
    $fsql=mysql_query($fquery);
    $fnum=mysql_num_rows($fsql);
    $frow=mysql_fetch_row($fsql);

                if (
    $fnum 0) {

    // Add to amount
    $fquery="UPDATE no_results SET amount='$new_amount' WHERE id='$frow[0]' LIMIT 1";
    mysql_query($fquery);

                } else {

    // Add new record

    $fquery="INSERT INTO no_results(term,type,amount) VALUE('$terms','title','1')";
    mysql_query($fquery);

                }

    mysql_close();

            } elseif (
    $search_method == "artist") {

    mysql_connect($dbhost$dbuser$dbpass) or die("Couldn't connect to server.");
    @
    mysql_select_db($dbname) or die("Couldn't connect to database.");

    $fquery="SELECT id,amount FROM no_results WHERE type='artist' AND term='$terms' LIMIT 1";
    $fsql=mysql_query($fquery);
    $fnum=mysql_num_rows($fsql);
    $frow=mysql_fetch_row($fsql);

                if (
    $fnum 0) {

    // Add to amount

    $fquery="UPDATE no_results SET amount='amount+1' WHERE id='$frow[0]' LIMIT 1";
    mysql_query($fquery);

                } else {

    // Add new record

    $fquery="INSERT INTO no_results(term,type,amount) VALUE('$terms','artist','1')";
    mysql_query($fquery);

                }

    mysql_close();

            } 
    Last edited by e-Raser; 09-22-2005 at 10:22 AM.
    I lost my signature, have you seen it?
    I have the power to erase you...

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks E-raser; I'll try that!

    Quote Originally Posted by nikkiH
    I'm confused.
    Is the amount a number or a string?
    You keep putting quotes around it like it's a string, but then you add 1 to it like it's a number.
    I'm still a php newbie myself, but other languages might do odd things when you add a number to a string.
    Hmmmmm..... good point, I just have got into the habit of always using single quotes around all my database INSERT's to help prevent SQL injections.

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Well I've tried everything & I cannot work out for the life of me why it is doing that!!??

  • #6
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Well, when I can't figure out why something is happening, I set up echo statements all over to trace what is going on, the values of variables (including ones I "assumed" would be a certain value, and so on.
    Put echo (print) statements after each if/else and in any loops to see what order things are being run in, where execution is going, and so on.
    Also echo out your sql string to see what is really being executed.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #7
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Ok.... more weird reports now..

    As for the INSERT's..

    When I remove the $terms variable from the input & instead input a word directly it will insert the record correctly with the correct "1" result; however, it will make duplicate records... so instead of making one record it will make 2 duplicate records...

    Nikki... yes, I have tried echoing out things but nothing I tried seems out of place or wrong.

    I echoed out my SQL statement of the INSERT & the output I got was "1".

  • #8
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Ok.....I'm starting to narrow it down even more; however it keeps getting weirder & weirder..

    Here is my complete code that executes inside the if statement that contains this problem..

    PHP Code:
            if ($search_method == "title") {

    mysql_connect($dbhost$dbuser$dbpass) or die("Couldn't connect to server.");
    @
    mysql_select_db($dbname) or die("Couldn't connect to database.");

    $fquery="SELECT id,amount FROM no_results WHERE type='title' AND term='$terms' LIMIT 1;";
    $fsql=mysql_query($fquery) or die( 'QUERY ERROR:' mysql_error()  );
    $fnum=mysql_num_rows($fsql);
    $frow=mysql_fetch_row($fsql);

                if (
    $fnum 0) {

    // Add to amount
    $new_amount $frow[1] + 1;

    $fquery="UPDATE no_results SET amount=$new_amount WHERE id=$frow[0] LIMIT 1;";
    mysql_query($fquery) or die( 'QUERY ERROR:' mysql_error() );

                } else {

    // Add new record

    $fquery="INSERT INTO no_results (term,type,amount) VALUES('$terms','title',1);";
    mysql_query($fquery) or die( 'QUERY ERROR:' mysql_error() );

                }

    mysql_close();

            } elseif (
    $search_method == "artist") {

    mysql_connect($dbhost$dbuser$dbpass) or die("Couldn't connect to server.");
    @
    mysql_select_db($dbname) or die("Couldn't connect to database.");

    $fquery="SELECT id,amount FROM no_results WHERE type='artist' AND term='$terms' LIMIT 1;";
    $fsql=mysql_query($fquery) or die( 'QUERY ERROR:' mysql_error() );
    $fnum=mysql_num_rows($fsql);
    $frow=mysql_fetch_row($fsql);

                if (
    $fnum 0) {

    // Add to amount
    $new_amount $frow[1] + 1;

    $fquery="UPDATE no_results SET amount=$new_amount WHERE id=$frow[0] LIMIT 1;";
    mysql_query($fquery) or die( 'QUERY ERROR:' mysql_error() );

                } else {

    // Add new record

    $fquery="INSERT INTO no_results (term,type,amount) VALUES('$terms','artist',1);";
    mysql_query($fquery) or die( 'QUERY ERROR:' mysql_error() );

                }

    mysql_close();

            }

    echo 
    "<center>\n";
    echo 
    "<font class=\"b2text\">Sorry, no results were returned.</font>\n";
    echo 
    "<p>\n";

            if (
    $search_method == "title") {
    echo 
    "<font class=\"sbtext\">Try using the \"Partial Search\" type instead or altering your query slightly to find the music you're searching for.<p>You may also want to browse through the listings with the navigation links to the left of every page.</font>\n";
            } elseif (
    $search_method == "artist") {
    echo 
    "<font class=\"sbtext\">Try using the \"Partial Search\" type instead or altering your query slightly to find the music artist you're searching for.<p>You may also want to browse through the listings with the navigation links to the left of every page.</font>\n";
            } elseif (
    $search_method == "lyrics") {
    echo 
    "<font class=\"sbtext\">Try using the \"Partial Search\" type instead or altering your query slightly to find the music lyrics you're searching for.<p>You may also want to browse through the listings with the navigation links to the left of every page.</font>\n";
            }

    echo 
    "</center>\n";

    echo 
    "<br>\n";
    echo 
    "<br>\n";

    echo 
    "<center>\n";
    echo 
    "<script type=\"text/javascript\"><!--\n";
    echo 
    "google_ad_client = \"pub-**************\";\n";
    echo 
    "google_alternate_color = \"FFFFFF\";\n";
    echo 
    "google_ad_width = 300;\n";
    echo 
    "google_ad_height = 250;\n";
    echo 
    "google_ad_format = \"300x250_as\";\n";
    echo 
    "google_ad_type = \"text_image\";\n";
    echo 
    "google_ad_channel =\"*********\";\n";
    echo 
    "google_color_border = \"FFFFFF\";\n";
    echo 
    "google_color_bg = \"FFFFFF\";\n";
    echo 
    "google_color_link = \"0000FF\";\n";
    echo 
    "google_color_url = \"008000\";\n";
    echo 
    "google_color_text = \"000000\";\n";
    echo 
    "//--></script>\n";
    echo 
    "<script type=\"text/javascript\"\n";
    echo 
    "  src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">\n";
    echo 
    "</script>\n";
    echo 
    "</center>\n"
    Strange thing is.... if I "exit()" the script before this line..

    PHP Code:
    echo "<center>\n";
    echo 
    "<font class=\"b2text\">Sorry, no results were returned.</font>\n";
    echo 
    "<p>\n"
    ..the script works perfectly.

    If I exit it right at the end (after the google ad code) I still have the issue..... so then I decided to comment out all the code starting from the first line as below..

    PHP Code:
    echo "<center>\n";
    echo 
    "<font class=\"b2text\">Sorry, no results were returned.</font>\n";
    echo 
    "<p>\n"
    .. right to the end of the output & ran the script with no extra exit() calls.... & I still get the issue.

    So basically... script works if I call an exit before all that output & doesn't work if I call it after the output; but also doesn't work if I remove all the output & run the script normally....

    Is this weird or what?

  • #9
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    your script is very repetative :- try this:

    PHP Code:
    mysql_connect($dbhost$dbuser$dbpass) or die("Couldn't connect to server.");
    @
    mysql_select_db($dbname) or die("Couldn't connect to database.");

    $fquery="SELECT id,amount FROM no_results WHERE type='$search_method' AND term='$terms' LIMIT 1;";
    $fsql=mysql_query($fquery) or die( 'QUERY ERROR:' mysql_error()  );
    $frow=mysql_fetch_row($fsql);

                if (
    mysql_num_rows($fsql) > 0) {
    // Add to amount
    $fquery="UPDATE no_results SET amount=amount+1 WHERE id={$frow[0]} LIMIT 1;";
    mysql_query($fquery) or die( 'QUERY ERROR:' mysql_error() );
                } else {

    // Add new record
    $fquery="INSERT INTO no_results (term,type,amount) VALUES('$terms','$search_method',1);";
    mysql_query($fquery) or die( 'QUERY ERROR:' mysql_error() );

     }

    mysql_close();


    echo 
    "<center>\n";
    echo 
    "<font class=\"b2text\">Sorry, no results were returned.</font>\n";
    echo 
    "<p>\n";

    echo 
    "<font class=\"sbtext\">Try using the \"Partial Search\" type instead or altering your query slightly to find the music $search_method you're searching for.<p>You may also want to browse through the listings with the navigation links to the left of every page.</font>\n";
    echo 
    "</center>\n";

    echo 
    "<br>\n";
    echo 
    "<br>\n";

    echo 
    "<center>\n";
    echo 
    "<script type=\"text/javascript\"><!--\n";
    echo 
    "google_ad_client = \"pub-**************\";\n";
    echo 
    "google_alternate_color = \"FFFFFF\";\n";
    echo 
    "google_ad_width = 300;\n";
    echo 
    "google_ad_height = 250;\n";
    echo 
    "google_ad_format = \"300x250_as\";\n";
    echo 
    "google_ad_type = \"text_image\";\n";
    echo 
    "google_ad_channel =\"*********\";\n";
    echo 
    "google_color_border = \"FFFFFF\";\n";
    echo 
    "google_color_bg = \"FFFFFF\";\n";
    echo 
    "google_color_link = \"0000FF\";\n";
    echo 
    "google_color_url = \"008000\";\n";
    echo 
    "google_color_text = \"000000\";\n";
    echo 
    "//--></script>\n";
    echo 
    "<script type=\"text/javascript\"\n";
    echo 
    "  src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">\n";
    echo 
    "</script>\n";
    echo 
    "</center>\n"

  • #10
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Unfortunately that didn't solve anything....

  • #11
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    Quote Originally Posted by cyphix
    Unfortunately that didn't solve anything....
    Then your issue is most likely elsewhere

  • #12
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah I have to agree with Nancy... Where are you hosting this, is it with your webhost? I was with a very bad webhost once (I cant believe i actually paid them to host me for a while), and a script that was writing to a file was actually writing to the file twice instead of once... Nothing to do with my script, it just was (for no known reason).

    The problems you are experiencing are very very weird, and unless we are all missing something blindingly obvious, it is most likley to do with your hosts setup.

  • #13
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Hmmmm..... well my web host is www.webair.com & I run on a dedicated server if that matters.

  • #14
    New Coder
    Join Date
    Sep 2005
    Location
    Romania
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    hmm

    I is posible that google adsense to request also the page one time? i had some problems in the past because of google adsense.. try to make some changes to the script to log to a file requester IP / browser and the amount or $row[0].
    I'll try it on my server to see what it is happening.

  • #15
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    I is posible that google adsense to request also the page one time?
    You my friend are a legend!!!!!!! Removed both google ad's & now the issue is resolved!

    Question is..... how do I go about still using the google ads without having this issue?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •