0
public function executar($sql){
$con = new conexao();
$con->abrir();
$re = $con->mysqli->query($sql);
// Preciso retornar esta informação tambem:
$last_id = $con->mysqli->insert_id;
$con->fechar();
return $re;
}
function inserir($tabela,$dados){
$arrCampo = array_keys($dados);
$arrValores = array_values($dados);
$numCampos = count($arrCampo);
$numValores = count($arrValores);
if($numCampos == $numValores){
$SQL = "INSERT INTO " .$tabela." (";
foreach($arrCampo as $campo){
$SQL .= "$campo, ";
}
$SQL = substr_replace($SQL, ")", -2, 1);
$SQL .= "VALUES (";
foreach($arrValores as $valores){
$SQL .= "'".$valores."', ";
}
$SQL = substr_replace($SQL, ")", -2, 1);
}else{
echo "Erro ao verificar campos";
}
$this->executar($SQL);
}
I am using the following codes, and need to return the ID of the last insertion in the database.
$dados_cliente = array(
'cliente_nome'=>$cliente_nome,
'cliente_email'=>$cliente_email,
'cliente_celular'=>$cliente_celular,
'cliente_tipo'=>$cliente_tipo,
'cliente_documento'=>$cliente_documento
);
$comando->inserir('clientes',$dados_cliente);
Which is sent this way. Only after executing the command $comando->inserir('clientes',$dados_cliente);
I can’t get the value of the last bank insert.
Select Max (column)+1 from table
– Willian Coqueiro
@The problem with this solution is that the ID will not always be MAX+1. If records from the end of the IDS list are deleted, the autonumbering will not reuse the numbers, but the MAX yes, which may be unwanted.
– Bacco
@Bacco Yes. If using auto increment. Generally do not use this function. More if it uses after the Insert only MAX achieves the correct values.
– Willian Coqueiro