-4

If you look at the query below I cannot insert this query as the error it is returning is :

undefined variable:

error in C:\wamp64\www\course selector\senthil\insert_data.php on line 25

And my code is:

<?php

        require_once("connectdb.php");


        $name = $_POST["name"];
        $email = $_POST["email"];
        $contact = $_POST["contact"];
        $country = $_POST["country"];
        $state = $_POST["state"];
        // $Areaofstudy = $_POST["Areaofstudy"];

        $course = $_POST["course"];

        $institutes = $_POST["checkbox"];

        if(isset($_POST['submit']))
        {
            for ($i=0; $i<sizeof($institutes); $i++)
                {
            // $query ="INSERT INTO `enquiries` (`name`, `email`, `contact`, `country`, `state`, `area_of_study`, `course`, `institute`) VALUES ('" . $name ."', '" . $email . "', '" . $contact .  "', '" . $country . "', '" . $state . "', '" . $Areaofstudy . "', '11', '" . $institutes . "')";

          $query = "INSERT INTO `enquiries` (`name`, `email`, `contact`, `country`, `state`, `area_of_study`, `course`, `institute`) VALUES ('" . $name ."', '" . $email . "', '" . $contact .  "', '" . $country . "', '" . $_POST['Areaofstudy'] . "', '" . $_POST['course'] . "', '16', '17')";

        $result = $dbhandle->query($query) or die($error) ;
    }
    }

     ?>
Siddhant Shah
  • 75
  • 1
  • 8
  • 3
    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) – Jens Sep 27 '17 at 09:24
  • 1
    Check you code $institutes[$i = $_POST["checkbox"]; – Aman Kumar Sep 27 '17 at 09:26
  • Apart from the error on the line Aman Kumar mentioned, you should really be sanitizing those POST vars. That is an sql injection hazard you have there – Cornel Raiu Sep 27 '17 at 09:45
  • Where did you declare your $error variable? – Adhan Timothy Younes Sep 30 '17 at 14:06

3 Answers3

0

Line 25 is $result = $dbhandle->query($query) or die($error); and since you have not defined $error before you used it in that line, it will throw an "undefined variable: error" error.

If you are using PDO you could probably do:

$result = $dbhandle->query($query) or die($dbhandle->errorCode());

or get more detailed error information from $dbhandle->errorInfo().

aljo f
  • 2,430
  • 20
  • 22
-1

The variable $error is not declared first. You must declare it first.

$error = 'Error description here';

And then you can call it.

-1

check this line

$institutes[$i = $_POST["checkbox"];

correct

$institutes[$i = $_POST["checkbox"]];
Umer Best
  • 9
  • 1
  • 8