I'm receiving a fatal error of "Call to undefined function" but cannot figure out why I'm receiving that error?

Viewed 37

I am trying to update user profile of an existing user in mysql database using php pdo but I am receiving an error like this.., "Fatal error: Uncaught Error: Call to undefined function test_input() on line 12". A similar code worked well when I'm registering a user, inserting data onto my database table, but I'm getting an error when I'm trying to update user details. The code is like this...

if(isset($_SESSION['user'])) {
    
    $name = $surname = $gender = $race = $email = $username = "";

    if($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = test_input($_POST["name"]); // Line: 12...
        $surname = test_input($_POST["surname"]);
        $gender = test_input($_POST["gender"]);
        $race = test_input($_POST["race"]);
        $email = test_input($_POST["email"]);
        $username = test_input($_POST["username"]);
    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
}

To update user details, I did the following code;

$id = $_SESSION['user_id'];
            
            if(isset($_POST['submit'])) {
                try {
                    $update_query = 'UPDATE users SET 
                                        name = :name, 
                                        surname = :surname, 
                                        gender = :gender, 
                                        race = :race, 
                                        email = :email, 
                                        username = :username
                                     WHERE ID = :id
                    ';
                    
                    $statement = $pdo->prepare($update_query);
                    $statement->bindParam(':name', $_POST['name'], PDO::PARAM_INT);
                    $statement->bindParam(':surname', $_POST['surname'], PDO::PARAM_INT);
                    $statement->bindParam(':gender', $_POST['gender'], PDO::PARAM_INT);
                    $statement->bindParam(':race', $_POST['race'], PDO::PARAM_INT);
                    $statement->bindParam(':email', $_POST['email'], PDO::PARAM_INT);
                    $statement->bindParam(':username', $_POST['username'], PDO::PARAM_INT);
                    $statement->bindParam(':id', $id, PDO::PARAM_INT);
                    if($statement->execute()) {
                        $updateSuccess = "Profile has been updated successfully!<br><br><a href='profile.php'>View Profile?</a>";
                    }
                } catch(PDOException $e) {
                    echo 'ERROR: ' . $e->getMessage();
                    echo '<br>';
                }       
            }

I looked at the code carefully and tried different ways to solve the the problem. It seems to me that I have defined the function test_input() but clearly something is wrong. I also checked the spelling to find out if it isn't a typo but it seems that's not the case. So I'm struggling find out what is the problem. What could I be missing or what do I need to do to get rid of the error and get the code working?

1 Answers

The problem is that you are defining the function inside the if block denoted by the condition isset($_SESSION['user']).

Also you can use function_exists() to check for existence of a function like so:

if ( function_exists( 'test_input' ) ) {
    test_input( params );
}

In the following code, the function test_input is located outside the if block.

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

if(isset($_SESSION['user'])) {
    
    $name = $surname = $gender = $race = $email = $username = "";

    if($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = test_input($_POST["name"]); // Line: 12...
        $surname = test_input($_POST["surname"]);
        $gender = test_input($_POST["gender"]);
        $race = test_input($_POST["race"]);
        $email = test_input($_POST["email"]);
        $username = test_input($_POST["username"]);
    }

   
}
Related