Ronaldo Adriano de Souza
Erro ao inserir idade session no MYSQL EM PDO
07 de July de 2016 às 09:26PM
Galera, Boa noite!

Estou quebrando a cabeça para realizar uma tarefa, que é Pegar o Id do usuário logado e salvar no banco de dados como chave estrangeira em uma outra tabela, além do Id usuário, faço uma consulta para pegar outro Id que Id_sup.

Segue códigos.

Tabelas.
-- Estrutura da tabela `acordo`
--

CREATE TABLE IF NOT EXISTS `acordo` (
`id_acordo` int(11) NOT NULL AUTO_INCREMENT,
`id_sup` int(11) DEFAULT NULL,
`id_user` int(11) DEFAULT NULL,
`Nome_Cliente` varchar(45) NOT NULL,
`CPF` varchar(20) NOT NULL,
`Valor_Entrada` decimal(10,0) NOT NULL,
`IOF` decimal(10,0) NOT NULL,
`Vencimento` date DEFAULT NULL,
`Proposta` int(11) NOT NULL,
`Parcelas` int(6) NOT NULL,
`Valor_Parcelas` decimal(10,0) NOT NULL,
`Excecao` int(2) NOT NULL,
`Atraso` int(10) NOT NULL,
`Valor_Principal` decimal(10,0) NOT NULL,
`Data` date NOT NULL,
`Hora` time NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

--
-- Estrutura da tabela `admin`
--

CREATE TABLE IF NOT EXISTS `admin` (
`id_adm` int(11) NOT NULL AUTO_INCREMENT,
`Nome` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`Login` varchar(20) NOT NULL,
`Password` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
--
-- Estrutura da tabela `supervisor`
--

CREATE TABLE IF NOT EXISTS `supervisor` (
`id_sup` int(11) NOT NULL AUTO_INCREMENT,
`Nome` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`Login` varchar(20) NOT NULL,
`Password` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Extraindo dados da tabela `supervisor`
--

--
-- Estrutura da tabela `users`
--

CREATE TABLE IF NOT EXISTS `users` (
`Id_user` int(11) NOT NULL AUTO_INCREMENT,
`id_sup` int(11) NOT NULL,
`Negociador` varchar(45) NOT NULL,
`user` varchar(20) NOT NULL,
`Password` varchar(40) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

PHP

<?php
//Inicia sessão
session_start();
require_once './config/init.php';
require './config/check.php';


// resgata variáveis do formulário
$Nome_Cliente = isset($_POST['Nome_Cliente']) ? $_POST['Nome_Cliente']:'';
$CPF = isset($_POST['CPF']) ? $_POST['CPF'] : '';
$Valor_Entrada = isset($_POST['valor']) ? $_POST['valor'] : '';
$IOF = isset($_POST['IOF']) ? $_POST['IOF'] : '';
$Vencimento = isset($_POST['Vencimento']) ? $_POST['Vencimento'] : '';
$Proposta = isset($_POST['Proposta']) ? $_POST['Proposta'] : '';
$Parcelas = isset($_POST['Parcelas']) ? $_POST['Parcelas'] : '';
$Valor_Parcelas = isset($_POST['Valor_Parcelas']) ? $_POST['Valor_Parcelas'] : '';
$Excecao = isset($_POST['Excecao']) ? $_POST['Excecao'] : '';
$Atraso = isset($_POST['Atraso']) ? $_POST['Atraso'] : '';
$Valor_Principal= isset($_POST['Valor_Principal']) ? $_POST['Valor_Principal'] : '';
$Data = date('Y-m-d');
$Hora = date('h:i:s');

//Conecta PDO
$PDO = db_connect();

// Seleciona id_user, id_sup da tabela users se id_user mysql for igual ao id_user da sessão
//e se id_sup mysql for igual ao id_sup da sessão.
$sql = "SELECT id_user, id_sup FROM users WHERE id_user = :id_user AND id_sup = :id_sup";
$stmt = $PDO->prepare($sql);

$stmt->bindParam(':id_user', $id_user);
$stmt->bindParam(':id_sup', $id_sup);

$stmt->execute();

$ids = $stmt->fetchAll(PDO::FETCH_ASSOC);

//insere no banco
$PDO = db_connect();
$sql = "INSERT INTO acordo(id_user, id_sup, Nome_Cliente, CPF, Valor_Entrada, IOF, Vencimento, Proposta, Parcelas, Valor_Parcelas, Excecao, Atraso, Valor_Principal, Data, Hora) VALUES(:id_user, :id_sup, :Nome_Cliente, :CPF, :Valor_Entrada, :IOF, :Vencimento, :Proposta, :Parcelas, :Valor_Parcelas, :Excecao, :Atraso, :Valor_Principal, :Data, :Hora)";
$stmt = $PDO->prepare($sql);

$stmt->bindParam(':id_user', $id_user);
$stmt->bindParam(':id_sup', $id_sup);
$stmt->bindParam(':Nome_Cliente', $Nome_Cliente);
$stmt->bindParam(':CPF', $CPF);
$stmt->bindParam(':Valor_Entrada', $Valor_Entrada);
$stmt->bindParam(':IOF', $IOF);
$stmt->bindParam(':Vencimento', $Vencimento);
$stmt->bindParam(':Proposta', $Proposta);
$stmt->bindParam(':Parcelas', $Parcelas);
$stmt->bindParam(':Valor_Parcelas', $Valor_Parcelas);
$stmt->bindParam(':Excecao', $Excecao);
$stmt->bindParam(':Atraso', $Atraso);
$stmt->bindParam(':Valor_Principal', $Valor_Principal);
$stmt->bindParam(':Data', $Data);
$stmt->bindParam(':Hora', $Hora);

if ($stmt->execute())
{
header('Location: cad_oy.php');
}
else
{
echo "Erro ao cadastrar";
print_r($stmt->errorInfo());
}


?>

PS. O código funciona legal só que não consigo pegar o Id_user e Id_sup para cadastrar como chave estrangeira na tabela acordo.
Ricardo
Re: Erro ao inserir idade session no MYSQL EM PDO
07 de July de 2016 às 09:47PM
Onde você deu atributo ás variáveis $id_user e $id_sup?
Ronaldo Adriano de Souza
Re: Erro ao inserir idade session no MYSQL EM PDO
08 de July de 2016 às 09:30PM
Não entendi bem a pergunta.

Faço uma consulta na tabela users para pegar o Id_user e Id_sup.



//Conecta PDO
$PDO = db_connect();

// Seleciona id_user, id_sup da tabela users se id_user mysql for igual ao id_user da sessão
//e se id_sup mysql for igual ao id_sup da sessão.
$sql = "SELECT id_user, id_sup FROM users WHERE id_user = :id_user AND id_sup = :id_sup";
$stmt = $PDO->prepare($sql);

$stmt->bindParam(':id_user', $id_user);
$stmt->bindParam(':id_sup', $id_sup);

$stmt->execute();

$ids = $stmt->fetchAll(PDO::FETCH_ASSOC);
Você precisa estar logado no PHPBrasil.com para poder enviar mensagens para os nossos fóruns.

Faça o login aqui.