如何在简单的PHP中发布在同一页面?

时间:2013-07-09 05:25:23

标签: php post system registration

我正在尝试仅使用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 个答案:

答案 0 :(得分:2)

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

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

应该是

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

}

答案 1 :(得分: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 } ?>

答案 2 :(得分:0)

您的脚本应该像

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

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

答案 3 :(得分: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>