I have an inheritance in my work and I'm having trouble making it work. Tables Example
CREATE TABLE IF NOT EXISTS pessoa (
idpessoa INT NOT NULL AUTO_INCREMENT,
tipopessoa VARCHAR(45) NOT NULL,
PRIMARY KEY (idpessoa)
);
CREATE TABLE IF NOT EXISTS juridica (
idjuridica INT NOT NULL AUTO_INCREMENT,
cnpj VARCHAR(25) NOT NULL,
inscrestad VARCHAR(45) NOT NULL,
razaosocial VARCHAR(150) NOT NULL,
nomefantasia VARCHAR(150) NOT NULL,
pessoa_idpessoa INT NOT NULL ,
PRIMARY KEY (idjuridica),
FOREIGN KEY (pessoa_idpessoa) REFERENCES pessoa (idpessoa)
);
Well, this is a part of the inheritance that I have, I'm trying to register a Legal person, but first I have to register the Person. I'm using PHP (learning now) and the code is the following for inserting a legal person.
Connection to the bank:
function abrirBanco(){
$connection = new mysqli("localhost","root","","apsbd1");
if($connection->connect_error){
die("Conexão com o banco falhou: " . $connection->connect_error);
}
return $connection;
}
Person and Legal INSERT:
function inserirPJuridica(){
$banco = abrirBanco();
//Insere uma Pessoa;
$sql = "INSERT INTO pessoa (tipopessoa) VALUES ('Juridica')";
$banco->query($sql);
//Seleciona o ID da Pessoa;
$sql = "SELECT idpessoa FROM pessoa ORDER BY idpessoa DESC LIMIT 1";
$res_idpessoa = $banco->query($sql);
//Insere a Conexão entre Pessoa e Jurídica;
$sql = "INSERT INTO juridica (cnpj, inscrestad, razaosocial, nomefantasia, pessoa_idpessoa) VALUES ('{$_POST["cnpj"]}', '{$_POST["inscrestad"]}', '{$_POST["razaosocial"]}', '{$_POST["nomefantasia"]}', '$res_idpessoa'";
$banco->query($sql);
The person I can insert into the bank, but when I insert the Legal it gives an error
Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\apsbd1\inc\funcoes.php on line 302
Line 302 is the line of $ res_idpessoa = $ banco-> query ($ sql);
Probably the query is not a string that can be used in an INSERT, how do I then insert the person id from the person table into the legal person's person?