How to send data from an HTML page to Mysql?

Asked

Viewed 452 times

0

I made a registration form, but when sending the values to the database, it does not fill the table and returns no error.

Code I used was the following:

<body>
    <?php
        $conexao = mysqli_connect("localhost", "root", "","villadobem")
    ?>

    <?php 
    // RECEBENDO OS DADOS PREENCHIDOS DO FORMULÁRIO !
    $nome   = $_POST ["nome"];
    $data   = $_POST ["data"];
    $sexo   = $_POST ["sexo"];
    $email  = $_POST ["email"];
    $email2 = $_POST ["email2"];
    $senha  = $_POST ["senha"];
    $senha1 = $_POST ["senha1"];
    $cpf    = $_POST ["cpf"];
    $tel    = $_POST ["tel"];
    $cel    = $_POST ["cel"];
    $cep    = $_POST ["cep"];
    $rua    = $_POST ["rua"];
    $numero = $_POST ["numero"];
    $bairro = $_POST ["bairro"];
    $cidade = $_POST ["cidade"];
    $uf     = $_POST ["uf"];
    $instituiçao    = $_POST ["instituição"];
    $nivel  = $_POST ["nivel"];
    $curso  = $_POST ["curso"];
    $area   = $_POST ["area"];
    $turno  = $_POST ["turno"];
    $palestras  = $_POST ["palestras"];
    $cidade2    = $_POST ["cidade2"];

    if(!empty($_POST['dias']) && count($_POST['dias']) ){
       $chgeckboxes = $_POST['dias'];
       print_r($chgeckboxes);
       //implode
       $dias = implode(',', $_POST['dias']);
    }

    if(!empty($_POST['hora']) && count($_POST['hora']) ){
       $chgeckboxes = $_POST['hora'];
       print_r($chgeckboxes);
       //implode
       $hora = implode(',', $_POST['hora']);
    }

    if(!empty($_POST['Habilidades']) && count($_POST['Habilidades']) ){
       $chgeckboxes = $_POST['Habilidades'];
       print_r($chgeckboxes);
       //implode
       $habilidades = implode(',', $_POST['Habilidades']);

    //rotina para gravação no DB

    }
    //Gravando no banco de dados !
    mysqli_query($conexao, "insert into voluntários (nome, data, sexo, email, email2, senha, senha1, cpf, tel, cel, cep, rua, numero, bairro, cidade, uf, instituição, nivel, curso, area, turno, dias, hora, habilidades, palestras, cidade2) values ('$nome', '$data', '$sexo', '$email', '$email2', '$senha', '$senha1', '$cpf', '$tel', '$cel', '$cep', '$rua', '$numero', '$bairro', '$cidade', '$uf', '$instituiçao', '$nivel', '$curso', '$area', '$turno', '$dias', '$hora', '$habilidades', '$palestras', '$cidade2')");
    ?>
</body>

What could be wrong?

  • You checked with mysqli_error()? And it has variable with cedilla and table name with accent, is that right? It may be accepted, but I wouldn’t risk.

  • It was the cedilla and the same accent, I took it right there it worked, thank you, helped too!!

  • 2

    phpmyadmin has no direct relation to your application, it is just an extra management tool, which can be easily replaced by other similar tools, however the bank remains the same, take a look at it just out of curiosity: What’s the difference between Mysql and phpMyAdmin?

2 answers

1

Always escape the data you want to send in SQL... prevents errors regarding accentuation and special characters.

mysqli_query($conexao, "insert into `voluntários` (`nome`, `data`, `sexo`, ...

It is not in your problem, but I wanted to leave a tip here for you very cool, for you avoid many lines. That is the function extract

<?php
  extract($_POST);
  /* esse carinha vai fazer isso aqui pra você:
    $nome   = $_POST ["nome"];
    $data   = $_POST ["data"];
    $sexo   = $_POST ["sexo"];

    Ele converte a sua chave do array em variável
  */
  • No such amendment is necessary

0

When entering data it is important to keep in mind the type of data you are entering.

1 is different from '1'. 1 is a numerical value, and '1' is a string (A string).

Let’s assume the following example:

Tabela Exemplo(
Id int,
Nome varchar(100),
data date
)

When entering data in it we would do as follows:

insert into Exemplo (id, nome, date) values ($id, '$nome', '$date')

Note: The Field Nome and Data are inserted between single quotes because these fields are inserted into the database as String. Already the id does not need, as it is a whole field.

Browser other questions tagged

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