hossam hossam - 4 years ago 97
PHP Question

Code isn't working inside Function which is working outside


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);


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.

Answer Source

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 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download