0

When trying to submit my form I get the following error message:

Notice: Undefined index: application_results in C:\xampp\htdocs\cas\insert.php 

This is my form php code:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>CAS Application</title>
  <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap        /3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body style="background-color:#FF9933">
    <div class="container" >
        <form action="insert.php" method="post" class="form-horizontal"    role="form" enctype="multipart/form-data">
            <h2 style="text-align:center">Application Form</h2>

            </div>
            <div class="form-group">
                <label for="surname" class="col-sm-3 control-label">Surname</label>
                <div class="col-sm-9">
                    <input type="text" name="surname" id="surname" placeholder="Surname" class="form-control" autofocus>
                </div>
            </div>

            <div class="form-group">
                <label for="firstName" class="col-sm-3 control-label">First Name</label>
                <div class="col-sm-9">
                    <input type="text" name="first_name" id="first_name" placeholder="First Name" class="form-control" autofocus>
                </div>
            </div>

            <div class="form-group">
                <label for="email" class="col-sm-3 control-label">Email</label>
                <div class="col-sm-9">
                    <input type="text" id="email" placeholder="Email" class="form-control" name="email">
                </div>
            </div>

            <div class="col-sm-offset-3">
                <h2>Application Results</h2>

                <label for="application_results">Please upload your application results here:</label>

                    <input type="file" name="application_results" ><br>

            </div>

            <div class="form-group">
                <div class="col-sm-6 col-sm-offset-3">
                    <button type="submit" class="btn btn-primary btn-block" name="submit">Submit</button>
                </div>
            </div>

        </form> 

    </div>
</body>
</html>
<?php

 $con = mysql_connect("localhost","root","");
 mysql_select_db("cas",$con);

 if(isset($_POST['submit'])) {
     $file = rand(1000,100000). "-".$_FILES['application_results']['name'];
     $file_loc = $_FILES['application_results']['tmp_name'];

     $folder="application_results";

     if(move_uploaded_file($file)) {
      $sql="INSERT INTO applications (application_results) VALUES    ($application_results)";
      mysql_query($sql);
}
}

?>

This is my code for insertion:

<?php

    $link = mysqli_connect("localhost", "root", "", "cas");

    if($link === false){

        die("ERROR: Could not connect. " . mysqli_connect_error());

     }


    $surname = mysqli_real_escape_string($link, $_REQUEST['surname']);

    $first_name = mysqli_real_escape_string($link, $_REQUEST['first_name']);


    $email = mysqli_real_escape_string($link, $_REQUEST['email']);

    $application_results = mysqli_real_escape_string($link, $_REQUEST['application_results']);


// attempt insert query execution

$sql = "INSERT INTO applications ( surname, first_name, 
 email, application_results ) 
VALUES ('$surname', '$first_name', '$email', $application_results )";

if(mysqli_query($link, $sql)){

    echo "Your application has been submitted.";

} else{

    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

}



// close connection

mysqli_close($link);

?>

What I am actually trying to accomplish is after insertion when calling an applications.php page details of the applicant must be displayed together with the uploaded file for reading. I suspect most of the uploaded files will be in pdf.

R. Mclaren
  • 21
  • 1
  • 6
  • `$_REQUEST['application_results']` only gets filled with GET and POST values for file you need to use `$_FILES['application_results']` – Raymond Nijland Mar 05 '17 at 16:33
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – u_mulder Mar 05 '17 at 16:40
  • @RaymondNijland - I've now changed `$application_results = mysqli_real_escape_string($link, $_REQUEST['application_results']);` to `$application_results = mysqli_real_escape_string($link, $_POST['application_results']);` and now I get this error: `Warning: mysqli_real_escape_string() expects parameter 2 to be string.` – R. Mclaren Mar 05 '17 at 17:25

0 Answers0