1

I've got a error message from my xampp server saying that :

Notice: Undefined index: u_name in C:\xampp\htdocs\kownload\path\admin-rgst.php on line 5

Notice: Undefined index: u_pwd in C:\xampp\htdocs\kownload\path\admin-rgst.php on line 6

Notice: Undefined index: u_email in C:\xampp\htdocs\kownload\path\admin-rgst.php on line 7

My php code :

<?php
    include 'header.php';
    ?>
    <?php
$u_name = strip_tags($_POST['u_name']);
$u_pwd = md5($_POST['u_pwd']);
$u_email = strip_tags($_POST['u_email']);
if(isset($_POST['rgstdo'])){
    if(!empty($u_name) or !empty($u_pwd) or !empty($u_email)){
        echo "<h1>Error</h1>";
        }
}
?>

My html :

 <form action="admin-rgst.php" method="post">
    <table>
<tr>
<td><input type="text" name="u_name" placeholder="username"></td>
</tr>
<tr>
<td><input type="password" name="u_pwd" placeholder="pwd"></td>
</tr>
<tr>
<td><input type="email" name="u_email" placeholder="email"></td>
</tr>
<tr><td><input type="submit" name="rgstdo" value="submit"></td></tr>
    </table>
    </form>

again php

<?
    include 'footer.php';
    ?>

The form is working (showing) and the empty error message also working but i have these messages in the header. What is the problem and the solution please ?

  • Those are not _errors_, they are _notices_. Reason for them is that `$_POST` does not contain such entries. – arkascha Jan 30 '16 at 14:16
  • you want when the bottom click show error message or check post data yea? –  Jan 30 '16 at 14:16
  • Because your initial PHP code is running when the form loads the first time. Wrap them in a check to see if the form has been submitted. – andrewsi Jan 30 '16 at 14:16
  • So what is the solution for that ? – Mahmoud Shaheen Jan 30 '16 at 14:17
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Jan 30 '16 at 14:17
  • Do not keep the form definition and the form processing in the same script. That is a stupid approach, taught everywhere, which makes no sense at all. Use separate files. – arkascha Jan 30 '16 at 14:17

2 Answers2

1

try like this:

if(isset($_POST["rgstdo"]){
$u_name = strip_tags($_POST['u_name']);
$u_pwd = md5($_POST['u_pwd']);
$u_email = strip_tags($_POST['u_email']);
if(isset($_POST['rgstdo'])){
    if(!empty($u_name) or !empty($u_pwd) or !empty($u_email)){
        echo "<h1>Error</h1>";
        }
}
}
1

Your logic is off. First check if the submit is pressed, then check if any of your inputs are empty.

Plus, it looks as if your entire code is inside the same file, in turn giving you those notices. As soon as your page loads, it throws those errors.

<?php

if(isset($_POST['rgstdo'])){

$u_name = strip_tags($_POST['u_name']);
$u_pwd = md5($_POST['u_pwd']);
$u_email = strip_tags($_POST['u_email']);

if(!empty($u_name) or !empty($u_pwd) or !empty($u_email)){
    $u_name = strip_tags($_POST['u_name']);
    $u_pwd = md5($_POST['u_pwd']);
    $u_email = strip_tags($_POST['u_email']);

        }

if(empty($u_name) or empty($u_pwd) or empty($u_email)){
        echo "<h1>Error</h1>";
    }

else{
   echo "All fields were filled.";
   }
}
?>

Passwords

I also noticed that you may be attempting to store passwords with MD5. This is not recommended and is quite old and no longer considered safe to use as a password storing function.

Use one of the following:

Other links:

Important sidenote about column length:

If and when you do decide to use password_hash() or crypt, it is important to note that if your present password column's length is anything lower than 60, it will need to be changed to that (or higher). The manual suggests a length of 255.

You will need to ALTER your column's length and start over with a new hash in order for it to take effect. Otherwise, MySQL will fail silently.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141