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...

Recommended Answers

All 4 Replies

Man, what's the error message, show me what's hapanning :D

It's no error, it just don't do the echo

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'])
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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.