Geetha
Geetha

Reputation: 353

Notice : Undefined index in php

My data is getting saved in database but show the following notice:

Notice: Undefined index: fname in D:\xampp\htdocs\savedata\saa.php on line 20

Notice: Undefined index: lname in D:\xampp\htdocs\savedata\saa.php on line 20

1 record added

This is validation code:

    <?php
    $firstname=$lastname="";
    $firstnameErr=$lastnameErr="";
    if ($_SERVER['REQUEST_METHOD']== "POST") {
       $valid = true; //Your indicator for your condition, actually it depends on what you need. I am just used to this method.
     if(empty($_POST["fname"]))
    {
        $firstnameErr="*Name is Required";
        $valid=false;
    }
    else
    {
    $firstname=test_input($POST["fname"]);  
    }  

    if(empty($_POST["lname"]))
    {
        $lastnameErr="*Name is Required";
        $valid=false;
    }
    else
    {
    $$lastname=test_input($POST["lname"]);  
    }  

     //if valid then redirect
      if($valid){

          echo '<META HTTP-EQUIV="Refresh" Content="0; URL=saa.php">';    
        exit;
    //   header('Location: datasubmitted.php');
    //   exit();
      }
    }
    // Sanitize data
    function test_input($data) {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }
    ?>

html form code

    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
    Firstname: <input type="text" name="fname" /><?php echo $firstnameErr;?><br />
    Lastname: <input type="text" name="lname" /><?php echo $lastnameErr?>

    <input type="submit" name="submit" value="Submit"/>
    </form>

code to insert data in database

<?php
$con = mysql_connect("localhost","root","geetha@99");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);

$sql="INSERT INTO nametable (firstname,lastname)
VALUES
('$_POST[fname]','$_POST[lname]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>

Upvotes: 0

Views: 1603

Answers (6)

rccoros
rccoros

Reputation: 590

Its a notice; not an error

To fix this, you'll have to check whether $_POST['fname'] and $_POST['lname'] are set:

if(!isset($_POST["fname"] && empty($_POST["fname"])) {
   ...
}

if(!isset($_POST["lname"] && empty($_POST["lname"])) {
  ...
}

Upvotes: 2

aconrad
aconrad

Reputation: 586

First, using $_POST variables directly in your queries will make your code insecure, due to mysql inject danger. At least use mysql_real_escape_string($_POST['var']) if not PDO with bindparams.

Second, mysql_* functions are deprecated.

Regarding your erorr message, it would help to actually let us know what line is line 20.

Regardless, you error is because you are using something like $_POST[fname] without testing if $_POST[fname] exists. Also, use $_POST['fname'] not $_POST[fname].

$p_fname = '';
if (isset($_POST['fname'])) $p_fname = $_POST['fname'];
//then
$sql = "INSERT ..... SET field='".mysql_real_escape_string($p_fname)."'";

And if you don't care about the E_NOTICE level errors, you just put

error_reporting(E_ALL^E_NOTICE); //at the top of your scripts

Upvotes: 0

MP.
MP.

Reputation: 131

you can ignore this notice by using this conditions on top of page,

<?php
if(isset($_POST['fname']) && $_POST['fname']!=''){ $firstname=$_POST['fname'];}else{$firstname='';}
if(isset($_POST['lname']) && $_POST['lname']!=''){ $lastname=$_POST['lname'];}else{$lastname='';}
?>

and use $firstname and $lastname instread of $_POST[fname]and $_POST['lname'].

Upvotes: 0

NoobEditor
NoobEditor

Reputation: 15891

Its happening because your fname variable is not set inPOST when its checking for the condition.....so $_POST["fname"] is empty and hence its throws undefined warning

use it this way:

if(isset($_POST["fname"]) && !empty($_POST["fname"]))
{
   /* execution part*/
}

Upvotes: 1

Eternal1
Eternal1

Reputation: 5625

You have several typos in your script. First one

 $firstname=test_input($POST["fname"]);

should be

 $firstname=test_input($_POST["fname"]);

second one contains two

 $$lastname=test_input($POST["lname"]);

should be

 $lastname = test_input($_POST["lname"));

Upvotes: 0

Ruben Giaquinto
Ruben Giaquinto

Reputation: 390

For validation on an array element do not use empty but use array_key_exists

Your validation could be

if(array_key_exists('lname', $_POST) && empty($_POST["lname"]))
{
    $lastnameErr="*Name is Required";
    $valid=false;
}

Or you should to get the element with the filter_input function. This is the preferred method

$lname = filter_input(INPUT_POST, 'lname');

if (empty($lname)) {
    $lastnameErr="*Name is Required";
    $valid=false;
}

For the insertion of the element I discourage to use the $_POST variable into a query, but to replace with an escape version of this.

You could put this check into the validation

$lname = mysqli_real_escape_string($connection, $lname);

Another note: Do not use mysql_ functions because they are deprecated. You must use the mysqli_ extensions. http://www.php.net/manual/en/class.mysqli.php

Upvotes: 1

Related Questions