[PHP SCRIPT] Registar User se não existir outro

mKSilva

Power Member
Boas, o que pretendo fazer são 2 querys que verificam se o user existe, caso não exista verifica se o email já existe, se nenhum existir, ele insere o user e a password.

O erro de momento:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

O form com a action register.php:

Código:
 <form widht="500px" id="form1" name="form1" method="post" action="register.php">
    Username: <input type="text" name="username" id="username" /><br />
    Password: <input type="password" name="password" id="password" /><br />
    Email: <input type="text" name="email" id="email" /><br />
    <input type="submit" name="submit" id="submit" value="Register" />
  </form>


O código register.php:

Código:
<?php
include("dbconn.php");
$errors = "";

$username=$_POST['username'];
$email=$_POST['email'];

$sql = "SELECT username FROM $tbl_name WHERE username='$username'";
$result = @mysql_query($sql);
$count=@mysql_num_rows($result);

if ($count == 1) { echo "User already exists!"; }
    else {
        $sql = "SELECT email FROM $tbl_name WHERE email='$email'";
        $result = mysql_query($sql);
        $count=mysql_num_rows($result);
        if ($count == 1) { echo "Email already in use!"; }
        else {
            mysql_query("INSERT INTO user VALUES('$_POST[username]','$_POST[password]','$_POST[email]'") or die(mysql_error());
            echo "Registration Successful!";
            header("Location:index.php"); }
        }
?>

desde ja obrigado ;)
 
Back
Topo