How to Post in the Same Page in Simple PHP?

断了今生、忘了曾经 提交于 2020-02-01 06:58:46

问题


I am trying to create a Registration System using only PHP. This is an example of that. But I think I have done something wrong. I tried to find similar solution in StackOverFlow posts but didn't get any exact solution. It would be really get if someone would help me to find the error in my code below.

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

回答1:


You have to check the submit button is set or not.

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

should be

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

}



回答2:


registerform element is not treated as post element so check with submit button.

Try following code :

<?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 } ?>



回答3:


Your script should be like

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

because you are getting all the results and you need to retrive the id only and your form action should be your same page itself




回答4:


you need to read about mysql queries http://php.net/manual/en/book.mysql.php

also check your insert query not any data values inserted.

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


来源:https://stackoverflow.com/questions/17540558/how-to-post-in-the-same-page-in-simple-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!