1
I want to check if a name exists in the database; if there is no registration. Using the code below I can register several users with the same name.
php.
$user = $_POST['user'];
$pass = md5($_POST['pass']);
if(empty($user) or empty($pass)) echo "Preencha todos os campos para continuar!";
else {
$dbhost = "localhost";
$dbuser = "user";
$dbpass = "senha";
$dbname = "banco";
$con = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
$db = mysql_select_db($dbname, $con) or die(mysql_error());
$query = mysql_query("INSERT INTO tbl_users VALUES (NULL,'$user','$pass')");
if($query) echo "Sua conta foi criada com sucesso!";
}
html form.
<form name="form3" method="post" action="cadastrar.php">
<label>USER:</label>
<input type="text" name="user"/>
<label>PASS:</label>
<input type="password" name="pass" />
<input type="submit" value="CADASTRAR" />
</form>
tabela.sql
CREATE TABLE `tbl_users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(250) collate latin1_general_ci NOT NULL,
`password` varchar(250) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=6 ;
I tried to modify these 3 lines and the registration is not done already jumps to message that the user exists even without existing. How to fix this?
$query = mysql_query("INSERT INTO tbl_users VALUES ('$user','$pass')");
if($query) echo "Sua conta foi criada com sucesso!";
else echo "Usuário já existe, escolha outro nome.";
That’s right! a small detail that makes all the difference I was killing myself here!!! thanks for the help.
– Rose
Cool. If you have decided you can mark the answer as "accepted". I saw that you didn’t do this in any of the questions you asked, probably because you don’t know the appeal yet. This is important to inform that the problem has been solved and give an acknowledgement to those who answered you. To better understand see the [tour]. This gives you reputation too.
– Maniero
I didn’t know that either rsrs... now I’ll dial when useful!
– Rose
The vote for useful things you can give where you want, not only in your things. Acceptance you can only choose one answer in question you have asked. Take the opportunity to review whether to accept answers in your other past questions.
– Maniero