Page 1 of 1

I can´t seem to find the error in this code.

Posted: Wed Oct 16, 2002 2:27 pm
by Owe Blomqvist
Hi.
I´m new to this board and php, but i allways read it for answers when i get stuck in php.
Anyhow, I have this problem wich i´ve been trying to solve for the last week without any succes.
I made this script that compares 2 dates to each other to output the sum of those two into a column in a mysql database.

$row[6] = registrered date.
$row[7] = registrered date + 10 days.
$row[9] = remaining days

When i run the code below i get this output:

Name: name1
Before Update: 0
Patch Status: UPDATE `table` SET status=10
After Update: 5

Name: name2
Before Update: 0
Patch Status: UPDATE `table` SET status=10
After Update: 5

Name: name3
Before Update: 0
Patch Status: UPDATE `table` SET status=5
After Update: 5

Everything seems fine except putting the correct values in $row[9].
I hope you php gurus can help me with this one.
I can´t seem to find an answer anywhere in my php books nor php forums.
Thank you for your time & sorry about the length of this thread.
/Owe Blomqvist

Here´s the code.

Code: Select all

<?
		$link = mysql_connect ("host", "user", "pass");
		$database =	mysql_select_db ("database");
		$sql = "SELECT * FROM table";
		$result = mysql_query($sql);
			
			for($i=0; $i <=$result; $i++)
			{
			$row = mysql_fetch_row($result);
			$difference = ($rowї'7'] - $rowї'6']);
			$sum = ($difference - ($difference % 86400)) / 86400;
			$patch_status = "UPDATE `table` SET status=$sum";
			$do_patch = (mysql_query($patch_status));

					if(!$do_patch)
						{
						echo "Hmmm. No workie";
						return false;
						}
mysql_query($do_patch);
			
 			echo 	"Name: $rowї1]<br>
					 Before Update: $rowї9]<br>
					 Patch Status: $patch_status<br>
					 After Update: $rowї9]<br><br>";
			}
?>

row[9] is status?

Posted: Wed Oct 16, 2002 2:58 pm
by phpScott
Try putting your $sum is single quotes
'$sum' and see what happens.

phpScott

Posted: Wed Oct 16, 2002 3:03 pm
by Owe Blomqvist
Thanks for the reply, but that didn´t help.

Posted: Wed Oct 16, 2002 4:17 pm
by Coco
$difference = ($row['7'] - $row['6']);
try putting the numbers without (')s since they are numbers not strings

Posted: Wed Oct 16, 2002 4:29 pm
by Owe Blomqvist
Thanks, but that didn´t do it either.
It seems the code adds the first value (5) to all rows.

Posted: Wed Oct 16, 2002 4:35 pm
by Coco
mysql_query($do_patch);

==

mysql_query(mysql_query($patch_status));

since

$do_patch = (mysql_query($patch_status));


now i might be going crazy, and i am definately quite a newbie when it comes to this sort of thing, but im assuming that would give you the funny answer you get


BESIDES WHICH

you are saying that this:

Code: Select all

<?php
          echo    "Name: $rowї1]<br> 
                Before Update: $rowї9]<br> 
                Patch Status: $patch_status<br> 
                After Update: $rowї9]<br><br>";
?>
gives 2 different values for $row[9]

Posted: Wed Oct 16, 2002 4:52 pm
by Owe Blomqvist
you are saying that this:

Code: Select all

<?php
          echo    "Name: $rowї1]<br> 
                Before Update: $rowї9]<br> 
                Patch Status: $patch_status<br> 
                After Update: $rowї9]<br><br>";
?>
gives 2 different values for $row[9]
No, i´m saying, the code writes the first "$patch_status" value (5) into all rows.

You´re right about executing the mysql_query twice.
I fixed it but it still won´t work. :cry:

Posted: Thu Oct 17, 2002 2:27 am
by twigletmac
You need to make a change to your update statement - as you've found out just doing:

Code: Select all

UPDATE table SET column1='value'
sets every single column1 field to be value. You need to add a where statement to limit it to just the record that you're working on. If you had an ID field in the database this is what I would use (any unique value will do):

Code: Select all

UPDATE table SET column1='value' WHERE ID='current_record_id'
Have a look at http://www.mysql.com/doc/en/UPDATE.html for more info.

Mac

Posted: Thu Oct 17, 2002 9:06 am
by Owe Blomqvist
Thank You so very much.
it worked right away (I had the unique 'ID' column).
A Huge hug to you guys for taking your time to look into my problem.
Thanks again.
/Best Regards,
Owe Blomqvist.