I want to check if a name exists in the database; if there is no registration. Using the code below I can register multiple users with the same name.
cadastro.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!";
}
form.html
<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 though it does not exist. How do I 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.";