Jump to content

Data Erase From Fields


john666
Go to solution Solved by joallen,

Recommended Posts

I need Code for this problem

If Query not Execute due to some Reason then Username and Password should be stored in Fields user should not need to enter again and again user name and password actually im not getting this concpt fully how to do that...Fields should not Empty untill user Enter correct username and password

Here is my Code and need Extra code for this where to add

<?php
include('header.php');
include('config.php');

if (isset($_POST['submit']))
{
    $username = ($_POST['username']);
    $password = ($_POST['password']);

    $query  = "SELECT * FROM login WHERE user_name='$username' AND pass_word='$password' LIMIT 1";
    $result = mysql_query($query) or die(mysql_error());

    if(mysql_num_rows($result))
    {
        header('location:home.php');
        exit;
        
    }
    else {
        $error = "Wrong Username / Password";        
    }
}

?>

<table class="login" align="center">
 <tr>
      <td class="table1" > Student Information System</td>
 </tr>
</table>

<div class="table2">
    <form method="post">
        <table class="table3" align="center">
            <?php if(isset($error)): ?>
            <tr>
                <td colspan="2" style="color: red; font-weight: bold"><?php echo $error; ?></td> 
            </tr>
            <?php endif; ?>
            <tr>
                <td>Username</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" name="submit" value="LogIn"></td>
            </tr>
        </table>
    </form>
</div>

<?php
include('footer.php');
?>
Link to comment
Share on other sites

You need to set the values when the form has been submitted

                <td><input type="text" name="username" value="<?php echo isset($_POST['username']) ? $_POST['username'] : ''; ?>"></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="password" value="<?php echo isset($_POST['password']) ? $_POST['password'] : ''; ?>"></td>
Edited by Ch0cu3r
Link to comment
Share on other sites


<?php
include('header.php');
include('config.php');

if (isset($_POST['submit']))
{
$username = ($_POST['username']);
$password = ($_POST['password']);

$query = "SELECT * FROM login WHERE user_name='$username' AND pass_word='$password' LIMIT 1";
$result = mysql_query($query) or die(mysql_error());

if(mysql_num_rows($result))
{
header('location:home.php');
exit;

}
else { ?>

<form action='#' method='POST' name='loginForm'>
<input type="textbox" name="username" value="<?php echo $_POST['username'];?>"/>
<input type="textbox" name="password" value="<?php echo $_POST['password'];?>"/>
<input type="submit"/>
</form>

<?php
}
}

?>


 

Edited by paddyfields
Link to comment
Share on other sites

And if i m Sending Data to Other page like i have 2 pages 1 name Std_add.php and 2nd name  std_save.php and in std_add.php i have just page where table is created and Set post Method there and Action set to std_save.php and validating there Data and then add into data base then same procedure will Apply ????

Link to comment
Share on other sites

I'm sorry but I don't really understand your grammar, but do you mean you have two pages... the first with the form, and the second with the script which communicates with the database?

 

If so then you need to put the address of the second page in the 'action' part of the form.

 <form action='page2.php' method='POST' name='loginForm'>
Link to comment
Share on other sites

  • Solution

Your Code:

<?php
include('header.php');
include('config.php');

if (isset($_POST['submit']))
{
    $username = ($_POST['username']);
    $password = ($_POST['password']);

    $query  = "SELECT * FROM login WHERE user_name='$username' AND pass_word='$password' LIMIT 1";
    $result = mysql_query($query) or die(mysql_error());

    if(mysql_num_rows($result))
    {
        header('location:home.php');
        exit;
        
    }
    else {
        $error = "Wrong Username / Password";        
    }
}

?>

<table class="login" align="center">
 <tr>
      <td class="table1" > Student Information System</td>
 </tr>
</table>

<div class="table2">
    <form method="post">
        <table class="table3" align="center">
            <?php if(isset($error)): ?>
            <tr>
                <td colspan="2" style="color: red; font-weight: bold"><?php echo $error; ?></td> 
            </tr>
            <?php endif; ?>
            <tr>
                <td>Username</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" name="submit" value="LogIn"></td>
            </tr>
        </table>
    </form>
