Fatal error: Function name must be a string

Asked

Viewed 6,501 times

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 processes.php on line 4

  • 1

    I see some syntax errors, but just in case: the if in the archive conexao.php does not have the same lock key?

  • It has yes, it is that at the time of selecting everything was out but it has yes lock on it, I was doing watching a tutorial on youtube followed everything he did only that in the video worked normal, already in mine appeared this syntax error

  • 1

    You can add the full error message to the question?

  • Fatal error: Function name must be a string in C: wamp www CADASTRO2 processa.php on line 4 ... this error message is showing '

  • 1

    Variable $_POST index nome not in parenthesis, in brackets

  • In the title of the question you are presenting an error, in the other body, and explain nothing about them.

  • I am using Notepad+++ to edit everything, and in it when I put between conchetes appears more errors ( ( ! ) Notice: Undefined index: Name in C: wamp www CADASTRO2 processes.php on line 4 Call Stack # Time Memory Function Location 1 0.5960 133376 {main}( ) .. processes.php:0 )

  • The @Jeffersonquesado already told you where the mistake is. If his comment doesn’t suit you, please edit the question by placing details of your full code.

  • I managed to solve the problem, thanks for the personal help.!

Show 4 more comments

2 answers

2

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 the type array, that is not called, generating the error that says that the function name must be a string (and not a array).

On 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 a array: using brackets, which is the traditional form, or using keys.

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

According to the official documentation:

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

So the right thing would be:

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

Note: it would be interesting to keep the constancy in the code. Either define all names starting with lower case or upper case. Mixing the two, although it has no direct side effects, makes the code more confusing and less readable.

Also, avoid defining a string between parentheses without need, as he did in:

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

External parentheses are unnecessary and also make the code less readable, as it is expected that, in an expression, parentheses are 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 bank can be improved, because in the current way, when the connection is not successful, the message will be displayed and the code will continue running. Since the rest of the code depends directly on a properly open connection, it makes no sense to execute it when the error occurs, so it would be better if you killed the execution there, replacing echo for die:

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

Thus, when the connection is not established, the execution is terminated.

  • Everything worked with this programming I was able to register the data in the database. Thanks ! ... Not wanting to abuse your goodwill but wanted to ask for another help with other programming, wanted to connect a site from a facul work in php in a database ..

  • @Gabriellima this would be subject for another question, but you already make the connection to a database with PHP in this question, so I do not understand what would be the difficulty; the code is the same.

0

Function name must be the string

"Translating the function name must be a string"

The variable $_POST is an array then as mentioned in the comments, must change the parentheses by square brackets, and refer the indices as the 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 needed around the argument. Take care when comparing return values.¹

Browser other questions tagged

You are not signed in. Login or sign up in order to post.