Page 1 of 2

code not working, PHP says it is!!!

Posted: Wed Mar 10, 2004 2:54 am
by malcolmboston
ok, its a simple PHP script to insert data based on a condition being met

for some godforesaken reason that i cant see it wont work, if anyone can see an error here (which i cant) then please share it

i even call affected_rows() which returns a value of 1, so it is working#

but when i go into MySQLFront and PHPmyAdmin both havent changed

any ideas?

Code: Select all

<?php
require ("dbhandler.php");
// ill create a dummy var just so the script thinks it's set
$add_article = "Yes";
// first we'll run a query to find out the value of the items in the database
$query = "SELECT Articles, Downloads, Comments FROM stats" or die(mysql_error());
$statvalues = mysql_query($query);
$values = mysql_fetch_array($statvalues, MYSQL_ASSOC) or die(mysql_error());
// now lets make the newly created variables look nicer
$old_articles = $values['Articles'];
$old_downloads = $values['Downloads'];
$old_comments = $values['Comments'];
// now we need to add 1 to the current value and then reinsert it into the database
// lets create a function so i dont have to constantly write out the same script for each
// condition
if (isset($add_article))
{
	// this is run only if someone has added an article to the database
	$new_article_result = ($old_articles + 1);
	// now we have the new value we will run an update statement
	$update = "UPDATE stats SET Articles='$new_article_result'  WHERE Articles='$old_articles' AND Downloads='$old_downloads' AND Members=$old_members AND Comments=$old_comments'" or die (mysql_error());
	$affectedrows = mysql_affected_rows();
	print "Added Successfully!\n The New Value of Articles Is --> $new_article_result ";

// this script is not working for some stupid reason, MYSQL
// is not throwing up an error and my conditional print statement is printing
// it even tells me how many rows it has just affected (1)
// however when i check the database its not updated it
// any ideas?
}
else 
{
	print " var not set";
}

?>
<?php
print "
Old Articles = $old_articles\n
Old Downloads = $old_downloads\n
Old Comments = $old_comments\n
New Articles = $new_article_result\n
The query has affected $affectedrows rows
"
?>

Posted: Wed Mar 10, 2004 3:08 am
by JayBird
are you sure your checking in the correct place in MySQLFront and PHPmyAdmin? I have done that before, looking for updated data in the wrong DB or table.

May sound stupid but worth double checking cod your code is definatley fine from what i can see, and if it is saying the rows are updated.

Also, are you sure the data been updated, is not the same as the data already in the DB?

Mark

Posted: Wed Mar 10, 2004 3:10 am
by malcolmboston
yeah im looking at the correct database

ive refreshed it like at least 200+ times and viewed it in both programs/scripts MySQL Front and PHPmyAdmin

i have the default fields set to 0 but that has nothing to do with it
also the feels are 'unsigned' whatever that means however that is default and ive never changed it before (or this time) so i cant see that being the problem

Posted: Wed Mar 10, 2004 3:14 am
by Wayne
dont know if this has anything to do with it,.... but ... you have a ' at the end of $old_comments but no matching 1 at the beginning

Code: Select all

$update = "UPDATE stats SET Articles='$new_article_result'  WHERE Articles='$old_articles' AND Downloads='$old_downloads' AND Members=$old_members AND Comments=$old_comments'" or die (mysql_error());

Posted: Wed Mar 10, 2004 3:16 am
by JayBird
actually, where are you actually executing the Update Query? i can't see it.

Mark

Posted: Wed Mar 10, 2004 3:18 am
by malcolmboston
$update = "UPDATE stats SET Articles='$new_article_result' WHERE Articles='$old_articles' AND Downloads='$old_downloads' AND Members=$old_members AND Comments=$old_comments'" or die (mysql_error());
im assuming seeing as you can do

Code: Select all

$query = "Select * from wherever";
you can use

Code: Select all

$update = "UPDATE stats SET Articles='$new_article_result'  WHERE Articles='$old_articles' AND Downloads='$old_downloads' AND Members=$old_members AND Comments=$old_comments'" or die (mysql_error());
[/b]

Posted: Wed Mar 10, 2004 3:21 am
by JayBird
but it ain't getting executed!?

Your just assigning a string to a variable

on your select query, you set the query string, then execute straight after like this

Code: Select all

$query = "SELECT Articles, Downloads, Comments FROM stats" or die(mysql_error()); 
$statvalues = mysql_query($query);
Mark

Posted: Wed Mar 10, 2004 3:23 am
by malcolmboston
o ffs

let this be a lesson to you boys and girls

DO NOT stay up all night coding :roll:

Posted: Wed Mar 10, 2004 3:23 am
by malcolmboston
ANYWAY

why the hell is affected_rows giving me back a 1 value?

therefore mysql is doing something!?

Posted: Wed Mar 10, 2004 3:32 am
by JayBird
did it return 1 or -1?

Mark

Posted: Wed Mar 10, 2004 3:33 am
by malcolmboston
it returns 1

definitely 1, everytime

Posted: Wed Mar 10, 2004 3:37 am
by JayBird
how many rows does...

Code: Select all

"SELECT Articles, Downloads, Comments FROM stats"
...return?

mysql_affected_rows(); tells you the affected rows on the last query run, ehich in your case, is the query above.

Mark

Posted: Wed Mar 10, 2004 3:38 am
by malcolmboston
1

because there is only one row

DOWNLOADS - ARTICLES - MEMBERS - CONTENT
0 0 0 0

no more in that table

Posted: Wed Mar 10, 2004 3:40 am
by JayBird
so thats why it was returning 1, because that was the last query that was actually executed!

Even though in the manual it says that mysql_affected_rows(); does not work with SELECT statements, i just tried it and it does!?!?

Mark

Posted: Wed Mar 10, 2004 3:41 am
by malcolmboston
:?

not in my experience with PHP

always throws up an error (not a resource blah blah blah)

i am pretty sure it doesnt, thats y mysql_num_rows() is there :?