MyBB Community Forums

Full Version: Handle SQL error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I trying to handle SQL error for duplicate "name, surname" rows. Instead of error I need to redirect the user to other site. Is it possible?

[Image: Lf6fy74.png]


My example that doesn't work:

    $query = $db->query("INSERT INTO roleplay_characters(userid, name, surname, weight, height, birthdate, sex) VALUES (".$mybb->user['uid'].", '".$db->escape_string($c_name)."', '".$db->escape_string($c_surname)."', ".$c_weight.", ".$c_height.", ".$c_birthdate.", '".$c_sex."')");
    $theError = $db->error_number();
    
    if($theError == 1062)
        {
            echo("error");
        }
You'd be better off running a query to check if a row with those values already exists first, rather than relying on it erroring.
Okay, thank you very much.
It also seems inefficient to do $db->query rather than $db->insert_query and running array_map on the values that are input. Additionally, if each user on your forum is going to be allowed to have more than one character, you would need a different primary key.