joaocasta Posted July 20, 2012 at 09:33 PM Report #469436 Posted July 20, 2012 at 09:33 PM Boa noite, tenho aqui este código e eu queria adicionar mais um elseif ou if a dizer que aquele email já está em uso... aqui está o codigo: <?php include "config.php"; include "template.php"; $error = ""; if (!empty($_POST['register'])) { if (empty($_POST['username'])) { $error .= 'Username em falta.<br />'; } if (empty($_POST['password'])) { $error .= 'Password em falta.<br />'; } if (empty($_POST['email'])) { $error .= 'Email em falta.<br />'; } if (empty($error )) { $username = mysql_real_escape_string($_POST['username']); $password = md5($_POST['password']); // escaping not needed when hashing. Should use a salt for md5 $checkusername = mysql_query("SELECT * FROM users WHERE Username = '".$username."'"); if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){ $error .= 'Email is not valid format.<br />'; } $email = mysql_real_escape_string($_POST['email']); if(mysql_num_rows($checkusername) == 1) { $error .= '<p>Sorry, that username is taken. Please go back and try again.</p>'; } $id_usertipo = 4; // este é o id com que o user fica quando se regista $registodata = date("d F Y"); if (empty($error)) { $registerquery = mysql_query("INSERT INTO users (Username, Password, Email, id_usertipo, data_de_registo) VALUES('".$username."', '".$password."', '".$email."', '".$id_usertipo."', '".$registodata."')"); if($registerquery) { $success = "<h1>Success</h1>"; $success .= "<p>Your account was successfully created. Please <a href=\"login.php\">click here to login</a>.</p>"; } else { $error .= "<p>Sorry, your registration failed. Please go back and try again.</p>"; } } } } ?> <?php if (!empty($error)) { ?> <h1>Error</h1> <?php echo $error; ?> <?php }elseif (!empty($success)) { echo $success; }else { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Registo</title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <div id="main"> <div id="login_wrapper"> <div id="login_box"> <form method="post" action="registar.php" name="registerform" id="registerform"> <table> <tr> <td> Username: </td> <td> <input name="username" type="text" class="input" size="25"/> </td> </tr> <tr> <td> Password: </td> <td> <input name="password" type="password" class="input" size="25"/> </td> </tr> <tr> <td> Email: </td> <td> <input name="email" type="text" class="input" size="25"/> </td> </tr> </table> <input type="submit" name="register" id="register" value="Register" /> </form></div></div> <?php } ?> </div> </body> </html>
HappyHippyHippo Posted July 20, 2012 at 10:13 PM Report #469446 Posted July 20, 2012 at 10:13 PM então força ai ... IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
joaocasta Posted July 20, 2012 at 10:17 PM Author Report #469448 Posted July 20, 2012 at 10:17 PM mas como faço?
HappyHippyHippo Posted July 20, 2012 at 10:46 PM Report #469455 Posted July 20, 2012 at 10:46 PM (edited) assim : tenho aqui este código e eu queria adicionar mais um elseif ou if Edited July 20, 2012 at 10:47 PM by HappyHippyHippo IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
joaocasta Posted July 21, 2012 at 07:23 AM Author Report #469473 Posted July 21, 2012 at 07:23 AM pois isso ja eu sabia, mas tentei e nao consegui
dash Posted July 21, 2012 at 10:00 AM Report #469479 Posted July 21, 2012 at 10:00 AM Então tu consegues verificar se um utilizador já existe mas nao consegues verificar um email? Não sei se foste tu que programaste isso que aí está ou não, mas basta leres os comentários para perceberes que acção deves tomar para fazer a verificação.
joaocasta Posted July 22, 2012 at 08:49 AM Author Report #469621 Posted July 22, 2012 at 08:49 AM (edited) Boas, já consegui fazer mais ou menos o que queria, mas estou a receber um erro: Notice: Undefined variable: success in C:\wamp\www\novo tema v1\registar.php on line 53 <?php include "config.php"; include "template.php"; $error = ""; if (!empty($_POST['register'])) { if (!$_POST['username'] || !$_POST['password'] || !$_POST['email'] || !$_POST['repassword']) { print "<div align='center'><h4>Tem de preencher todos os campos.</h4></div>"; exit; } $username = $_POST['username']; $dupe1 = mysql_num_rows(mysql_query("select * from users where username='$username'")); if ($dupe1 > 0) { print "<div align='center'><h4>Esse username ja esta em uso.</h4></div>"; exit; } $email = $_POST['email']; $dupe2 = mysql_num_rows(mysql_query("select * from users where email='$email'")); if ($dupe2 > 0) { print "<div align='center'><h4>Esse email ja esta a ser usado.</h4></div>"; exit; } elseif(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){ print "<div align='center'><h4>Email nao é valido.</h4></div>"; } //check if passwords are the same if ($_POST['password'] != $_POST['repassword']) { print "<div align='center'><h4>Passwords nao coincidem.</h4></div>"; exit; } if (empty($error )) { $username = mysql_real_escape_string($_POST['username']); $password = md5($_POST['password']); // escaping not needed when hashing. Should use a salt for md5 $email = mysql_real_escape_string($_POST['email']); $id_usertipo = 4; // este é o id com que o user fica quando se regista $registodata = date("d F Y"); if (empty($error)) { $registerquery = mysql_query("INSERT INTO users (username, password, email, id_usertipo, data_de_registo) VALUES('".$username."', '".$password."', '".$email."', '".$id_usertipo."', '".$registodata."')"); if($registerquery) { $success .= "<div align='center'>A sua conta foi criada. Por favor <a href=\"login.php\">clique aqui para login</a>.</div>"; } else { $error .= "<div align='center'>Desculpe, o registo falhou. Por favor tente de novo.</div>"; } } } } ?> <?php if (!empty($error)) { ?> <h1 align='center'>Erro</h1> <?php echo $error; ?> <?php }elseif (!empty($success)) { echo $success; }else { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Registo</title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <div id="main"> <div id="login_wrapper"> <div id="login_box"> <form method="post" action="registar.php" name="registerform" id="registerform"> <table> <tr> <td> Username: </td> <td> <input name="username" type="text" class="input" size="25"/> </td> </tr> <tr> <td> Password: </td> <td> <input name="password" type="password" class="input" size="25"/> </td> </tr> <tr> <td> Confirme a Password: </td> <td> <input name="repassword" type="password" class="input" size="25"/> </td> </tr> <tr> <td> Email: </td> <td> <input name="email" type="text" class="input" size="25"/> </td> </tr> </table> <input type="submit" name="register" id="register" value="Register" /> </form></div></div> <?php } ?>[/b] [b]</div> </body> </html> Linha 53: $success .= "<div align='center'>A sua conta foi criada. Por favor <a href=\"login.php\">clique aqui para login</a>.</div>"; Edited July 22, 2012 at 08:50 AM by joaocasta
HappyHippyHippo Posted July 22, 2012 at 08:59 AM Report #469622 Posted July 22, 2012 at 08:59 AM <?php //... $error = ""; // ... if (empty($error)) { // ... if($registerquery) { // concatenar a mensagem de erro a NADA (porque ainda não existe) !!!! $success .= "<div align='center'>A sua conta foi criada. Por favor <a href=\"login.php\">clique aqui para login</a>.</div>"; } else { $error .= "<div align='center'>Desculpe, o registo falhou. Por favor tente de novo.</div>"; } } então estás a concatenar uma string a uma variável não inicializada ... isso funciona, porque (é um notificação não é um erro) o PHP inicializa automaticamente a variável a string vazia mas não escapas sem um aviso do interpretador IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
joaocasta Posted July 22, 2012 at 09:03 AM Author Report #469623 Posted July 22, 2012 at 09:03 AM Tenho de retirar a variavel $success ?
HappyHippyHippo Posted July 22, 2012 at 09:05 AM Report #469624 Posted July 22, 2012 at 09:05 AM Tenho de retirar a variavel $success ? epa ... eu disse isso ?? e achas mesmo que o que disseste faz sentido ??? se fosse esse o caso não poderias declarar mais nenhuma variável ..... olha para o caso da variável $error, eu deixei o código dessa variável no comentário por alguma razão IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
joaocasta Posted July 22, 2012 at 09:17 AM Author Report #469626 Posted July 22, 2012 at 09:17 AM então acho já percebi tenho de criar uma variavel como a da $error, certo? tipo: $error = ""; $success = "";
HappyHippyHippo Posted July 22, 2012 at 09:21 AM Report #469627 Posted July 22, 2012 at 09:21 AM claro !!!! concatenar uma string é uma operação binária, necessita de dois operadores, no teu caso só tinhas 1 : a string "<div ... " IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
joaocasta Posted July 22, 2012 at 09:49 AM Author Report #469630 Posted July 22, 2012 at 09:49 AM criei a variavel $success mas nao deu
HappyHippyHippo Posted July 22, 2012 at 10:11 AM Report #469631 Posted July 22, 2012 at 10:11 AM criei a variavel $success mas nao deu mas que raio de resposta é esta ??? - não deu o que ? - mensagem de erro ? - não fez o pino ? - o que te leva dizer que não deu ? - ????????????????? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
joaocasta Posted July 22, 2012 at 10:19 AM Author Report #469632 Posted July 22, 2012 at 10:19 AM nao deu, porque deu o mesmo erro.
HappyHippyHippo Posted July 22, 2012 at 10:33 AM Report #469633 Posted July 22, 2012 at 10:33 AM mete aqui o teu código ... não faço ideia como fizeste a alteração IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
joaocasta Posted July 22, 2012 at 10:43 AM Author Report #469634 Posted July 22, 2012 at 10:43 AM <?php include "config.php"; include "template.php"; $error = "";[/b] [b]$success = ""; if (!empty($_POST['register'])) { if (!$_POST['username'] || !$_POST['password'] || !$_POST['email'] || !$_POST['repassword']) { print "<div align='center'><h4>Tem de preencher todos os campos.</h4></div>"; exit; } $username = $_POST['username']; $dupe1 = mysql_num_rows(mysql_query("select * from users where username='$username'")); if ($dupe1 > 0) { print "<div align='center'><h4>Esse username ja esta em uso.</h4></div>"; exit; } $email = $_POST['email']; $dupe2 = mysql_num_rows(mysql_query("select * from users where email='$email'")); if ($dupe2 > 0) { print "<div align='center'><h4>Esse email ja esta a ser usado.</h4></div>"; exit; } elseif(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){ print "<div align='center'><h4>Email nao é valido.</h4></div>"; } //check if passwords are the same if ($_POST['password'] != $_POST['repassword']) { print "<div align='center'><h4>Passwords nao coincidem.</h4></div>"; exit; } if (empty($error )) { $username = mysql_real_escape_string($_POST['username']); $password = md5($_POST['password']); // escaping not needed when hashing. Should use a salt for md5 $email = mysql_real_escape_string($_POST['email']); $id_usertipo = 4; // este é o id com que o user fica quando se regista $registodata = date("d F Y"); if (empty($error)) { $registerquery = mysql_query("INSERT INTO users (username, password, email, id_usertipo, data_de_registo) VALUES('".$username."', '".$password."', '".$email."', '".$id_usertipo."', '".$registodata."')"); if($registerquery) { $success .= "<div align='center'>A sua conta foi criada. Por favor <a href=\"login.php\">clique aqui para login</a>.</div>"; } else { $error .= "<div align='center'>Desculpe, o registo falhou. Por favor tente de novo.</div>"; } } } } ?> <?php if (!empty($error)) { ?> <h1 align='center'>Erro</h1> <?php echo $error; ?> <?php }elseif (!empty($success)) { echo $success; }else { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd%22> <html xmlns="http://www.w3.org/1999/xhtml%22> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Registo</title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <div id="main"> <div id="login_wrapper"> <div id="login_box"> <form method="post" action="registar.php" name="registerform" id="registerform"> <table> <tr> <td> Username: </td> <td> <input name="username" type="text" class="input" size="25"/> </td> </tr> <tr> <td> Password: </td> <td> <input name="password" type="password" class="input" size="25"/> </td> </tr> <tr> <td> Confirme a Password: </td> <td> <input name="repassword" type="password" class="input" size="25"/> </td> </tr> <tr> <td> Email: </td> <td> <input name="email" type="text" class="input" size="25"/> </td> </tr> </table> <input type="submit" name="register" id="register" value="Register" /> </form></div></div> <?php } ?>[/b] [b]</div> </body> </html>
HappyHippyHippo Posted July 22, 2012 at 10:53 AM Report #469635 Posted July 22, 2012 at 10:53 AM e qual a mensagem de erro ?!?!?!??! IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
joaocasta Posted July 22, 2012 at 10:55 AM Author Report #469636 Posted July 22, 2012 at 10:55 AM Ainda agora tinha dado erro, mas testei outra vez e passou-lhe....
HappyHippyHippo Posted July 22, 2012 at 10:55 AM Report #469637 Posted July 22, 2012 at 10:55 AM gravar o ficheiro depois de alterar e antes de testar ajuda 1 Report IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
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