Recording data from a SESSION in the BD

Asked

Viewed 497 times

0

I would like to add in the field registered the functionname logged in to database.

<?php 
    require_once("../../../session.php");
    require_once("../../../class.user.php");
    $auth_user = new USER();
    $user_id = $_SESSION['user_session'];
    $stmt = $auth_user->runQuery("SELECT * FROM cad_funcionarios WHERE id_funcionario=:id_funcionario");
    $stmt->execute(array(":id_funcionario"=>$user_id));
    $userRow=$stmt->fetch(PDO::FETCH_ASSOC);

    $stmt = $auth_user->runQuery('INSERT INTO cadastro_clientes (numerocontrato, id_cpf_cnpj, nome_completo, adesao, cpfoucnpj, cpfoucnpjnumero, data_nascimento, email, cep, rua, numero, bairro, cidade, estado, id_vendedor, id_forma_pagamento, id_forma, id_sexo, data_vencimento, id_revenda, id_plano, id_acomodacao, telefone, celular, total, cadastradopor, data_hora_cadastro, nomedependente, cpfdependente, datanascimentodependente, planodependente, acomodacaodependente, grauparentescodependente, valordependente) VALUES (:numerocontrato, :id_cpf_cnpj, :nome_completo, :adesao, :cpfoucnpj, :cpfoucnpjnumero, :data_nascimento, :email, :cep, :rua, :numero, :bairro, :cidade, :estado, :id_vendedor, :id_forma_pagamento, :id_forma, :id_sexo, :data_vencimento, :id_revenda, :id_plano, :id_acomodacao, :telefone, :celular, :total, :cadastradopor, NOW(), :nomedependente, :cpfdependente, :datanascimentodependente, :planodependente, :acomodacaodependente, :grauparentescodependente, :valordependente)');

    foreach ($_POST['usuarios'] as $usuario) {
        $usuario['numerocontrato']= $_POST['numerocontrato'];
        $usuario['id_cpf_cnpj']= $_POST['id_cpf_cnpj'];
        $usuario['nome_completo']= $_POST['nome_completo'];
        $usuario['adesao']= $_POST['adesao'];
        $usuario['cpfoucnpj']= $_POST['cpfoucnpj'];
        $usuario['cpfoucnpjnumero'] = $_POST['cpfoucnpjnumero'];
        $usuario['data_nascimento'] = date('Y-m-d',strtotime($_POST['data_nascimento']));
        $usuario['email'] = $_POST['email'];
        $usuario['cep'] = $_POST['cep'];
        $usuario['rua'] = $_POST['rua'];
        $usuario['numero'] = $_POST['numero'];
        $usuario['bairro'] = $_POST['bairro'];
        $usuario['cidade'] = $_POST['cidade'];
        $usuario['estado'] = $_POST['estado'];
        $usuario['id_vendedor'] = $_POST['id_vendedor'];
        $usuario['id_forma_pagamento'] = $_POST['id_forma_pagamento'];
        $usuario['id_forma'] = $_POST['id_forma'];
        $usuario['id_sexo'] = $_POST['id_sexo'];
        $usuario['data_vencimento'] = date('Y-m-d',strtotime($_POST['data_vencimento']));
        $usuario['id_revenda'] = $_POST['id_revenda'];
        $usuario['id_plano'] = $_POST['id_plano'];
        $usuario['id_acomodacao'] = $_POST['id_acomodacao'];
        $usuario['telefone'] = $_POST['telefone'];
        $usuario['celular'] = $_POST['celular'];
        $usuario['total']= $_POST['total'];
        $stmt->execute($usuario); 
} 
echo 'Cadastro realizado com sucesso<br><br>';

echo "<h1>" . $userRow['nome_funcionario'] .  "</h1>";

//header('refresh:6;cad_usuarios.php');
?>
  • What is the logic of this foreach ? oo ... Could you edit the question? It’s too vague, there’s no session variable there, how will you save in the bank if there are no such variables?

  • This foreach serves to record the data of the responsével + the dependent @lvcs I do not know how to do or how to place the session

  • "id of a user logged in to the database" - Which user are you talking about? root? If not, how do these users do logon?

  • @Maurosantos it seems to me that the foreach always takes the same data, so I do not understand, I think it is the case that you edit the question by putting what you want to do, but in the question there is only code, explain what you want to do, with details.

  • You accepted an answer that has nothing to do with what you were asked. Edit your question.

  • Welcome to Stackoverflow in English. I edited your question to remove the greetings as we usually keep the text as clean as possible to focus on your scheduling question. If you are interested in visiting a part of the site that is not aimed to ask questions can know the [chat]. If you have questions about the operation, rules and procedures of the site visit the [meta] :)

Show 1 more comment

2 answers

1


If you intend to put the value $userRow['nome_funcionario'] in the field cadastradopor, just put a line in your foreach before the $stmt->execute($usuario);:

$usuario['cadastradopor'] = $userRow['nome_funcionario']

0

The PDO connection is working right?

If so, try doing it that way:

<?php 
    require_once("conexao.php");
    $pdo = Database::conexao();

    $stmt = $pdo->prepare('INSERT INTO cadastro_clientes (numerocontrato, id_cpf_cnpj, nome_completo, adesao, cpfoucnpj, cpfoucnpjnumero, data_nascimento, email, cep, rua, numero, bairro, cidade, estado, id_vendedor, id_forma_pagamento, id_forma, id_sexo, data_vencimento, id_revenda, id_plano, id_acomodacao, telefone, celular, total, data_hora_cadastro, nomedependente, cpfdependente, datanascimentodependente, planodependente, acomodacaodependente, grauparentescodependente, valordependente) VALUES (:numerocontrato, :id_cpf_cnpj, :nome_completo, :adesao, :cpfoucnpj, :cpfoucnpjnumero, :data_nascimento, :email, :cep, :rua, :numero, :bairro, :cidade, :estado, :id_vendedor, :id_forma_pagamento, :id_forma, :id_sexo, :data_vencimento, :id_revenda, :id_plano, :id_acomodacao, :telefone, :celular, :total, NOW(), :nomedependente, :cpfdependente, :datanascimentodependente, :planodependente, :acomodacaodependente, :grauparentescodependente, :valordependente)');

    foreach ($_POST['usuarios'] as $usuario) {
        $stmt->bindParam(1,$usuario['numerocontrato']);
        $stmt->bindParam(2,$usuario['id_cpf_cnpj']);
        $stmt->bindParam(3,$usuario['nome_completo']);
        /*Completa as outras*/
} 

$stmt->execute(); 

echo 'Cadastro realizado com sucesso<br><br>';
//header('refresh:6;cad_usuarios.php');
?>
  • Just out of curiosity, what does that answer have to do with session?

  • As there is NOTHING with Session in his question other than the title, so I just thought it might improve.

  • I’ve improved the code, now I’m getting Sesssion @Carlosrocha, I just don’t know how to add.

  • Improve, but don’t answer, @carlos-rocha.

  • @Carlosrocha in this case, when you see that the question is a little confused, I suggest not answer. Probably, if it is not closed, it will be edited. And if it is edited, your reply will be invalid. Then you will have to edit, because someone will want to negatively if it is not valid.

  • You mean the Insert is not working? That’s it?

  • Whallace, got it! I’ll take precaution next time. Or should I delete the answer?

  • @Maurosantos, if you do print_r($_POST['usuarios']), which comes out in the browser?

Show 3 more comments

Browser other questions tagged

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