0

I'm having errors like this in 4 of the text boxes.
<b/><b>Notice</b>: Undefined variable: fname in <b>C:\xampp\htdocs\webpage\phpmyadmin\db\form.php</b> on line <b>..</b><br />

I read about similar problem from here but it didn't work for me.
Where did I make mistake?

<?php
if(isset($_POST['name']) && isset($_POST['password'])&& isset($_POST['fname'])
&& isset($_POST['lname']) && isset($_POST['email'])){
$name = $_POST['name'];
$pass = $_POST['password'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$passhash = md5($pass);
}
?>

<form action="#" method="POST">
Name:<br>
<input type="text" name="name" value="<?php echo $name ?>"><br>
Password:<br>
<input type="password" name="password"><br>
Firstname:<br>
<input type="text" name="fname" value="<?php echo $fname ?>">  <br>
Lastname:<br>
<input type="text" name="lname" value="<?php echo $lname ?>"> <br>
Email:<br>
<input type="text" name="email" value="<?php echo $email ?>"><br>
<input type="submit" value="Submit">
</form>
Community
  • 1
  • 1
007mrviper
  • 376
  • 3
  • 19
  • You really shouldn't use [MD5 password hashes](http://security.stackexchange.com/questions/19906/is-md5-considered-insecure) and you really should use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. Make sure that you [don't escape passwords](http://stackoverflow.com/q/36628418/1011527) or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard May 12 '16 at 12:45

4 Answers4

0

Check isset and then echo because before posting data you will not get all data:

<form action="#" method="POST">
Name:<br>
<input type="text" name="name" value="<?php if(isset($name)) echo $name; ?>"><br>
Password:<br>
<input type="password" name="password"><br>
Firstname:<br>
<input type="text" name="fname" value="<?php if(isset($fname)) echo $fname; ?>">  <br>
Lastname:<br>
<input type="text" name="lname" value="<?php if(isset($lname)) echo $lname; ?>"> <br>
Email:<br>
<input type="text" name="email" value="<?php if(isset($email)) echo $email; ?>"><br>
<input type="submit" value="Submit">
</form>
Jayesh Chitroda
  • 4,847
  • 11
  • 18
0

Use <?php if(isset($name )) echo $name; ?> instead <?php echo $name ?> and same for all ..

OR

you can use $name = isset($_POST['name']) ? $_POST['name']: ""; instead $name = $_POST['name']; and same for all ..

Brijal Savaliya
  • 1,073
  • 9
  • 19
0

Initialize $name = $pass = $fname = $lname = $email = $passhash ='';

Before this line 'if(isset($_POST['name']) && isset($_POST['password'])&& isset($_POST['fname'])' ;

Your code will be fine.

Brijal Savaliya
  • 1,073
  • 9
  • 19
Web Artisan
  • 1,640
  • 3
  • 21
  • 28
0

i got your problem you are trying to access variable which is not decalre. you can try below code its working fine.

<?php
if(isset($_POST['name']) && isset($_POST['password'])&& isset($_POST['fname'])
&& isset($_POST['lname']) && isset($_POST['email'])){
$name = $_POST['name'];
$pass = $_POST['password'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$passhash = md5($pass);
}
?>

<form action="#" method="POST">
Name:<br>
<input type="text" name="name" value="<?php echo isset($name)?$name:"" ?>"><br>
Password:<br>
<input type="password" name="password"><br>
Firstname:<br>
<input type="text" name="fname" value="<?php echo isset($fname)?$fname : "" ?>">  <br>
Lastname:<br>
<input type="text" name="lname" value="<?php echo isset($lname)?$lname:"" ?>"> <br>
Email:<br>
<input type="text" name="email" value="<?php echo  isset($email) ? $email : "" ?>"><br>
<input type="submit" value="Submit">
</form>

here i am checked with isset function if variable is set then print value otherwise blank.

Denis Bhojvani
  • 819
  • 1
  • 9
  • 18