Recover city from database id

Asked

Viewed 128 times

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 their id in the table (only this way to work with the function in AJAX). But at the time of entering I’m not managing to turn that state and city id into the name of the city (select * from cidade where id = $_post[cidade]). By placing a mysqli_query in the PHP variable and then try 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)");

Estrutura da tabela

The first two records were entered manually into the database.

  • What is the error? the data is information you did not enter in the form?

  • It’s actually working, but I just wanted you to enter the name of the city instead of the id.

1 answer

0

There is an error in your insertion:

Note this example the syntax should contain the name of the fields you will insert,

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

Something else in case of routines you need before the Insert effect a select you can do so :

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

This avoids server resource consumption because it minimizes the number of queries, the Insert and the select within the same consultation.

Would that be your relationship between entities:

SELECT * FROM establishment and INNER JOIN cities ON e.cod_city = cities.id_city;

That is to say :

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

Same thing for table states....

  • I think you confused. The select I’m referring to is an HTML field

  • Yes I got confused! But the answer still proceeds if you are collecting the city id and the state of your html form and posting to this php routine will receive the values up to the global variable POST with this just make the INSERT with a select inside collecting the name of the city and the state selected and inserted in the new table.

  • 1

    A detail that is usually a penalty against the performance of the system I do not advise to keep the name of the city and state within the establishment table. In this case the id of the city and state is kept, because they are primary keys of the Tables. And therefore, since all the queries that use these fields are natural, they are streamlined by this fact. This projection you demonstrated will cause a performance impact on your system.

  • So I believe that only storing the Ids would solve my problem (because in the end it will stay the way it is). So it would have to create relations with the city and state table, correct?

  • Yes, when you need to perform the relationship between the establishment table and the city or state, just build a query with Join!

  • I changed my answer check! = P

Show 1 more comment

Browser other questions tagged

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