Undefined variable error when defining variable


Julien Vincent

When defining a variable, I get an "undefined variable" error. bellow is my code

<?php
$imagepath = $_SESSION['path'];
require_once('class-db.php');

if ( !class_exists('INSERT') ) {
    class INSERT {
        public function post($postdata) {
            global $db;

            $query = "
                            INSERT INTO posts (title, content, subcontent, date, category, image)
                            VALUES ('$postdata[title]', '$postdata[content]', '$postdata[subcontent]', '$postdata[date]', '$postdata[category]', '$imagepath')
                        ";

            return $db->insert($query);
        }
    }
}

$insert = new INSERT;

?>
John Conder

$imagepathis declared outside the function, so it's not available inside the function unless you pass it as a parameter or use the globalkeyword . see variable scope

public function post($postdata, $imagepath) {
    global $db;

or

public function post($postdata) {
    global $db, $imagepath;

Related


Undefined variable error when defining variable

Julien Vincent When defining a variable, I get an "undefined variable" error. bellow is my code <?php $imagepath = $_SESSION['path']; require_once('class-db.php'); if ( !class_exists('INSERT') ) { class INSERT { public function post($postdata) {

Strange error when defining variable

Duy So I'm trying to use the Lua C Api on the game, but I'm getting some errors when defining the variables in the .cpp file I have a library called CLua.h that defines everything I use in the following code and it compiles fine. (I won't share it because it's

Error when showing undefined variable

Shah Error: _co.bellowContent is undefinedThere was an error displaying the data . You can quote my code below. There was an error Error: _co.bellowContent is undefineddisplaying the data . You can quote my code below. app.component.html <table class="table">

Error when showing undefined variable

Shah Error: _co.bellowContent is undefinedThere was an error displaying the data . You can quote my code below. There was an error Error: _co.bellowContent is undefineddisplaying the data . You can quote my code below. app.component.html <table class="table">

Error when showing undefined variable

Shah Error: _co.bellowContent is undefinedThere was an error displaying the data . You can quote my code below. There was an error Error: _co.bellowContent is undefineddisplaying the data . You can quote my code below. app.component.html <table class="table">

Cannot read property 'lookup' of undefined when defining variable

Unknown Beast I'm getting a TypeError, although it shouldn't be because I believe I have the variable defined correctly (please correct me!) Have looked around trying to find it; however, even with the "fix" it still gives me the error. I apologize for the mis

Cannot read property 'lookup' of undefined when defining variable

Unknown Beast I'm getting a TypeError, although it shouldn't be because I believe I have the variable defined correctly (please correct me!) Have looked around trying to find it; however, even with the "fix" it still gives me the error. I apologize for the mis