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

Asked

Viewed 676 times

0

I have a form of questions with multiple questions, after the person answer 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 table responses:

resp_fixa_id
resp_fixa_id_perg
resp_fixa_id_cliente
resp_fixa_id_resp
resp_fixa_data_cad

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

In other words, you need to leave it as follows to insert it in the bank:

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 I make this conversion ?

Note: This sequence is the number of questions on the screen that the person answered, more is not stay, can be more or even this 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 general registration systems sugestoes salva.php on line 28

Notice: Undefined index: in C: xampp htdocs general registration systems sugestoes salva.php on line 28

Notice: Undefined index: in C: xampp htdocs general registration systems sugestoes salva.php on line 28

Notice: Undefined index: in C: xampp htdocs general registration systems sugestoes salva.php on line 28

  • Wouldn’t it be better to send the questions and answers in a single variable to treat this as Array in PHP? For example: salva.php?...&respostas=1,3,2,0,3,1 where odd positions are the question id and pairs the id question answer to the left. After receiving the information, you can go through this array and record each question and its answer in the database.

2 answers

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 searching for something. The best would be to use the POST method, this you change in the form within the HTML, through the method attribute of the FORM tag.

The contents of the archive save php. would look 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 more how to capture this information: https://secure.php.net/manual/en/language.variables.external.php

After capturing the information, you need to save in the database, with PHP follows a link with example of how to open a connection to the database and do the Insert: http://www.bosontreinamentos.com.br/php-programming/curso-de-php-inserindo-dados-em-um-banco-de-dados-mysql/

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

  • So this part of moving to post and how to take from the other side and quiet, the biggest doubt and the following that the questions there can be the 24 that is there or 20 or 25 or 30 ... and how to take this variation that is catching me

  • I got it, you can check if the variable exists in POST, type so you want to know if you have up to 24, ai verifies if there is variable 24 in POST. As follows: if(isset($_POST['24']){ //EXISTS}Else{ // NO EXIST} the isset function checks if the variable exists. If the variable always exists, you can check if there is content in it type if($_POST['24'] == ''){//HAS VALUE}Else{//HAS NO VALUE}

  • Okay, I’m gonna test, Thank you

  • 1

    @Otáciobarbosa $_POST is an array, you can loop everything in it with foreach($_POST as $chave=>$valor) { /* ... */ }.

2


It counts 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 have been asked.

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

$total_de_perguntas = count($_POST)-9;
  • @Otáciobarbosa Try to change the $iniciaPerguntas = 1; for $iniciaPerguntas = 9; because it should only give 24 loops in the script, which is the difference between the overall total of fixed posts and the variables.

  • , It worked out, but it was actually a mess of variables that I had done, entered ok, thanks, for the help.

Browser other questions tagged

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