代码不在函数内部工作[复制]

This question already has an answer here:

<?php

include 'config.php';

function getid()
{
    $fname = @$_POST['fname'];
    $lname = @$_POST['lname'];
    $email = @$_POST['email'];
    $password = @$_POST['password'];

    if (@$_POST['login'])
    {
        $qry = "insert into register values (NULL, '$fname', '$lname',   
        '$email', '$password')";

        mysqli_query($conn, $qry);  
    }
};

Calling of the function in another file where the html exists:

include 'Config\api.php';

getid($conn, $_POST);

getid();

Taking into consideration that the code is working outside of the function which means that the connection is working and there is no problem with the coding or variables. Thank you.

</div>

You funtion getid() cannot access the global variable $conn. You can make it accessible to the function defining it like this :

function getid()
{
  global $conn;
  //Now you can use it inside the function.
}

Or pass it as a variable when calling the function like this :

function getid($conn)
{
  //Now you can use $conn inside the function.
}

//Call the function like this 
getid($conn)

The $_POST is using in this function. You need to pass $_POST & $conn to function.

Change from

function getid()

to

function getid($conn, $_POST)

and then call it like getid($conn, $_POST);

Note: You must call this function where form is submitting.