</div>

<?php
include('footer.php');
?>

Change To:

<?php
include('header.php');
include('config.php');

if (isset($_POST['submit']))
{
    $username = ($_POST['username']);
    $password = ($_POST['password']);
    $error = '';
    $query  = "SELECT * FROM login WHERE user_name='".$username."' AND pass_word='".$password."' LIMIT 1";
    $result = mysql_query($query) or die(mysql_error());
    $numresults = mysql_num_rows($result);
    if($numresults > 0)
    {
        //Needs something to set a cookie or session data to check if user is logged in.
        header('location:home.php');
        exit;
    }
    else {
        $error = "Wrong Username / Password";        
    }
}

?>

<table class="login" align="center">
 <tr>
      <td class="table1" > Student Information System</td>
 </tr>
</table>

<div class="table2">
    <form method="post">
        <table class="table3" align="center">
            <tr>
                <td colspan="2" style="color: red; font-weight: bold"><?php if(isset($error)){echo $error;} ?></td> 
            </tr>
            <tr>
                <td>Username</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" name="submit" value="LogIn"></td>
            </tr>
        </table>
    </form>
</div>

<?php
include('footer.php');
?>

I will generally use mysql_num_rows to verify if any results returned from a query. Although it is an additional step, It enables me to utilize the variable if needed in other parts of the script.

 

It doesn't seem like you are setting anything such as a session variable or cookie so your other pages know the user is logged in. In essence all they would have to do is just navigate to your home.php page if they knew the url and it wouldn't stop them from viewing the information.

 

How does your login page know they aren't already logged in? A simple method would be to set a session variable such as a md5 hash of the user's id in your database. See below:

<?php
//Start the session. This must be at the beginning of every page if you are going to check the session variable.
session_start();

//Check to see if the visitor is logged in by checking for $_SESSION['id']. If they are then simply send them to the home.php page. 
if (isset($_SESSION['id'])){
    header('Location: home.php');
}
include('header.php');
include('config.php');

if (isset($_POST['submit']))
{
    $username = ($_POST['username']);
    $password = ($_POST['password']);
    $error = '';
    $query  = "SELECT * FROM login WHERE user_name='".$username."' AND pass_word='".$password."' LIMIT 1";
    $result = mysql_query($query) or die(mysql_error());
    $numresults = mysql_num_rows($result);
    if($numresults > 0)
    {
        $row = mysql_fetch_assoc($result);
        $_SESSION['id'] = md5($row['id']); <-----This should reference a field in your database table that is set as the primary key and an auto-number. If you aren't using one then you should start.
        header('Location: home.php');
        exit;
    }
    else {
        $error = "Wrong Username / Password";        
    }
}

?>

<table class="login" align="center">
 <tr>
      <td class="table1" > Student Information System</td>
 </tr>
</table>

<div class="table2">
    <form method="post">
        <table class="table3" align="center">
            <tr>
                <td colspan="2" style="color: red; font-weight: bold"><?php if(isset($error)){echo $error;} ?></td> 
            </tr>
            <tr>
                <td>Username</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" name="submit" value="LogIn"></td>
            </tr>
        </table>
    </form>
</div>

<?php
include('footer.php');
?>

Keep in mind, $_SESSION['id'] can be set to anything. You could do $_SESSION['myid'] or $_SESSION['itdoesntmatterwhatyounameit']. You are simply setting a variable to the user's internet session which you can access from any page. The session will eventually timeout, at which point they will be redirected to the login page if they try to navigate to a page which requires them to be logged in. A session can last for days though. So if you require a timeout feature then you will need to write a script to check the time based upon however you decide, and if the user is supposed to be timed out then the script should destroy the session variable using unset($_SESSION['id']) or unset($_SESSION['whatever your variable is']) and redirect the user to the login.php page.

 

If you decide to use the session variable method then at the beginning of every php page you direct a user to which requires them to be logged in should have the following:

<?php
session_start();
if (!isset($_SESSION['id'])){
    header("Location: login.php");
}

//// The rest of your page's script here

?>

Cheers!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.