How to Post in the Same Page in Simple PHP?

2019-04-11 02:08发布

问题:

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>