PHP Form not directing to correct pages

2020-05-05 00:44发布

问题:

I'm making a login page for the admins to make some changes to a website easily. However, the login page isn't working correctly. It won't go to the error page InvalidLogin.html and it won't go to the next page of the admin website AdminChanges.php.

Instead, I'm getting the following message:

Not Found The requested URL /website/method="post" was not found on this server.

<?php
    if ($_POST['submit'] == "submit")
    {
    $userName = $_POST['username'];
    $passWord = $_POST['password'];

$db= mysql_connect("localhost", "root", "root");
        if(!$db) die("Error connecting to MySQL database.");
        mysql_select_db("onlineform", $db);


$checkUserNameQuery = "SELECT username FROM onlineformdata ORDER BY id DESC LIMIT 1";
$checkUserName = mysql_query($checkUserNameQuery);
$checkPassWordQuery = "SELECT password FROM onlineformdata ORDER BY id DESC LIMIT 1";
$checkPassWord = mysql_query($checkPassWordQuery);

if (($userName == $checkUserName) && ($passWord == $checkPassWord)) 
    {
    $AdminChanges = "AdminChanges.php";
    }
else 
    {
    $AdminChanges = "InvalidLogin.html";
    }
}

function PrepSQL($value)
    {
        // Stripslashes
        if(get_magic_quotes_gpc()) 
        {
            $value = stripslashes($value);
        }

        // Quote
        $value = "'" . mysql_real_escape_string($value) . "'";

        return($value);
    }

?>
<html>
<head>
<title>Admin Login</title>
</head>
<body>
<form action = <?php PrepSQL($AdminChanges); ?>  method="post">
username: <input type="text" name="username" />
password: <input type="text" name="password" /> <br/>

<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

回答1:

Two problems are joining forces to cause this error. First, your PrepSQL function does not echo the response, and neither does the code that calls it. You need to echo or print the response so that it appears in your generated HTML.

<?php echo PrepSQL($AdminChanges); ?>

Second, you need to encapsulate that value of the action attribute in double-quotes, like this:

<form action = "<?php echo PrepSQL($AdminChanges); ?>"  method="post">

Also note that your code assumes that your mysql_query() statements were successful. For troubleshooting purposes, you should at least add an or die(mysql_error()) statement to the end of the mysql_query() lines. This will allow your code to provide some feedback when the query fails.

Additionally, please note that your query-handling method will never result in a valid login response.

$checkUserName = mysql_query($checkUserNameQuery);
$checkPassWord = mysql_query($checkPassWordQuery);
if (($userName == $checkUserName) && ($passWord == $checkPassWord))

mysql_query() returns a MySQL resource, not a single field from the database. Your code attempts to compare that resource to the supplied username and password, and the comparison will always fail. For details about handling the results of mysql_query() see the documentation.



回答2:

Replace: PrepSQL($AdminChanges);

with: print PrepSQL($AdminChanges);



回答3:

Try this:

<form action = "<?php echo PrepSQL($AdminChanges); ?>"  method="post">

You need to echo the value.



回答4:

There are 2 errors I noticed:

Your $_POST['submit'] if statement doesn't let $AdminChanges be set for the form unless it has already been submitted.

To fix this you could change your if submit statement to just redirect to your invalid login page like so:

if (($userName == $checkUserName) && ($passWord == $checkPassWord)) 
{
    //Correct info do what you need to here
}
else 
{
    header("Location: InvalidLogin.html");
    exit();
}

And also:

You need to change the action to go post to this page.

<form action="<? $_SERVER['PHP_SELF'];?>"  method="post" enctype="multipart/form-data">