How to access a vector within a PHP obejto and insert the data into the Mysql database

Asked

Viewed 28 times

0

I would like to know how to access the array of addresses within this object:

foto do vetor

And insert the attributes inside a table in Mysql, I am doing it as follows

 foreach ($this->endereco as $elemento) {
            $nome = $elemento['nome'];
            $logradouro = $elemento['logradouro'];
            $bairro = $elemento['bairro'];
            $numero = $elemento['numero'];
            $cidade = $elemento['cidade'];
            $estado = $elemento['estado'];
            $cep = $elemento['cep'];
            echo $nome;
            $query = $query."('".$nome."','".$logradouro."','".$bairro."','".$numero."','".$cidade."', '".$estado."', '".$cep."', '".$id."'),";
        }

However the query is as follows:

INSERT INTO endereco_juridica(nome, logradouro, bairro, numero, cidade, estado, CEP, juridica_id_juridica) VALUES('campus pici','','','','', '', '', ''),('sede','','','','', '', '', ''),('','AV DA UNIVERSIDADE','','','', '', '', ''),('','R VERGUEIRO','','','', '', '', ''),('','','','2853','', '', '', ''),('','','','123','', '', '', ''),('','','BENFICA','','', '', '', ''),('','','mucambo','','', '', '', ''),('','','','','FORTALEZA', '', '', ''),('','','','','SAO PAULO', '', '', ''),('','','','','', 'Selecione o estado', '', ''),('','','','','', 'SP', '', ''),('','','','','', '', '60.020-181', ''),('','','','','', '', '34213123', ''),"

I collect the data coming from the forumlarium of this froma:

foreach ($_POST['nome'] as $nome) {
    $empresa->endereco[] = array('nome' => $nome);
}

foreach ($_POST['logradouro'] as $logradouro) {
    $empresa->endereco[] = array('logradouro' => $logradouro);
}

foreach ($_POST['numero'] as $numero) {
    $empresa->endereco[] = array('numero' => $numero);
}


foreach ($_POST['bairro'] as $bairro) {
    $empresa->endereco[] = array('bairro' => $bairro);
}


foreach ($_POST['cidade'] as $cidade) {
    $empresa->endereco[] = array('cidade' => $cidade);
}

foreach ($_POST['estado'] as $estado) {
    $empresa->endereco[] = array('estado' => $estado);
}

foreach ($_POST['cep'] as $cep) {
    $empresa->endereco[] = array('cep' => $cep);
}

If anyone has any ideas I’ll be very grateful, I’ve tried everything.

1 answer

0

I was able to solve the problem, I was making the vector construction in the wrong way. It’s working this way:

$tam = 0;
foreach ($_POST['nome'] as $nomepost) {
    $nome = $nomepost;
    $logradouro = $_POST['logradouro'][$tam];
    $numero = $_POST['numero'][$tam];
    $bairro = $_POST['bairro'][$tam];
    $cidade = $_POST['cidade'][$tam];
    $estado = $_POST['estado'][$tam];
    $cep = $_POST['cep'][$tam];  
    $empresa->endereco[] = array('nome' => $nome, 'logradouro' => $logradouro, 'numero'=> $numero, 'bairro' => $bairro, 'cidade' => $cidade, 'estado'=> $estado, 'cep'=> $cep);
    $tam++;
}

Browser other questions tagged

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