1

I'm trying to do a mysql_query(); and if that's successful I wan't to do another one, which has to update a field in another table.

My mysql_query(); code looks like this:

$query = mysql_query($sql)or die(mysql_error());
    if($query){
        $sqlU = mysql_query("UPDATE lookup SET lookupStatus=1 WHERE lookupId=".(int)$post['FK_lookupId']."");
        return true;
    }

The $sql which is not provided in the question is just a simple INSERT statement.

The $sqlU UPDATE statement is not even run.

P.S: I think the problem is that it's running through a jQuery AJAX, and therefore when the first mysql_query(); returns true, it's cutting off the rest of my PHP.

My jQuery:

$('#lookupAnswerGo').submit(function() {
var dataString = $(this).serialize();

$.ajax({  
    type: "POST",  
    url: "/includes/classes/handler.php?do=addLookupAnswer",
    data: dataString,
    success: function(returnedData){

        if(returnedData){
            $('.errorMessage').fadeIn().html(returnedData);
        } else {
            window.location.href = "";
        }

    }
  });
  return false;
});

If I'm right about the problem is AJAX, then how do I prevent it from cutting off my PHP?

4
  • AJAX doesn't 'cut off PHP', it just requests a page from the webserver just like any browser would. Commented Feb 27, 2012 at 10:12
  • Call your php-file directly (without using AJAX) and watch the result. Use 'print_r()' or 'echo' to get the content of '$query' Commented Feb 27, 2012 at 10:30
  • I just thought that AJAX could determine if the query was succesful and therefore would "jump" out of my function. Commented Feb 27, 2012 at 10:43
  • no, AJAX can't do that, it has no "special powers", it acts the same as a browser - PHP is run on the server and content is returned... Commented Feb 27, 2012 at 11:13

1 Answer 1

1

Maybe you could try a more robust version of how to check if the first query ran, something like this:

  $query = mysql_query($sql)or die(mysql_error());
  if( mysql_affected_rows() > 0 )
  {
    $sqlU = mysql_query("UPDATE lookup SET lookupStatus=1 WHERE lookupId=".(int)$post['FK_lookupId']."");
    return true;
  }

There is no reason why you should consider this a problem of the AJAX call. The execution of the php script only ends with php statements - i.e. return or die. AJAX itself can't determine if a query if finished or not (unless you provide an appropriate statement in php).

Sign up to request clarification or add additional context in comments.

1 Comment

I'll try this when I'll get back to my computer. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.