SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,756
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Handle Database Error in PHP

    Quote Originally Posted by r937
    Quote Originally Posted by DoubleDee
    And *if* the database is enforcing that "gender" must be either "Male" or "Female" then what happens if that isn't the case?
    you get an error saying that foreign key integrity has been violated, and the insert fails

    note you can catch the error in php and issue your own user-friendly error message (but don't ask me how to do that, as i don't do php)
    Can someone help me figure this one out?

    Since I use Prepared Statements, would I capture it like below...
    PHP Code:
                // Verify Insert.
                
    if (mysqli_stmt_affected_rows($stmt3)==1){
                    
    // Insert Succeeded.
                    
    do something...
                }else{
                    
    // Insert Failed.
                    
    $_SESSION['resultsCode'] = 'ACCOUNT_MEMBER_ACCT_FAILED_2103';
                }
    // End of VERIFY INSERT.

                // Close prepared statement.
                
    mysqli_stmt_close($stmt2);

                
    // Set Error Source.
                
    $_SESSION['errorPage'] = $_SERVER['SCRIPT_NAME'];

                
    // Redirect to Display Outcome.
                
    header("Location: " BASE_URL "/account/results.php");

                
    // End script.
                
    exit();
            }else{
                
    // Invalid form data.
                // Drop through to display Form.
            
    }//End of ATTEMPT TO CREATE MEMBER 
    Thanks,


    Debbie

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    There's a difference between an insert failing (0 affected rows), and an ERROR.

    if it's an error, mysqli_error() would hold the text contents of the error.
    mysqli_errno() would hold the error number (as specified by the MySQL Error Codes)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

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