Fatal error: Function name must be a string [closed]

0
<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta charset="utf8_general_ci">
            <title> sistema de cadastro</title>
            <link rel="stylesheet" href="_css/estilo.css">
    </head>
    <body>
        <div class="container">
        <nav> 
        <ul class="menu">
            <a href="index.php"<li>Cadastro</li></a>

            <a href="consultas.php" <li>Consultas</li></a>
            </ul>

        </nav>
        <section>
        <h1>Cadastro de Usuario</h1>
        <hr><br><br>
        <form method="post" action= "processa.php">
        <input type="submit" value="Salvar" class="btn">
        <input type="reset" value="Limpar" class="btn">
        <br><br>

        Nome <br>
        <input type="text" name= "nome" class= "campo" maxlength="40" required autofocus> <br>
        Email <br>
        <input type="email" name="Email" class="campo" maxlength="50" required><br>
        Profissao <br>
        <input type="text" name="Profissao" class="campo" maxlength="40" required><br>

        </form>


        </section>
        </div>
    </body>
<html>
<?php

$hostname = "localhost";
$user = "root";
$password = "";
$database = "cadastro4";
$conexao = mysqli_connect($hostname, $user, $password, $database);
if (! $conexao){
echo "falha na conexao com o banco de dados";
<?php

include_once ("conexao.php");
$nome =$_POST ('$nome');
$email=$_POST ('Email');
$profissao=$_POST ('Profissao');

$sql = ("insert into usuarios3 (nome,email, profissao) values ('$nome', '$email', '$profissao')");

$salvar = mysqli_query ($conexao, $sql);


mysqli_close ($conexao);

?>

The error that appears:

  

Fatal error: Function name must be a string in   C: \ wamp \ www \ CADASTRO2 \ processa.php on line 4

    
asked by anonymous 29.09.2017 / 12:22

2 answers

1

The error itself occurs on the line:

$nome = $_POST('$nome');

When using parentheses, PHP will parse $_POST as a function and try to execute its call by passing a parameter of type string ; however, the superglobal variable $_POST is of type array , which is not callable, generating the error that says the function name must be a string (and not a array ).

  

In this line, too, you used $nome instead of just nome as defined in the HTML form field.

In PHP, there are two possible ways to access a position of an array : using brackets, which is the traditional way, or using braces.

$nome = $_POST["nome"];  // colchetes
$nome = $_POST{"nome"};  // chaves

According to official documentation :

  

Both brackets and braces can be used interchangeably to access elements of an array (for example, $array[42] and $array{42} will do the same thing as the previous example). [sic]

So, the correct one would be:

$nome = $_POST['nome'];
$email = $_POST['Email'];
$profissao = $_POST['Profissao'];
  

Note : It would be interesting to maintain constancy in the code. Or it defines all names starting with lowercase or uppercase. Mixing the two, while not having direct side effects, leaves the code more confusing and less legible.

Also, avoid defining a string in unnecessary parentheses, as you did in

$sql = ("insert into usuarios3 ...");

External parentheses are unnecessary and also make the code less readable, since it is expected that in an expression, the parentheses will be used to control the execution order of operators depending on their level of precedence. This is not the case.

Taking advantage of the response, the error message about the connection to the database can be improved, because in the current way, when the connection is not successful, the message will be displayed and the code will continue to execute. Since the rest of the code depends directly on a properly open connection, it does not make sense to run it when the error occurs, so it would be best if you killed the execution there, replacing echo with die :

if (! $conexao){
    die("falha na conexao com o banco de dados");
}

So when the connection is not established, the execution terminates.

    
29.09.2017 / 14:07
0
  

Function name must be a string

"Translating the function name must be a string"

The variable $_POST is an array then as mentioned in the comments, you should change the parentheses by brackets, and refer to the indexes such as name of inputs :

$nome = $_POST['nome'];
$email= $_POST['Email'];
$profissao= $_POST['Profissao'];

Another detail, include_once dispenses the parameters in the arguments, then:

include_once "conexao.php";
  

Because include is a special language constructor, parentheses are not required around the argument. Be careful when comparing return values. ¹

    
29.09.2017 / 13:36