pedromartins Posted April 19, 2012 at 10:35 AM Report #449907 Posted April 19, 2012 at 10:35 AM Boas pessoal, estou aqui com um problema que espero que me possam ajudar a ultrapassar :| Estou a fazer um projecto, para já ainda estou na fase inicial a criar o sistema de login com registo etc, e o problema que estou a ter é no registar.php if (isset($_POST['reg'])) { try { if (trim($_POST['nome']) == '') { throw new Exception ('O campo Nome é obrigatório', 1); } if (trim($_POST['username']) == '') { throw new Exception('O campo Username é obrigatório', 1); } if (trim($_POST['pass']) == '') { throw new Exception('O campo Password é obrigatório', 1); } if ($_POST['pass'] != $_POST['confpw']) { $erro = "As passwords não correspondem"; } if (trim($_POST['email']) == '') { throw new Exception('O campo Email é obrigatório', 1); } $nome = $_POST['nome']; $utilizador = strip_tags(mysql_real_escape_string($_POST['username'])); $password = md5(addslashes($_POST['pass'])); $email = $_POST['email']; $sql = "insert into utilizadores (nome, utilizador, pwd, email) VALUES ('$nome', '$utilizador', '$password', '$email')"; $resultado = mysql_query($sql) or die("Erro na consulta."); if (mysql_affected_rows() != 1) $erro = "Não foi possível inserir o registo:" . mysql_error(); } catch (Exception $e) { switch ($e->getCode()) { case 1: echo $e->getMessage(); break; case 2: echo 'Ocorreu um erro ao efectuar a operação, por favor contacte o suporte técnico.'; error_log('Error n. ' . $e->getCode() . ' - ' . $e->getMessage(), 0); break; } } } Else ?> <script type="text/javascript"> window.alert("Registo com sucesso!") window.location='index.php'; </script> Bem ao deixar os campos livres ele deveria de apresentar a mensagem de erro, mas não o faz e dá o seguinte erro "Fatal error: Uncaught exception 'Exception' with message 'O campo Username é obrigatório' in C:\wamp\www\isep\lregistar.php"
KTachyon Posted April 19, 2012 at 10:26 PM Report #450071 Posted April 19, 2012 at 10:26 PM Então... tu estás a fazer um throw new Exception(...) com o code 1. No catch estás a apanhar, e no switch, case 1 mostra a mensagem. Só no case 2 é que tens a tua mensagem personalizada. “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now