View Full Version : MySQL Async problem?

06-08-2007, 09:22 AM
If I'm in a javascript function, and I do the xmlHttp stuff (ajax) to summon some php code that does an SQL UPDATE, how do I know when the update has completed? The problem is I'm doing an SQL SELECT to update the screen after the SQL UPDATE, and it shows the old values but evidently, the UPDATE isn't done yet. (If I stick an Alert("bla bla bla) between ajax calls to the php/sql functions, it works fine).

Can I check for SQL UPDATE completion? Can I call SQL UPDATE with SYNCH instead of ASYNC? If you can explain this to me there's no need to read the rest of this. It just goes into some more detail of what I'm doing.


I've got a list filled with row data from a db table. Next to the list are a bunch of fields (details of the selected entry). You can edit the fields, hit the submit button, and your changes get recorded. A form/post deal is not used - instead it's AJAX'ed for a nice clean flicker free look.

The sequence of events are...

1- User edits the fields
2- User hits the Submit button
3- The Submit's "onClick" sends to the javascript function below...

function UpdatePlayer()
savedRow = gLastSelectedRow;
firstname = document.getElementById("id_firstname").value;
lastname = document.getElementById("id_lastname").value;
playerid = document.getElementById("id_player_id").value;

(NOTE: There are a bunch of other fields but for clarity I'll just show the firstname, lastname, and playerid)


The DoAJAX function does the xmlHttp stuff and passes the field data on to the php code (player_update.php) where the actual sql is done to update the table ...

$sql = "UPDATE players
SET firstname = '$fname', lastname = '$lname',
WHERE id = $playerid";

$result = mysql_query($sql);

It all works great except for one problem...
...going back to the javascript code above...


The RebuildPlayerList() function not only redisplays the list, but calls other functions to populate the details (the fields to the right of the list). What I am seeing is that after the SQL Update is called, the old values will be displayed (over the changes you just made) even though the database is updated properly. If I stick an Alert("bla bla bla); after the DoAJAX call, all looks fine.

06-08-2007, 10:23 AM
OooooK.... I've been doing some AJAX reading and I think I understand the xmlhttp.open function a bit better ... the difference between GET and POST and the all important 3rd arguement, ASYNCH = true/false... but if I set AYNCH to false I still have the same problem. Hmmmm.

06-08-2007, 10:45 AM
So the function I set for "xmlHttp.onreadystatechange" should tell me the state - when it's completed. but why didn't the asynch=false flag work? just curious. anyway, it's time for bed. i guess there's enough documentation out there on this topic to figure it out. i'll try using the xmlHttp.onreadystatechange next to figure out when to proceed after the SQL UPDATE with the SQL SELECT. Sorry for taking up space having this conversation with myself. If anyone reading this has anything else to add, please do.