Jump to content

[SOLVED] PHP not updating database when called via Ajax.... but no errors reported!


olivertreend

Recommended Posts

Hey

I hope you can help me with this problem...

 

Basically, I have this page with a form on it.

For example:

<form action="update.php?id=1" method="post">
<input type="text" name="price" />
</form>

(That is a very cut down version of the form to try and get my idea across - I'm never good with explaining stuff!)

 

update.php basically reads the posted variables and updates a MySQL record (in this case, the record with ID = 1 which was defined in the form request filename).

 

This works perfectly when posted to via the HTML form.

 

However, if Javascript is enabled, update.php is posted to via an Ajax call. I have done some tests on the request, and I know that it successfully posting all the right variables and what not. Oh, and the file name posted to is "update.php?id=1&ajax=true", not that it should make any difference.

 

But when I try posting the form via Ajax, and therefore updating the MySQL database, the PHP script comes back claiming that everything worked and the request was successful. However, the MySQL record is never updated when called via Ajax!

 

I have even enabled error reporting and used "$result = mysql_query($query) or die(mysql_error());" to no avail.

 

I'm really confused, because the request works fine without Ajax!

 

Then I tried dropping the "&ajax=true" parameter at the end of the file name request to make it look as if it was being called without Ajax. This made no difference whatsoever.

 

What's more confusing, is that I can successfully do SELECT commands and get results from the database via Ajax. But UPDATE commands don't work!

 

I'm totally stumped on this one - I have no idea what is going wrong.

I hope I have explained the problem clearly enough.

 

All help is greatly appreciated!!

If you post your code for update.php, you can probably get an answer faster.

 

The or die() logic only does something if the function call returned a FALSE value, meaning that the query failed due to a problem with the database server, the database, or a syntax error. An UPDATE query that matches no rows or does not change any values is a successful query (returns a TRUE value.)

Aah! Thanks for your help!

Checking through my code... it seems that it was my mistake all along.

 

The javascript code I was using to grab the value of the form field was:

var price = document.getElementById("price"+id).getAttribute("value");

 

I should have used:

var price = document.getElementById("price"+id).value;

 

because the javascript was grabbing the *default* value of that form field at page load, rather than the current contents of that field, which meant the database never seemed to be updated!

 

But thanks a lot for your help.

Now how do I mark this post as resolved??  ???

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.