...

View Full Version : Should only be adding +1 but is adding more..



cyphix
09-21-2005, 07:47 PM
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..... :confused:



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();

}

nikkiH
09-21-2005, 10:22 PM
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.

e-Raser
09-22-2005, 10:17 AM
You could just +1 to the db straight away.

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();

}

cyphix
09-22-2005, 12:41 PM
Thanks E-raser; I'll try that! :thumbsup:


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.

cyphix
09-22-2005, 03:04 PM
Well I've tried everything & I cannot work out for the life of me why it is doing that!!?? :confused: :confused: :confused:

nikkiH
09-22-2005, 03:20 PM
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.

cyphix
09-22-2005, 03:52 PM
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".

cyphix
09-23-2005, 02:47 PM
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..



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



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



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? :confused:

NancyJ
09-23-2005, 03:16 PM
your script is very repetative :- try this:


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

cyphix
09-23-2005, 07:06 PM
Unfortunately that didn't solve anything.... :(

NancyJ
09-23-2005, 08:03 PM
Unfortunately that didn't solve anything.... :(
Then your issue is most likely elsewhere

missing-score
09-24-2005, 01:07 AM
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.

cyphix
09-24-2005, 12:15 PM
Hmmmm..... well my web host is www.webair.com & I run on a dedicated server if that matters.

petronel
09-24-2005, 04:51 PM
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.

cyphix
09-24-2005, 06:28 PM
I is posible that google adsense to request also the page one time?

You my friend are a legend!!!!!!! :D Removed both google ad's & now the issue is resolved! :thumbsup: :thumbsup: :thumbsup:

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

NancyJ
09-24-2005, 11:28 PM
You my friend are a legend!!!!!!! :D Removed both google ad's & now the issue is resolved! :thumbsup: :thumbsup: :thumbsup:

Question is..... how do I go about still using the google ads without having this issue? :confused:
move your processing code to a dedicted processing page, then use location headers to redirect your users, then you dont need the adsense stuff on the pages that connect and do any db inserts



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum