-3

When im inserting a new record into my html im getting error to this. or is it just my codes are wrong? is the codes inserting a new records?

   <?php

    $servername = "localhost";
    $dbusername = "root";
    $dbpassword = "";
    $dbname = "signupform";

    $StudentID =$_POST['StudentID'];
    $Password = $_POST['Password'];
    $Cpassword = $_POST['Cpassword'];
    $Fname = $_POST['Fname'];
    $Lname = $_POST['Lname'];
    $Gender = $_GET['Gender'];
    $Email = $_POST['Email'];
    $Phone = $_POST['Phone'];

    $conn = mysqli_connect($servername, $dbusername, $dbpassword, $dbname)or die ("Cannot connect");


    $sql= "INSERT INTO studentdatabase (StudentID,Password,First_Name,Last_Name,Gender,Email,Phone)
            VALUE('.$StudentID.','.$Password.','.$Fname.','.$Lname.','.$Gender.','.$Gender.','.$Phone.')";

            if(!mysqli_query($conn, $sql))
            {
                die("error inserting record");

            }

        ?>

this is the error always showning up im still beginner to this php and html i wish guys can help me. i dont really understand what is my wrong here

( ! ) Notice: Undefined index: StudentID in D:\wamp\www\HAHA\saynup.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0010  135248  {main}( )   ...\saynup.php:0

( ! ) Notice: Undefined index: Password in D:\wamp\www\HAHA\saynup.php on line 9
Call Stack
#   Time    Memory  Function    Location
1   0.0010  135248  {main}( )   ...\saynup.php:0

( ! ) Notice: Undefined index: Cpassword in D:\wamp\www\HAHA\saynup.php on line 10
Call Stack
#   Time    Memory  Function    Location
1   0.0010  135248  {main}( )   ...\saynup.php:0

( ! ) Notice: Undefined index: Fname in D:\wamp\www\HAHA\saynup.php on line 11
Call Stack
#   Time    Memory  Function    Location
1   0.0010  135248  {main}( )   ...\saynup.php:0

( ! ) Notice: Undefined index: Lname in D:\wamp\www\HAHA\saynup.php on line 12
Call Stack
#   Time    Memory  Function    Location
1   0.0010  135248  {main}( )   ...\saynup.php:0

( ! ) Notice: Undefined index: Email in D:\wamp\www\HAHA\saynup.php on line 14
Call Stack
#   Time    Memory  Function    Location
1   0.0010  135248  {main}( )   ...\saynup.php:0

( ! ) Notice: Undefined index: Phone in D:\wamp\www\HAHA\saynup.php on line 15
Call Stack
#   Time    Memory  Function    Location
1   0.0010  135248  {main}( )   ...\saynup.php:0
Bap mop
  • 320
  • 2
  • 15
  • Common, wrap your stuff in a `isset()` please – Scuzzy Aug 16 '17 at 08:07
  • 9
    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) – Qirel Aug 16 '17 at 08:08

2 Answers2

0

You need to specify POST method on your html form:

<form class="modal-content animate" action="saynup.php" method="POST">

Without that extra attribute you are submitting via GET (ie you can see the values appear in the query string portion of your URL) and as such they end up in PHP's $_GET array instead of $_POST.

See here for more detail: http://php.net/manual/en/language.variables.external.php

Bananaapple
  • 2,984
  • 2
  • 25
  • 38
0
 $servername = "localhost";
    $dbusername = "root";
    $dbpassword = "";
    $dbname = "signupform";

    if (!empty( $_POST['StudentID'])) {
      $StudentID =$_POST['StudentID'];
    }
    if (!empty( $_POST['Password'])) {
      $Password = $_POST['Password'];
    }

$conn = mysqli_connect($servername, $dbusername, $dbpassword, $dbname)or die ("Cannot connect");


    $sql= "INSERT INTO studentdatabase (StudentID,Password,First_Name,Last_Name,Gender,Email,Phone)
            VALUE('.$StudentID.','.$Password.','.$Fname.','.$Lname.','.$Gender.','.$Gender.','.$Phone.')";

            if(!mysqli_query($conn, $sql))
            {
                die("error inserting record");

            }

Also check conditions for $Cpassword, $Fname, $Lname, $Gender, $Email, $Phone

Check for empty() methods .... if you get values blanks on submit then it will give you notices....

1990rk4
  • 728
  • 11
  • 21