how to convert my object into a string?

Asked

Viewed 133 times

0

I did a function to insert the data into the database, but since I am using classes, I have to insert a class object into the database that will contain the data of the person in question, only that the form that ta being passed the object ta wrong, pq is not a string, I think if I convert into string it will save in the bank the line that needs to be modified I’m sure it is only the last, someone please help me, if you have availability to contact in skype with Curse cmg, would be even better, because learn more, is very important to me, I am doing my work of completion 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");

1 answer

0

Your question is very poorly worded, and there is a lack of information from the file you made include so I can understand exactly what is happening, but I will try to help you follow the correct path based on what you reported, come on.

You need to execute a command INSERT, then basically your insert function, will need to build the string to execute this command. In the following example I am assuming that your database is Mysql and I’m using PHP 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 you want to perform the INSERT and the object with the fields corresponding to the table, we mount the SQL string of the command with this information using foreach to go through the object, we executed the command and terminated the connection with the database, optionally returning at the end the total of lines that were inserted, so you can detect that if it was inserted 1 line the command worked.

Note that this solution is not perfect, you would need to process the data that will enter this string, even more in the case of large text fields, but it is the path you need to follow.

Browser other questions tagged

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