Warning: Undefined array key 0 in filename on line *3*

I am trying to test navigation when a form is submitted. In the form I have one text box, just to have something containing text.

<form action="Filename.php" method="post">
        <input type="text" name="Name" id="Name">
        <button type="submit">Submit</button>
    </form>

The action PHP file contains

<?php
echo "success";
if($_SERVER['REQUEST_METHOD' === 'POST'])
{    
}
else
{    
}

The action file is executing and displays success, but giving me a warning
Warning: Undefined array key 0 in filename on line 3
Is the array key to do with $_SERVER or post?

You have your brackets placed incorrectly in this line.

if($_SERVER['REQUEST_METHOD'] === 'POST')

This should work. As you had it, ' === 'POST' was part of the array key.

Making some progress

try {
    if($_SERVER['REQUEST_METHOD'] === "POST")
    {
        echo "success";
        class Class1{

            public function Function1(){
                echo "success";
            }
        }
        $class1 = new Class1();
    }
} catch (PDOException $e) {
    echo "fail";
    echo $e->getMessage();
}

If the script executes correctly, it should print success twice. It’s not executing the function, as it’s only printing success once. I’m assuming the function will also print success when executed.

Doesn’t that tell you that your code is not calling the function/method?

You are defining a class, creating an instance of that class, and assigning the object to $class1, but where are you calling the Function1() function/method of that class?

Hint: calling a function/method of an object looks like $obj->method();

Next, the only time you should catch and handle a database exception in your code are for user recoverable errors, such as when inserting/updating duplicate user submitted data. For all other query errors, all other type of queries, and for the connection, you should simply do nothing in your code and let php catch and handle the database exception where php will use its error related settings to control what happens with the raw error information, via an uncaught exception error (database statement errors will ‘automatically’ get displayed/logged the same as php errors.)

And just to be abundantly clear as to why you got the error you did:
PHP saw this:

if($_SERVER['REQUEST_METHOD' === 'POST'])

Parser said:
“I see an if. Evaluate the condition.”
“The condition is an array or object reference. Evaluate the index.”
“The evaluation resolves to false
A boolean is not a valid array index. Acceptable indexes are int or string. Implicit Coercion to int. Evaluates as 0.
Error: $_SERVER does not contain an index 0. Throw Undefined Array Key.

1 Like

I am testing the navigation/communication between PHP files/classes
I have three files,
Config.php contains class Connection which uses function Connect() connects to dbase
Data.php containing methods that query the dbase
User.php which is executed when a form is submitted

User.php

<?php
    if($_SERVER['REQUEST_METHOD'] === "POST")
    {  
        class User{
            public function User_Verify(){
                $data = new Data();
                $test = $data->User_Verification();
                if($test > 0)
                {
                    echo $test;
                }
            }
        }
        $user = new User();
        $user->User_Verify();
    }

Data.php

<?php
class Data extends Connection{
    public function User_Verification()
    {
        $sql = "SELECT COUNT(EMAIL) from user";
        $stmt = $this->Connect()->query($sql);
        while($row = $stmt->fetch())
        {
            return $row[0];
        }
    }
}
$data = new Data();

Uncaught Error: class Data not found in User.php

Because class Data isnt in User.php.

You would need to include or require the file that holds the Data class inside User.php.
(Before the “but what about” crowd chimes in… the user has 3 files. This is not a Composer setup.)

1 Like

Cheers, got it working.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.