-4

好的,所以我正在学习php,今天我想学习如何制作用户注册页面。

我写了这段代码:-php

<?php
$db = mysql_connect('127.0.0.1', 'root', '') or die ('Unable to Connect.Check your connection parameters');
mysql_select_db('stock_inventory', $db) or die (mysql_error($db));
$usernamer=(isset($_POST['username'])) ? $_POST['username'] : '';
$passwordr=(isset($_POST['password'])) ? $_POST['password'] : '';
$emailr=(isset($_POST['email'])) ? $_POST['email'] : '';
$usernamer=strip_tags($usernamer);
$passwordr=strip_tags($passwordr);
$emailr=strip_tags($emailr);
$errors[]=array();
if(isset($_POST['submit']) && $_POST['submit'] == 'register')
{
    $query = 'SELECT * FROM user WHERE user_name = ' . $usernamer;
    $result=mysql_query($query, $db) or die (mysql_error($db));
    if(mysql_num_rows($result) > 0)
    {
        echo "Username already exists.";
        echo '<br/>';
        echo "Redirecting";
        header('Refresh: 3; URL=register.php');
    }
    if(empty($usernamer))
    {
        $errors[]="Username cannot be blank";
    }
else if(empty($passwordr))
{
    $errors[]="Password cannot be empty";
}
else if(empty($emailr))
{
    $errors[]="Email Cannot be empty";
}
else if(count($errors) > 0)
{
    echo '<table>';
    echo '<tr>';
    foreach($errors as $error)
    {
        echo '<td>' . $error . '</td>';
    }
    echo '</tr>';
    echo '</table>';
    die();
}
else
{
$query = 'INSERT INTO user
          (user_id, user_name, user_password, user_email)
          VALUES
          (NULL, "' . mysql_real_escape_string($usernamer, $db) . '", "' . PASSWORD($passwordr) . '", "' . mysql_real_escape_string($emailr, $db) . '")';
$result = mysql_query($query, $db) or die (mysql_error($db));
if($result)
{
    echo "Registration Succssfull";
}
else
{
    echo "Error in registration";
}

}
}
?>

HTML

<html>
    <head>
        <title>Register</title>
    </head>
    <body>
        <form action="register.php" method="post">
            <table>
                <tr>
                    <td>Username:</td>
                    <td><input type="text" name="username" /></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="password" /></td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td><input type="text" name="email" /></td>
                </tr>
                <tr>
                    <td><input type="submit" name="register" value="register" /></td>
                </tr>
            </table>
        </form>
    </body>
</html>

问题:- 不幸的是,这个脚本什么也没做。不管我是否输入任何东西,它什么都不做。我怎样才能解决这个问题 ?

任何帮助表示赞赏。谢谢。

4

1 回答 1

2

您的提交按钮被命名为注册,而不是提交,所以这一行:

if(isset($_POST['submit']) && $_POST['submit'] == 'register')

应该

if(isset($_POST['register']) && $_POST['register'] == 'register')

这就是为什么它什么都不做。

于 2013-07-29T18:45:11.133 回答