user6801758 user6801758 - 3 months ago 8
MySQL Question

PHP and HTML how can i load in a new file

I have created a login page in PHP where the user inputs their details and they are then posted to another PHP file that then checks the username entered by the user and sees if it registered in the MySQL accounts table, if so it then checks the password and if it's a match I want it to open up the profile page and if not, I want it to return to the login page. How can I get the PHP code to load in a new html/php file to be displayed on the screen. Heres my PHP code so far:

<html>

<?php
include_once "mysql_connect.php";

$usersName = $_POST['usersname'];
$passWord = $_POST['passsword'];

$result = mysql_query("SELECT * FROM allaccounts");
$num_rows = mysql_num_rows($result);

$username = "";
$password = "";

for ($i = 1; $i <= $num_rows; $i++) {
$currentname = mysql_query("SELECT * FROM allaccounts WHERE id=$i");

while ($row = mysql_fetch_array($currentname)) {
$username = $row[0];
$password = $row[1];
}
if (($username === $usersName) && ($password === $passWord)) {
echo "We got you";
break;
} else {
echo "nothing";
}
}
?>
</html>

Answer

use header() function. This will redirect to the page specified.

if (($username === $usersName) && ($password === $passWord)) {
           header("Location: profile.php");
    } else {
           header("Location: fail.php");
    }

Also - get rid of <html> at the top. It is of no use and means headers are already sent.