how to convert my object into a string?

0

I have done a function to insert the data into the database, however as I am using classes, I have to insert an object of class into the database that will contain the data of the person in question, except that the form being passed the object It's wrong, because it's not a string, I think if I convert it to a string it will save the bank line that needs to be modified. I'm sure it's only the last one, please help me if you have the availability to contact skype with curse cmg, it would be better, because I would learn more, it's very important for me, I'm doing my completion work and I need a help.

include ('Pessoa.class.php');
include ('funcao_inserir.php');


$nome = $_REQUEST['nome'];  
$email = $_REQUEST['email'];
$numero_cartao_sus =$_REQUEST['numerodosus'];
$cpf = $_REQUEST['cpf'];
$rg = $_REQUEST["rg"];
$nomesocial = $_REQUEST["nomesocial"];
$nascimento = $_REQUEST["nascimento"];
$sexo = $_REQUEST["sexo"];
$pis_pasep = $_REQUEST["pis_pasep"];
$nome_mae = $_REQUEST["nome_mae"];
$nacionalidade = $_REQUEST["nacionalidade"];
$pais_nascimento = $_REQUEST["pais_nascimento"];
$municipio_nascimento =$_REQUEST["municipio_nascimento"];
$estado_nascimento =$_REQUEST["estado_nascimento"];
$telefone =$_REQUEST["telefone"];
$estado_civil =$_REQUEST["estado_civil"];
$orientacao_sexual =$_REQUEST["orientacao_sexual"];

$pessoa = new Pessoa();
$pessoa->setNome($nome);
$pessoa->setEmail($email);

$pessoa->setNumeroCartaoSus($numero_cartao_sus);
$pessoa->setCpf($cpf);
$pessoa->setRg($rg);
$pessoa->setNomesocial($nomesocial);
$pessoa->setNascimento($nascimento);
$pessoa->setSexo($sexo);
$pessoa->setPisPasep($pis_pasep);
$pessoa->setNomeMae($nome_mae);
$pessoa->setNacionalidade($nacionalidade);
$pessoa->setPaisNascimento($pais_nascimento);
$pessoa->setMunicipioNascimento($municipio_nascimento);
$pessoa->setEstadoNascimento($estado_nascimento);
$pessoa->setTelefone($telefone);
$pessoa->setEstadoCivil($estado_civil);
$pessoa->setOrientacaoSexual($orientacao_sexual);



inserir(array("nome","email"), $pessoa,"dados");
    
asked by anonymous 05.04.2017 / 19:47

1 answer

0

Your question is very poorly formulated, and there is missing information from the files you have made, so I can understand exactly what is happening, but I will try to help you follow the correct path based on what you have informed, let's go.

You need to run an INSERT command, so basically your insert function will need to build the string to execute this command. In the following example I'm assuming your database is MySQL and I'm using PHP's mysqli:

<?php
    function inserir($tabela, $objeto){
        $mysqli = new mysqli("localhost","usuario_db","senha_db","nome_db");

        $sql = 'INSERT INTO ' . $tabela . ' (';
        foreach ($objeto as $indice => $valor){
            $sql .= $indice . ',';
        }
        $sql = substr($sql,0,-1) . ') VALUES (';
        foreach ($objeto as $indice => $valor){
            $sql .= "'" . $valor . "',";
        }
        $sql = substr($sql,0,-1);
        $sql .= ')';

        $mysqli->query($sql);

        $linhasAfetadas = $mysqli->affected_rows;

        $mysqli->close();

        return $linhasAfetadas;
    }
?>

In this situation the function receives the name of the table that wants to perform the INSERT and the object with the fields corresponding to the table, we set the SQL string of the command with this information using foreach to traverse the object, we execute the command and end the connection with the bank, optionally returning at the end the total number of rows that have been inserted, so you can detect that if it was inserted 1 line the command worked.

It's worth noting that this solution is not perfect, you'd have to treat the data that goes into that string, especially in the case of large text fields, but it's the path you need to follow.     

05.04.2017 / 20:26