Forum Moderators: coopster

Message Too Old, No Replies

Error message when form loads

This should appear only if form is submitted

         

mvaz

11:16 am on Jul 19, 2008 (gmt 0)

10+ Year Member



Hello, I have the following form, which on loading displays the error messages, whereas I want them to appear only if the form contents do not match those that are in the database. I am sure I have done something silly here, but I cannot figure it out. Please could you review my form and code and advise what needs to be changed. - Thanks in abundance in advance!
My Code and Form:

<?php
$host = "localhost";
$username = "root";
$password = "";
$db_name = "ukkonkans";
$table_name = "users";

mysql_connect ("$host", "$username", "$password") or die ("Server Connection Failed"); //connecting to server
mysql_select_db("$db_name") or die ("Database Unavailable"); //selecting database

$loginname = $_POST['loginname']; //submitted through loginform.php
$pword = md5($_POST['pword']); // submitted through loginform.php

//Clearing the junk from contents of loginform.php
$loginname = stripslashes($loginname);
$pword = stripslashes($pword);
$loginname = mysql_real_escape_string($loginname);
$pword = mysql_real_escape_string($pword);

$sql = "SELECT * FROM $table_name WHERE userName = '$loginname' and password = '$pword'";
$result = mysql_query($sql);

//Check for number of rows
$count = mysql_num_rows($result);
//The above should give a result of 1 row

if ($count == 1) {
//Register the loginname and password and direct to other page
session_register("loginname");
session_register("pword");
header("Location: login_success.php");
} else {
echo "Wrong Username or password";
}
?>

<form name="login-form" id="login-form" method="post" action="<?php echo $PHP_SELF; ?>">
<fieldset>
<legend>Please Login:</legend>
<dl>
<dt>
<label title="Username">Username: <input type="text" name="loginname" maxlength="25" id="loginname"
value = "<?php echo $loginname; ?>" />
</label>
</dt>
</dl>
<dl>
<dt>
<label title="Password">Password: <input type="password" name="pword" maxlength="15" id="pword" />
</label>
</dt>
</dl>
<dl>
<dt>
<label title="Submit"><input type="submit" name="login" value="Login" />
</label>
</label></dt>
</dl>
</fieldset>
</form>

henry0

11:32 am on Jul 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What's about giving a value to password
<label title="Password">Password: <input type="password" name="pword" maxlength="15" id="pword" />

where do you provide $password value=..etc..

Paul Bedford

11:36 am on Jul 19, 2008 (gmt 0)

10+ Year Member



The way your code is written at the moment "Wrong Username or password" will be displayed on loading. What you need to do is add to the code to determine whether the form has been submitted.

One way of doing this is to put a hidden input field in your form and then check to see whether this field exists. If the field exists you know that the form has been submitted. If the submitted username and password do not match what is stored in your database then you can output the error message to the browser.

mvaz

11:38 am on Jul 19, 2008 (gmt 0)

10+ Year Member



Thanks Henry, but the password field by default has to be blank isn't it? I have not assigned a value to this field so it doesn't have any value when the form re-submits itself.

mvaz

11:43 am on Jul 19, 2008 (gmt 0)

10+ Year Member



Thanks Henry, but the password field by default has to be blank isn't it? I have not assigned a value to this field so it doesn't have any value when the form re-submits itself. I am only teaching myself how to work with php and mysql, and so any advise and help is very much appreciated.