3

我正在尝试仅使用 PHP 创建一个注册系统。这是一个例子。但我认为我做错了什么。我试图在 StackOverFlow 帖子中找到类似的解决方案,但没有得到任何确切的解决方案。如果有人能帮助我在下面的代码中找到错误,那将是非常好的。

<?php
// POST HANDLER -->

if(isset($_POST['registerForm']))
{
    conFunc(); // Connection Function

    $userid = $_POST['userid'];
    $name = $_POST['name'];

    $getUserId = mysql_query("SELECT * FROM `user` WHERE `userid` = '".$userid."'");
    $id = mysql_fetch_array($getUserId);

    if($id)
    {
        echo "This User ID is Already Available on the System, Please Choose Something Else!";
    }
    else
    {

        $query = mysql_query("INSERT INTO `user` (`userid`, `name`");

        if($query)
        {
            echo "A New User is Registered Successfully:<br /><br />";
            echo "<b>User ID:</b> " . $userid . "<br />";
            echo "<b>User Name:</b> " . $name . "<br />";
        }
        else
        {
            echo "There is an Error while Saving: " . mysql_error();
            echo "<br />Please click on Create User from menu, and try again<br /><br />.";
        }

    }
    exit;
}
// POST HANDLER -->
?>

<!-- FORM GOES BELOW -->

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="registerForm">

<table style="width: 100%">
    <tr>
        <td>User ID</td>
        <td><input name="userid" type="text" style="width: 300px" /><br /></td>
    </tr>
    <tr>
        <td>Name</td>
        <td><input name="name" type="text" style="width: 300px" /><br /></td>
    </tr>
    <tr>
        <td></td>
        <td><input style="width: 130px; height: 30px" type="submit" name="submit" value="Register Now" /><br /></td>
    </tr>
</table>

</form>
4

4 回答 4

3

您必须检查是否设置了提交按钮。

if(isset($_POST['registerForm']))

应该

if(isset($_POST['submit'])) {
    // your php code
} else {
    // your html code

}
于 2013-07-09T05:27:06.130 回答
2

registerform 元素不被视为 post 元素,因此请使用提交按钮进行检查。

尝试以下代码:

<?php
// POST HANDLER -->

if(isset($_POST['submit'])){
    conFunc(); // Connection Function

    $userid = $_POST['userid'];
    $name = $_POST['name'];

    $getUserId = mysql_query("SELECT * FROM `user` WHERE `userid` = '".$userid."'");
    $id = mysql_fetch_array($getUserId);

    if($id)
    {
        echo "This User ID is Already Available on the System, Please Choose Something Else!";
    }
    else
    {

        $query = mysql_query("INSERT INTO `user` (`userid`, `name`");

        if($query)
        {
            echo "A New User is Registered Successfully:<br /><br />";
            echo "<b>User ID:</b> " . $userid . "<br />";
            echo "<b>User Name:</b> " . $name . "<br />";
        }
        else
        {
            echo "There is an Error while Saving: " . mysql_error();
            echo "<br />Please click on Create User from menu, and try again<br /><br />.";
        }

    }
    exit;
}else{
// POST HANDLER -->
?>

<!-- FORM GOES BELOW -->

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="registerForm">

<table style="width: 100%">
    <tr>
        <td>User ID</td>
        <td><input name="userid" type="text" style="width: 300px" /><br /></td>
    </tr>
    <tr>
        <td>Name</td>
        <td><input name="name" type="text" style="width: 300px" /><br /></td>
    </tr>
    <tr>
        <td></td>
        <td><input style="width: 130px; height: 30px" type="submit" name="submit" value="Register Now" /><br /></td>
    </tr>
</table>

</form>
<?php } ?>
于 2013-07-09T05:31:39.027 回答
0

你的脚本应该像

  $getUserId = mysql_query("SELECT id FROM `user` WHERE `userid` = '".$userid."'");

因为您获得了所有结果,并且您需要检索id唯一的结果,并且您的表单操作应该是您的同一页面本身

于 2013-07-09T05:27:21.793 回答
0

你需要阅读 mysql 查询http://php.net/manual/en/book.mysql.php

还要检查您的插入查询,而不是插入任何数据值。

 <?php if(isset($_POST['submit']))
    {
        conFunc(); // Connection Function

        $userid = $_POST['userid'];
        $name = $_POST['name'];

        $getUserId = mysql_query("SELECT * FROM user WHERE userid = '".$userid."'");
        $id = mysql_fetch_array($getUserId);

        if($id)
        {
            echo "This User ID is Already Available on the System, Please Choose Something Else!";
        }
        else
        {

            $query = mysql_query("INSERT INTO user(userid, name) values('" .$userid . "','" . $name . "')";

            if($query)
            {
                echo "A New User is Registered Successfully:<br /><br />";
                echo "<b>User ID:</b> " . $userid . "<br />";
                echo "<b>User Name:</b> " . $name . "<br />";
            }
            else
            {
                echo "There is an Error while Saving: " . mysql_error();
                echo "<br />Please click on Create User from menu, and try again<br /><br />.";
            }

        }
        exit;
    }
    // POST HANDLER -->
    ?>

<!-- FORM GOES BELOW -->

<form action="" method="post" name="registerForm">

<table style="width: 100%">
    <tr>
        <td>User ID</td>
        <td><input name="userid" type="text" style="width: 300px" /><br /></td>
    </tr>
    <tr>
        <td>Name</td>
        <td><input name="name" type="text" style="width: 300px" /><br /></td>
    </tr>
    <tr>
        <td></td>
        <td><input style="width: 130px; height: 30px" type="submit" name="submit" value="Register Now" /><br /></td>
    </tr>
</table>

</form>
于 2013-07-09T05:38:09.617 回答