-1

these are the two files in which error is occurred:

html_form.html

<html>
    <body>
        <center>
            <h1>Admission Form</h1>
            <br>
            <form action="php_register.php" name="registration">
            Firstname: <input type="text" name="name1" placeholder="Enter Firstname"/>
            <br>
            Lastname: <input type="text" name="name2" placeholder="Enter Lastname"/><br>
                <input type="submit" value="submit">
            </form>
        </center>
    </body>
</html>

php_register.php

<?php
     $fname=$_POST['name1'];
     $lname=$_POST['name2'];

    $conn=mysqli_connect("localhost","root","","test");

     if (isset($fname) && isset($lname))
      {
         mysqli_query($conn, "insert into test_table(firstname,lastname) 
         values ('$fname','$lname')");
      }
      else
         echo "<br> Errror....Values are not set in variables...!!!";
?>

( ! ) Notice: Undefined index: name1 in C:\wamp2\www\PHP_project\php_register.php on line 2 Call Stack

Time Memory Function Location

1 0.0022 131712 {main}( ) ...\php_register.php:0

( ! ) Notice: Undefined index: name2 in C:\wamp2\www\PHP_project\php_register.php on line 3 Call Stack

Time Memory Function Location

1 0.0022 131712 {main}( ) ...\php_register.php:0

Errror....Values are not set in variables...!!!

Robert
  • 33,429
  • 8
  • 90
  • 94
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – CBroe Apr 16 '18 at 07:46

3 Answers3

2

You need to add method="post" to your <form> tag.

Also you could a check if those values are set before on your php_register.php page to ensure the script will run successfully.

e.g.

if (empty($_POST['name1']) || empty($POST['name2']) { 
    // some kind of error setting and redirecting back maybe
}
Simon R
  • 3,732
  • 4
  • 31
  • 39
0

By default, form method is GET but you are trying to get values By $_POST.

You didn't define form POST method then you can get values by $_GET or $_REQUEST as:

$_GET['name1'];
$_GET['name2'];

or

$_REQUEST['name1'];
$_REQUEST['name2'];

If you want to use $_POST then you should define form method="post"

If you have no idea about form method, It's a better way to use $_REQUEST. By $_REQUEST you can get both type values.

Example:

$fname=isset($_POST['name1'])?$_POST['name1']:'';
 $lname=isset($_POST['name2'])?$_POST['name2']:'';

$conn=mysqli_connect("localhost","root","","test");

 if (!empty($fname) && !empty($lname))
  {
     mysqli_query($conn, "insert into test_table(firstname,lastname) 
     values ('$fname','$lname')");
  }
  else
     echo "<br> Errror....Values are not set in variables...!!!";
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
  • Now i defined the method in html file as method="post" but still there is same problem... – Mohd Tazammul Apr 16 '18 at 07:55
  • check it isset($_REQUEST['name2']). it's good habit to avoid such error. – Gufran Hasan Apr 16 '18 at 07:56
  • yup now its working. I replaced the $_POST method by $_REQUEST and now it is inserting the records.... Thanks alot Gurfan Hasan bhai... I had been suffering by this problem for number of days and you solved it. Thanks again..... – Mohd Tazammul Apr 16 '18 at 08:09
0

u don't declare POST or GET in the form ,but u use $_POST['name1'] to get the value, thats where the error occured.

Leslie
  • 11
  • 3
  • oh, well then u should var_dump() the data u post ,and find where is wrong, if $_POST['name1'] == {your data}, we can dismiss the problem from the frontend. – Leslie Apr 16 '18 at 08:05
  • yup now its working. I replaced the $_POST method by $_REQUEST and now it is inserting the records.... – Mohd Tazammul Apr 16 '18 at 08:12
  • if u r satisfied with my answer.it will be so appreciated to adopt it. – Leslie Apr 16 '18 at 08:57