code not working, PHP says it is!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

code not working, PHP says it is!!!

Post 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
"
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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());
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

actually, where are you actually executing the Update Query? i can't see it.

Mark
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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]
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

o ffs

let this be a lesson to you boys and girls

DO NOT stay up all night coding :roll:
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

ANYWAY

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

therefore mysql is doing something!?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

did it return 1 or -1?

Mark
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

it returns 1

definitely 1, everytime
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

1

because there is only one row

DOWNLOADS - ARTICLES - MEMBERS - CONTENT
0 0 0 0

no more in that table
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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 :?
Post Reply