-3

I have been trying to get data from my website to my MySQL table but everytime I try to insert data, my webpage shows undefined index. Any help would be appreciated.

<div class="form">
    <form action="legend.php" method="GET" id="legend">
        <h1>Contact Form</h1>          
        FIRST NAME<input type="text" id="fname" class="form-control" data-validation="length required " data-validation-length="min2" name="fname">        
        EMAIL<input type="email" id="email" class="form-control" data-validation="email required" name="email">     
        MESSAGE  <textarea class="form-control" name="message"  cols="50" data-validation= "message required" ></textarea>
        <input type="submit" class="btn btn-info" name="submit" value="Submit">
    </form>

    <?php
        if($_GET['submit']) {
            $id=$_GET['id'];
            $fname=$_GET['fname'];
            $email=$_GET['email'];
            if($fname!="" && $email!="") {
                $query = "INSERT INTO data(fname,email) VALUES('$fname','$email')";
                $data = mysqli_query($conn,$query);
            } else {
                echo "Enter all fields";
            }
        }
    ?>
</div>

At the top of the page I have given

<? include("connection.php"); ?>

and in the connection.php, i have given

<?php
    $servername= "localhost";
    $username= "root";
    $password= "";
    $dbname= "form";

    $conn= mysqli_connect($servername, $username, $password, $dbname);
    if($conn) { 
        echo"connected";
    } else {
        die("not connected because".mysqli_connected_error());
    }   
?>

Sorry for any noob mistake, I am new to web development.

Patrick Schocke
  • 1,493
  • 2
  • 13
  • 25
  • 3
    There is a missing closing brace at the very end. Also, error messages come with a line number, indicating in which line the error occurred. Could you provide in your question which line is producing the error (not the number, but the line)? – trincot Oct 26 '18 at 08:27
  • also, shouldn't the form still be post - even if you're intended use (for whatever reason) is to $_GET? – treyBake Oct 26 '18 at 08:28
  • You should use `if (isset($_POST['submit']))` instead of `if ($_GET['submit'])`. – Blue Oct 26 '18 at 08:30
  • 2
    You'll find all over the internet the warning of [SQL injection](http://php.net/manual/en/security.database.sql-injection.php). You ***really*** should not put your code in production without properly using prepared statements so to avoid this security hole. – trincot Oct 26 '18 at 08:30
  • I have an undefined index "submit" error at the line where i have coded: if($_GET['submit']) – Susanshu Dwivedi Oct 26 '18 at 08:35
  • Tried using POST instead of GET. Still getting undefined error. – Susanshu Dwivedi Oct 26 '18 at 08:35

2 Answers2

0

this part is not set:

$id=$_GET['id'];

you should set a name attribute with value of id. hope it works

0

replace it if($_GET['submit']) with if(isset($_GET['submit'])). Only you have to use isset check.

in your code you are using $id = $_GET['id']; but you have set value in code, you can get also same error for this line.

Rajeev Prasad
  • 31
  • 1
  • 3