0

Hi,
i trying to do a registration form (easiest thing ever) but i'm having trouble doing the validation :S

<?php
    include 'db_connect.php';
?>
<html>

<head>
    <title>Adminstração</title>
</head>

<body style="color=#FFFFFF" bgcolor="#666666">
    <table width="100%" height="100%" border="0" align="center" cellpadding="0" cellspacing="1">
        <tr>
            <form method="post" action="">
                <td align="center" valign="middle">
                    <table width="300px" cellpadding="0" cellspacing="3" bgcolor="#FFFFFF" style=" border: 1px solid #000000;">
                        <tr>
                            <td style=" border-style:none;">
                                <br>
                            </td>
                        </tr>

                        <tr>
                            <td align="right" style="font-size: 13;font-family: Arial, Helvetica, sans-serif;font-weight: bold;border-style:none;">
                                Utlizador:
                            </td>

                            <td style=" border-style:none;">
                                <input name="user" type="text">
                            </td>
                        </tr>

                        <tr>
                            <td align="right" style="font-size: 13;font-family: Arial, Helvetica, sans-serif;font-weight: bold;border-style:none;">
                                Password:
                            </td>
                            <td style=" border-style:none;">
                                <input name="password" type="password">
                            </td>
                        </tr>

                        <tr>
                            <td colspan="2" align="center" style=" border-style:none;">
                                <input type="submit" name="registar" value="Registar">
                            </td>
                        </tr>

                        <tr>
                            <td style=" border-style:none;">
                                <br>
                            </td>
                        </tr>
                    </table>
                    <span class="style1" style="font-size: 13;font-family: Arial, Helvetica, sans-serif;font-weight: bold;color: red;">&nbsp;</span>
                </td>
            </form>
        </tr>
    </table>
</body>
</html>
<?PHP
    if(isset($_POST['registar'])) 
    {
        $username = $_POST['user'];
        $password = $_POST['password'];

        if (empty($username) OR empty($password))
        {
            echo "<script>alert('Todos os campos são obrigatórios.')
                location.href = 'add_user.php';</script>";
        }

        if ($username!="" && $password!="")
        {
            $sel_user="SELECT * FROM utilizadores";
            $user=mysql_query($sel_user, $connect);
            $row_user=mysql_fetch_assoc($user);

            if ($username=$row_user['username'])
            {
                "<script>alert('O nome do utilizador ja existe.')</script>";
            }
            else
            {
                $sql="INSERT INTO utilizadores (username, password) VALUES ('".$username."','".sha1($password)."')";
                $result = mysql_query($sql);

                if(!$result) 
                {
                    echo "<script>alert('Erro a adicionar utilizador.')</script>";
                }
                else 
                {
                    echo "<script>alert('Utilizador adicionado com sucesso.');
                        location.href = 'admin.php';</script>";
                }
            }
            mysql_close($connect);
        }
    }
?>

Please help me...

Edited by PF2G

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by xShirowx
0

Hi,
Try change in your script, this code:

$sel_user="SELECT * FROM utilizadores";
$user=mysql_query($sel_user, $connect);
$row_user=mysql_fetch_assoc($user);
if ($username=$row_user['username'])

With this one:

$sel_user="SELECT * FROM utilizadores WHERE username='$username' LIMIT 1";
$user=mysql_query($sel_user, $connect);
$row_user=mysql_fetch_assoc($user);
if ($username == $row_user['username'])
0
if ($username=$row_user['username']) { 
    echo "<script>alert('O nome do utilizador ja existe.')</script>";
}

dont u forget to put an echo in ur code?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.