Recover town from database id

0

I'm getting information from a select, which pulls the states and cities registered in the database. In the value of each select (city and state) is the id of them in the table (only this way to work with the function in AJAX). But at the time of inserting I am not able to make this id of the state and city in the name of the city ( select * from cidade where id = $_post[cidade] ). Putting mysqli_query into the PHP variable and then trying to insert it gives an error.

$cidade = $_POST['cidade'];
        $estado = $_POST['estado'];

        //Validando dados do formulário
        if(!empty($nome) && !empty($endereco)  && !empty($num) && !empty($CEP) && !empty($bairro) && !empty($cidade) && !empty($estado)){
            //Caso todos os campos sejam preenchidos, insere os valores na tabela estabelecimento
            mysqli_query($conectar, "INSERT INTO estabelecimento VALUES (NULL, '$nome', '$CNPJ', '$endereco',
            '$num', '$comp', '$CEP', '$bairro', '$cidade', '$estado', 0)");

The first two records were entered manually into the database.

    
asked by anonymous 10.07.2017 / 16:52

1 answer

0

You have an error inserting:

Notice this example syntax should contain the name of the fields you are going to insert,

mysqli_query($con,"INSERT INTO pessoa ($FirstName,$LastName,$Age) 
VALUES ('Glenn','Quagmire',33)");

Another thing for routines that you need before insert you can do a select :

mysqli_query($con,"INSERT INTO pessoa ($FirstName,$LastName,$Age) 
SELECT 'Glenn','Quagmire',33 FROM pessoa WHERE id=12";

This avoids consuming server resources because it minimizes the number of queries, insert and select within the same query.

Your relationship between entities would look like this:

  

SELECT * FROM SETTING AND INNER JOIN cities ON and .cod_city = cities.id_city;

That is:

  

SELECT ALL THE FIELDS OF THE ESTABLISHMENT TABLES AND CITIES WHERE cod_city (known as FK foreign key of another table) is equal to the primary key_city id_city of the cities table.

Same thing for table states ....

    
10.07.2017 / 18:31