How to separate the data from a GET request and insert into the mysql database with php

0

I have a question form with several questions, after the respond person has a save button that sends the answers and questions to the salva.php

  

Follow the get (url):

salva.php?
data_cadastro=2017-07-08 
& cli_id = 1
& cli_nome=OTACIO+BARBOSA
& cli_end=RUA+A
& cli_tel=3333333333
& cli_data_nasc=1900-01-01
& cli_cidade=TESTE
& cli_estado=MG
& usu_id = 1
& 1=3
& 2=0
& 3=1
& 4=5
& 5=3
& 6=1
& 7=0
& 8=3
& 9=0
& 10=1
& 11=5
& 12=5
& 13=5
& 14=3
& 15=3
& 16=0
& 17=0
& 18=1
& 19=1
& 20=0
& 21=0
& 22=1
& 23=3
& 24=5

I need to save this data in the responses table:

resp_fixa_id
resp_fixa_id_perg
resp_fixa_id_cliente
resp_fixa_id_resp
resp_fixa_data_cad

Being as follows:

resp_fixa_id          = data_cadastro
resp_fixa_id_perg     = ( é o sequencial mmostrado de 1 a 24 )
resp_fixa_id_cliente  = cli_id
resp_fixa_id_resp     = é o que vem na frente do sequencial (1 = 3 (no caso seria o 3))
resp_fixa_data_cad    =  data_cadastro

That is, you need to leave it as follows to insert in the database:

INSERT INTO resposta (resp_fixa_id,resp_fixa_id_perg,resp_fixa_id_cliente,resp_fixa_id_resp,resp_fixa_data_cad) 
                        VALUES('NULL','1','1','3','2017-07-08');
INSERT INTO resposta (resp_fixa_id,resp_fixa_id_perg,resp_fixa_id_cliente,resp_fixa_id_resp,resp_fixa_data_cad) 
                        VALUES('NULL','2','1','0','2017-07-08');
INSERT INTO resposta (resp_fixa_id,resp_fixa_id_perg,resp_fixa_id_cliente,resp_fixa_id_resp,resp_fixa_data_cad) 
                        VALUES('NULL','2','1','1','2017-07-08');

How could you do this conversion?

Note: This sequence is the number of questions on the screen that the person answered, but it is not fico, it can be more or even that amount.

Complement:

$total_de_perguntas = count($_POST)-9;

$VarLoja            = $_POST['loja'];
$VarDataCadastro    = $_POST['data_cadastro'];
$VarUsuario         = $_POST['usuario'];
$VarNome            = $_POST['cli_nome'];
$VarEndereco        = $_POST['cli_end'];
$VarTelefone        = $_POST['cli_tel'];
$VarDataNascimento  = $_POST['cli_data_nasc'];
$VarCidade          = $_POST['cli_cidade'];
$VarEstado          = $_POST['cli_estado'];

$iniciaPerguntas = 1; 

while($iniciaPerguntas <= $total_de_perguntas) {


    $VarRespostas =  $_POST["$iniciaPerguntas"];

    $VarString = '$Query'.$iniciaPerguntas = "INSERT INTO sug_respostas (resp_fixa_id,
    resp_fixa_id_perg,
    resp_fixa_id_cliente,
    resp_fixa_id_usu,
    resp_fixa_id_resp,
    resp_fixa_data)
    VALUES (NULL,
    '$iniciaPerguntas',
    '$VarNewNumClientes',
    '$VarUsuario',
    '$VarRespostas',
    '$VarDataCadastro');"; 



    $Result = '$Result'.$iniciaPerguntas = $conn->query($VarString);

    $iniciaPerguntas++; 

}
  

Notice: Undefined index: in   C: \ xampp \ htdocs \ systems \ general_administration \ suggestions \ salva.php on line 28

     

Notice: Undefined index: in   C: \ xampp \ htdocs \ systems \ general_administration \ suggestions \ salva.php on line 28

     

Notice: Undefined index: in   C: \ xampp \ htdocs \ systems \ general_administration \ suggestions \ salva.php on line 28

     

Notice: Undefined index: in   C: \ xampp \ htdocs \ systems \ general_administration \ suggestions \ salva.php on line 28

    
asked by anonymous 07.08.2017 / 19:08

2 answers

2

Count the total number of variables sent in $ _POST and decreases by the number of variables that are fixed. With this result, you will know how many questions were asked.

In your case, you have 9 fixed variables and 33 in total. Just do the following:

$total_de_perguntas = count($_POST)-9;
    
07.08.2017 / 20:48
5

For this amount of information, it is not advisable to use the GET method because of the 1024 character limit and it is also good practice to use this method when fetching something. The best would be to use the POST method, this you change in the form inside the HTML, through the method attribute of the FORM tag.

The content of the save.php file looks like this:

// CODIGO PHP RETORNA AS INFORMAÇÕES DO POST
$client_id = $_POST['cli_id'];
$cli_nome = $_POST['cli_nome'];

.... (so on the rest of the information)

Follow the link explaining how to capture this information: link

After capturing the information, you need to save to the database, with PHP follows a link with example of how to open a connection to the database and make the insert: link

Note: I hope to have helped with this information, I did not quite understand the level of knowledge you have of PHP and then I tried to leave everything well structured with a link for consultation.

    
07.08.2017 / 19:29