Error trying to execute PDO Insert function - PHP

0

I'm trying to create a PDO function to execute two inserts, one after the other, but I'm having trouble.

The function code is:

public function double_cad(array $dados){
    $pdo = parent::getDB();
    $double_cad = $pdo->prepare("insert into login(nome, login, senha, role)values(?,?,?,?)");
    $double_cad->bindValue(1, $dados[0]);
    $double_cad->bindValue(2, $dados[1]);
    $double_cad->bindValue(3, $dados[2]);
    $double_cad->bindValue(4, $dados[3]);
    $double_cad->execute();

    $id_usr = $pdo->lastInsertId();

    $double_cad = $pdo->prepare("insert into transacoes(codigo_transacao, referencia, status_transacao, ".$id_usr. ", id_servico, tipo_pagamento, data_transacao)values(?,?,?,?,?,?,?)");
    $double_cad->bindValue(1, $dados[0]);
    $double_cad->bindValue(2, $dados[1]);
    $double_cad->bindValue(3, $dados[2]);
    $double_cad->bindValue(4, $dados[3]);
    $double_cad->bindValue(5, $dados[4]);
    $double_cad->bindValue(6, $dados[5]);
    $double_cad->bindValue(7, $dados[6]);
    $double_cad->execute();

}

What I'm trying to do is insert the user's data into the "login" table, retrieve his id, and then insert into the "transactions" table the transaction data for that user (the line "$ id_usr = $ pdo -> lastInsertId (); "is just to retrieve the id of the user to insert as foreign key in the transaction table).

However, running this function displays the error:

  

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE [42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server for the right syntax to use near '1, server_id, pay_type, data_transaction) values (' user name ',' user email 'at line 1' in C: \ wamp \ www \ xxxx \ lib \ class \ Transacao.php on line 44

Line 44 is just the second run of the function. How to solve?

    
asked by anonymous 24.10.2015 / 16:08

1 answer

1

The syntax of the insert is:

INSERT INTO TABELA (CAMPO1, CAMPO2, CAMPO3) VALUES (VALOR_CAMPO1, VALOR_CAMPO2...)

But in this line you use the id that returned from the last query to list the columns:

insert into transacoes(codigo_transacao, referencia, status_transacao, ".$id_usr. ", id_servico, tipo_pagamento, data_transacao)values(?,?,?,?,?,?,?)");

Generating an insert in column "1" for example so if you do not have a column with the name 1 it will not insert.

    
24.10.2015 / 18:09