Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts

    SQL Update - Success/Failure

    Was wondering if you guys might be able to to help me out.

    Currently stuck with part of a User Activation set up i have.

    PHP Code:
    if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "stage2b")) {
      
    $updateSQL sprintf("UPDATE accounts SET status=%s WHERE username=%s AND activate_code=%s",
                           
    GetSQLValueString($_POST['status'], "text"),
                           
    GetSQLValueString($_POST['username'], "text"),
                           
    GetSQLValueString($_POST['activatecode'], "text"));

      
    mysql_select_db($database_STC$STC);
      
    $Result1 mysql_query($updateSQL$STC) or die(mysql_error());

    //SUCCESS
      
    if ($Result1 mysql_query($updateSQL$STC)) { 
            
    $updateGoTo "regsystem.php?go=2complete"; } 
             
    //FAILURE
             
    else $updateGoTo "regsystem.php?go=2failure";
      if (isset(
    $_SERVER['QUERY_STRING'])) 
      
    header(sprintf("Location: %s"$updateGoTo)); 
    Basically i am trying to go to "2complete" if the UPDATE is a success - and go to "2failure" if it does not work.

    Anybody got any ideas? I've had a look around these forums and been playing with it for a while - would be very great full to anybody would might be able to help!

    NOTE: the script works fine whenever the right details are entered - however displays the Success page even when it doesn't update the account...
    Last edited by weir-07; 03-29-2009 at 03:32 AM. Reason: RESOLVED

  • #2
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Take out the $Result1 part. The function mysql_query returns a boolean, TRUE if it was a success and FALSE if it was a failure.

    Exampe:

    PHP Code:
    <?php

    if( $mysql_query$updateSQL$STC ) ){
         
    $updateGoTo "regsystem.php?go=2complete";
    }else{
         
    $updateGoTo "regsystem.php?go=2failure";
    }

    ?>
    What you are doing, though, is a bit redunant. In fact it may result in two queries being executed. Keep in mind that everytime you call mysql_query(), whether in an IF statement or not, it will execute that function.

    Try this:

    PHP Code:
    <?php

    if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "stage2b")) {
      
    $updateSQL sprintf("UPDATE accounts SET status=%s WHERE username=%s AND activate_code=%s",
                           
    GetSQLValueString($_POST['status'], "text"),
                           
    GetSQLValueString($_POST['username'], "text"),
                           
    GetSQLValueString($_POST['activatecode'], "text"));

      
    mysql_select_db($database_STC$STC);
      
    $Result1 mysql_query($updateSQL$STC) or die(mysql_error());

    //SUCCESS
      // $Result1 will have been given a bool value based on the execution of the last query
      
    if ($Result1 ) { 
            
    $updateGoTo "regsystem.php?go=2complete"; } 
             
    //FAILURE
             
    else $updateGoTo "regsystem.php?go=2failure";
      if (isset(
    $_SERVER['QUERY_STRING'])) 
      
    header(sprintf("Location: %s"$updateGoTo));  

    ?>
    Now having said that please be aware that in this script the $updateGoTo = "regsystem.php?go=2failure"; will never fire. That's because you have or die(mysql_error()); on your initial query. The OR statement is essentially saying "This returns true OR I do this." Take that out if you'd like $updateGoTo = "regsystem.php?go=2failure"; to fire when there is an error.

    Hope this helps! For more on the mysql_query and what is returns, Check out the PHP Manual.

    Edit: Added some examples
    Last edited by ShaneC; 03-29-2009 at 03:18 AM.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • Users who have thanked ShaneC for this post:

    weir-07 (03-29-2009)

  • #3
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    PHP Code:
    if ($Result1 mysql_query($updateSQL$STC)) 
    Line above will always return true, unless there's problem with the database. What you need is mysql_affected_rows().

  • Users who have thanked steelaz for this post:

    weir-07 (03-29-2009)

  • #4
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by steelaz View Post
    PHP Code:
    if ($Result1 mysql_query($updateSQL$STC)) 
    Line above will always return true, unless there's problem with the database. What you need is mysql_affected_rows().
    That did the trick! Thank you very much both of you!!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •