Incorrect value is saved to MySQL DB from PHP code

72 views Asked by At
$query="SELECT MINUTE (ETA - STA) 
FROM `FlightSchedule` WHERE `flightNum_arr`='".$flightNum_arr."';";

$delay=ejecutar_query($query);

$query = "UPDATE `FlightSchedule` 
SET `delay`='".$delay."' 
WHERE `flightNum_arr`='".$flightNum_arr."';";

$result=ejecutar_query($query);

Instead of saving the minutes values, it saves Resource id#. How to fix this issue?

2

There are 2 answers

3
som On BEST ANSWER

You have to fetch data using

$row = ejecutar_fetch_assoc($delay); // something like that but not sure

Modify your update query.

$query = "UPDATE `FlightSchedule` 
SET `delay`='".$row['delay']."' 
WHERE `flightNum_arr`='".$flightNum_arr."';";

Also modify select query little bit.

$query="SELECT MINUTE (ETA - STA) AS delay
FROM `FlightSchedule` WHERE `flightNum_arr`='".$flightNum_arr."';";
6
Fluffeh On

In your code, $delay is not the data in the row, but the entire object that represents the results.

You could try to do it in one statement like this however:

update `FlightSchedule` set delay=(SELECT MINUTE (ETA - STA) 
FROM `FlightSchedule` WHERE `flightNum_arr`='".$flightNum_arr."');";

Edit:

Try this instead to get around the silly lock that MySQL does :)

update `FlightSchedule` set delay=(select * from (SELECT MINUTE (ETA - STA) 
FROM `FlightSchedule` WHERE `flightNum_arr`='".$flightNum_arr."'));";

Edit 2: (aka I should test my queries rther than just scribbling them off the top of my head...)

mysql> select * from updatetest;
+------+------+
| var1 | var2 |
+------+------+
|  450 |    1 |
|  100 |    5 |
+------+------+
2 rows in set (0.00 sec)

mysql> update
    updatetest set var2=
        (select * from
            (select var2 from updatetest where var1=100)
        updater);
Query OK, 1 row affected (0.00 sec)
Rows matched: 2  Changed: 1  Warnings: 0

mysql> select * from updatetest;
+------+------+
| var1 | var2 |
+------+------+
|  450 |    5 |
|  100 |    5 |
+------+------+
2 rows in set (0.00 sec)