Html form for mysql database does not save

Asked

Viewed 438 times

1

I’m a beginner in PHP, and as many am already full of doubts and seeking to learn some techniques and so on, I’m using a template of a form I want to play in the database, the same will not, when I click to send, says it was successful but does not record in the bank... Can someone there give me a hand? I will post the php code q should record in the bank. Thanks for the attention! ; )

Code of the file I created: grava_cheklist.php

<?php
    include "conexao.php";

    $responsavel = $_POST['responsavel'];
    $placa = $_POST['placa'];
    $veiculo = $_POST['veiculo'];
    $cor = $_POST['cor'];
    $combustivel = $_POST['combustivel'];
    $pneus = $_POST['pneus'];
    $kilometragem = $_POST['kilometragem'];
    $paradiante = $_POST['paradiante'];
    $teto = $_POST['teto'];
    $traseira = $_POST['traseira'];
    $paratraseiro = $_POST['paratraseiro'];
    $paralamad = $_POST['paralamad'];
    $paralamae = $_POST['paralamae'];
    $portam = $_POST['portam'];
    $portac = $_POST['portac'];
    $ptrasd = $_POST['ptrasd'];
    $ptrase = $_POST['ptrase'];
    $parabrisa = $_POST['parabrisa'];
    $dia = $_POST['dia'];
    $mes = $_POST['mes'];
    $ano = $_POST['ano'];
    $nome_cliente = $_POST['nome_cliente'];
    $sobrenome = $_POST['sobrenome'];
    $telefone = $_POST['telefone'];
    $bairro = $_POST['bairro'];
    $cidade = $_POST['cidade'];
    $documento = $_POST['documento'];
    $email = $_POST['email'];
    $observacao = $_POST['observacao'];

    $result_cadastro = "INSERT INTO cadastro(responsavel, placa, veiculo, cor, combustivel, pneus, kilometragem, paradiante, teto, traseira, paratraseiro, paralamad, paralamae, portam, portac, ptrasd, ptrase, parabrisa, dia, mes, ano, nome_cliente, sobrenome, telefone, bairro, cidade, documento, email, observacao) VALUES ('$responsavel','$placa','$veiculo','$cor','$combustivel','$pneus','$kilometragem','$paradiante','$teto','$traseira','$paratraseiro','$paralamad','$paralamae','$portam','$portac','$ptrasd','$ptrase','$parabrisa','$dia','$mes','$ano','$nome_cliente','$sobrenome','$telefone','$bairro','$cidade','$documento','$email','$observacao')";
    $resultado_cadastro = mysqli_query($conn, $result_cadastro); 
    if(mysqli_affected_rows($conn) != 0){
                echo "Veiculo cadastrado com Sucesso";
            }else{
                echo "O veiculo não foi cadastrado com Sucesso.";   
     }

?>

this is the form:

            <form method="POST" action="grava_cheklist.php">
                <ul class="field-list">
                    <li>
                        <label class="form-label">
                          Responsável:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="responsavel" required>
                                <option value="">&nbsp;</option>
                                <option value="André"> André </option>
                                <option value="Tibolla"> Avelino Tibolla </option>
                                <option value="Atendimento"> Atendimento </option>
                                <option value="cliente"> Cliente </option>
                            </select>
                        </div>
                        </li>
                  <li>
                     <label class="form-label"> 
                           Placa:
                            <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <input type="text" name="placa" maxlength="7" placeholder="Entrar com a placa do carro" required >
                        </div>
                    </li>
                    <li>
                     <label class="form-label"> 
                           Veículo:
                            <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <input type="text" name="veiculo" maxlength="20" placeholder="Entrar com o nome do carro" required >
                        </div>
                    </li>
                    <li>
                        <label class="form-label">
                           Cor:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="cor" required>
                                <option value="">&nbsp;</option>
                                <option value="Branco"> Branco </option>
                                <option value="Preto"> Preto </option>
                                <option value="Cinza"> Cinza </option>
                                <option value="Vermelho"> Vermelho </option>
                                <option value="Marrom"> Marrom </option>
                                <option value="Verde"> Verde </option>
                                <option value="Amarelo"> Amarelo </option>
                                <option value="Bordo"> Bordo </option>
                                <option value="Cinza"> Cinza </option>
                                <option value="Azul"> Azul </option>
                                <option value="outros"> Outros </option>
                            </select>
                        </div>
                        </li>
                        <li>
                        <label class="form-label">
                           Combustível:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="combustivel" required>
                                <option value="">&nbsp;</option>
                                <option value="reseva"> reserva </option>
                                <option value="cheio"> Cheio </option>
                                <option value="1/3"> 1/3 </option>
                                <option value="3/4"> 3/4 </option>
                                <option value="meio"> meio </option>
                                <option value="vazio"> Vazio </option>
                                <option value="outros"> Outros </option>
                            </select>
                        </div>
                            </li>
                        <li>
                        <label class="form-label">
                           Pneus:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="pneus" required>
                                <option value="">&nbsp;</option>
                                <option value="bons"> Bons </option>
                                <option value="ruins"> Ruins </option>
                                <option value="novos"> Novos </option>
                                <option value="outros"> Outros </option>
                            </select>
                          </div>
                       </li>
                     <li> 
                        <label class="form-label">
                           Kilometragem:
                          <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <input type="text" name="kilometragem" maxlength="20" placeholder="Kilometragem" required>
                        </div>
                    </li>
                    <li>
                        <label class="form-label">
                           Parachoque Diant.:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="paradiante" required>
                                <option value="">&nbsp;</option>
                              <option value="OK"> OK </option>
                              <option value="Risco"> Risco </option>
                              <option value="Amassado"> Amassado </option>
                              <option value="Manchado"> Manchado </option>
                              <option value="Quebrado"> Quebrado </option>
                              <option value="Ver Nas Observações"> Ver Nas Observações </option>
                            </select>
                        </div>
                        </li>
                     <li>
                        <label class="form-label">
                           Teto:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="teto" required>
                                <option value="">&nbsp;</option>
                                <option value="OK"> OK </option>
                                <option value="Risco"> Risco </option>
                                <option value="Amassado"> Amassado </option>
                                <option value="Manchado"> Manchado </option>
                                <option value="Quebrado"> Quebrado </option>
                                <option value="Ver Nas Observações"> Ver Nas Observações </option>
                            </select>
                        </div>
                         </li>
                     <li>
                        <label class="form-label">
                           Tampa Traseira:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="traseira" required>
                                <option value="">&nbsp;</option>
                                <option value="OK"> OK </option>
                                <option value="Risco"> Risco </option>
                                <option value="Amassada"> Amassada </option>
                                <option value="Manchada"> Manchada </option>
                                <option value="Quebrado"> Quebrada </option>
                                <option value="Ver Nas Observações"> Ver Nas Observações </option>
                            </select>
                        </div>
                         </li>
                     <li>
                        <label class="form-label">
                           Parachoque Tras.:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="paratraseiro" required>
                                <option value="">&nbsp;</option>
                                <option value="OK"> OK </option>
                                <option value="Risco"> Risco </option>
                                <option value="Amassado"> Amassado </option>
                                <option value="Manchado"> Manchado </option>
                                <option value="Quebrado"> Quebrado </option>
                                <option value="Ver Nas Observações"> Ver Nas Observações </option>
                            </select>
                        </div>
                         </li>
                     <li>
                        <label class="form-label">
                           Paralama Dir.:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="paralamad" required>
                                <option value="">&nbsp;</option>
                                <option value="OK"> OK </option>
                                <option value="Risco"> Risco </option>
                                <option value="Amassado"> Amassado </option>
                                <option value="Manchado"> Manchado </option>
                                <option value="Quebrado"> Quebrado </option>
                                <option value="Ver Nas Observações"> Ver Nas Observações </option>
                            </select>
                        </div>
                         </li>
                     <li>
                        <label class="form-label">
                           Paralama Esq.:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="paralamae" required>
                                <option value="">&nbsp;</option>
                                <option value="OK"> OK </option>
                                <option value="Risco"> Risco </option>
                                <option value="Amassado"> Amassado </option>
                                <option value="Manchado"> Manchado </option>
                                <option value="Quebrado"> Quebrado </option>
                                <option value="Ver Nas Observações"> Ver Nas Observações </option>
                            </select>
                        </div>
                         </li>
                     <li>
                        <label class="form-label">
                           Porta do Motorista:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="portam" required>
                                <option value="">&nbsp;</option>
                                <option value="OK"> OK </option>
                                <option value="Risco"> Risco </option>
                                <option value="Amassada"> Amassada </option>
                                <option value="Manchada"> Manchada </option>
                                <option value="Quebrada"> Quebrada </option>
                                <option value="Ver Nas Observações"> Ver Nas Observações </option>
                            </select>
                        </div>
                         </li>
                      <li>
                        <label class="form-label">
                           Porta do Carona:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="portac" required>
                                <option value="">&nbsp;</option>
                                <option value="OK"> OK </option>
                                <option value="Risco"> Risco </option>
                                <option value="Amassada"> Amassada </option>
                                <option value="Manchada"> Manchada </option>
                                <option value="Quebrada"> Quebrada </option>
                                <option value="Ver Nas Observações"> Ver Nas Observações </option>
                            </select>
                        </div>
                          </li>
                     <li>
                        <label class="form-label">
                           Porta tras. Dir.:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="ptrasd" required>
                                <option value="">&nbsp;</option>
                                <option value="OK"> OK </option>
                                <option value="Risco"> Risco </option>
                                <option value="Amassada"> Amassada </option>
                                <option value="Manchada"> Manchada </option>
                                <option value="Quebrada"> Quebrada </option>
                                <option value="Sem esta Porta"> Sem esta Porta </option>
                                <option value="Ver Nas Observações"> Ver Nas Observações </option>
                            </select>
                        </div>
                         </li>
                     <li>
                        <label class="form-label">
                           Porta tras. Esq.:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="ptrase" required>
                                <option value="">&nbsp;</option>
                                <option value="OK"> OK </option>
                                <option value="Risco"> Risco </option>
                                <option value="Amassada"> Amassada </option>
                                <option value="Manchada"> Manchada </option>
                                <option value="Quebrada"> Quebrada </option>
                                <option value="Sem esta Porta"> Sem esta Porta </option>
                                <option value="Ver Nas Observações"> Ver Nas Observações </option>
                            </select>
                        </div>
                         </li>
                      <li>
                        <label class="form-label">
                           Parabrisa:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <select class="form-dropdown" name="parabrisa" required>
                                <option value="">&nbsp;</option>
                                <option value="OK"> OK </option>
                                <option value="Risco"> Risco </option>
                                <option value="Trincado"> Trincado </option>
                                <option value="Manchado"> Manchado </option>
                                <option value="Quebrado"> Quebrado </option>
                                <option value="Ver Nas Observações"> Ver Nas Observações </option>
                            </select>
                        </div>
                    </li>
                     <li>
                        <label class="form-label">
                           Data de Hoje:
                          <span class="form-required"> * </span>
                        </label>
                        <div class="form-input dob">
                            <span class="form-sub-label">
                                <select name="dia" class="Dia">
                                    <option>&nbsp;</option>
                                    <option value="1"> 1 </option>
                                    <option value="2"> 2 </option>
                                    <option value="3"> 3 </option>
                                    <option value="4"> 4 </option>
                                    <option value="5"> 5 </option>
                                    <option value="6"> 6 </option>
                                    <option value="7"> 7 </option>
                                    <option value="8"> 8 </option>
                                    <option value="9"> 9 </option>
                                    <option value="10"> 10 </option>
                                    <option value="11"> 11 </option>
                                    <option value="12"> 12 </option>
                                    <option value="13"> 13 </option>
                                    <option value="14"> 14 </option>
                                    <option value="15"> 15 </option>
                                    <option value="16"> 16 </option>
                                    <option value="17"> 17 </option>
                                    <option value="18"> 18 </option>
                                    <option value="19"> 19 </option>
                                    <option value="20"> 20 </option>
                                    <option value="21"> 21 </option>
                                    <option value="22"> 22 </option>
                                    <option value="23"> 23 </option>
                                    <option value="24"> 24 </option>
                                    <option value="25"> 25 </option>
                                    <option value="26"> 26 </option>
                                    <option value="27"> 27 </option>
                                    <option value="28"> 28 </option>
                                    <option value="29"> 29 </option>
                                    <option value="30"> 30 </option>
                                    <option value="31"> 31 </option>
                                </select>
                                <label class="form-sub-label1"> Dia </label>
                            </span>
                          <span class="form-sub-label">
                                <select name="mes" class="mes">
                                    <option>&nbsp;</option>
                                    <option value="Janeiro"> Janeiro </option>
                                    <option value="Fevereiro"> Fevereiro </option>
                                    <option value="Março"> Março </option>
                                    <option value="Abril"> Abril </option>
                                    <option value="Maio"> Maio </option>
                                    <option value="Junho"> Junho </option>
                                    <option value="Julho"> Julho </option>
                                    <option value="Agosto"> Agosto </option>
                                    <option value="Setembro"> Setembro </option>
                                    <option value="Outubro"> Outubro </option>
                                    <option value="Novembro"> Novembro </option>
                                    <option value="Dezembro"> Dezembro </option>
                                 </select>
                                <label class="form-sub-label1"> Mês </label>
                            </span> 
                            <span class="form-sub-label">
                                <input type="text" class="year" name="ano" size="4" maxlength="4" placeholder="Ano" required>
                                <label class="form-sub-label1"> Ano </label>
                            </span>
                        </div>
                    </li>
                    <li> 
                        <label class="form-label">
                           Cliente Nome/Sobrenome:
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input add">
                            <span class="form-sub-label">
                                <input type="text" name="nome_cliente" maxlength="50" placeholder="Nome" required>
                                <label class="form-sub-label1"> Nome </label>
                            </span>
                            <span class="form-sub-label">
                                <input type="text" name="sobrenome" maxlength="50" placeholder="Sobrenome" required>
                                <label class="form-sub-label1"> Sobrenome </label>
                            </span>
                            <span class="form-sub-label">
                                <input type="text" name="telefone" placeholder="Telefone" size="11" maxlength="11" required>
                                <label class="form-sub-label1"> Telefone </label>
                            </span>
                            <span class="form-sub-label">
                                <input type="text" name="bairro" placeholder="Bairro" maxlength="20" required>
                                <label class="form-sub-label1"> Bairro </label>
                            </span>
                            <span class="form-sub-label">
                                <input type="text" name="cidade" placeholder="Cidade" maxlength="20" required>
                                <label class="form-sub-label1"> Cidade </label>
                            </span>
                            <span class="form-sub-label">
                                <input type="text" name="documento" placeholder="Documento" size="11" maxlength="11" required>
                                <label class="form-sub-label1"> Documento </label>
                            </span>
                        </div>
                    </li>
                    <li> 
                        <label class="form-label">
                           E-Mail do Cliente
                           <span class="form-required"> * </span>
                        </label>
                        <div class="form-input">
                            <input type="email" name="email" placeholder="[email protected]" maxlength="50" required>
                        </div>
                    </li>
                    <li> 
                        <label class="form-label1">
                            Observações do Chek-list do Veículo
                        </label>
                        <div class="form-input2">
                            <textarea rows="5" cols="20" name="observacao" maxlength="200"></textarea>
                        </div>
                    </li>
                    <li class="last-type"> 
                        <label class="form-label1">
                            Atesto Chek-List acima devidamente preenchido e assino o presente.<br><br>
                            Responsável: ..........................................................<br><br>
                            Cliente: ...................................................................<br><br> 
                            <span class="form-required">Cliente acompanhou e assinou o Chek-List concordando, insentando a Mecânica Tibolla de qualquer coisa que por ventura venha a ser visto posteriormente.</span>
                        </label>
                        <div class="form-input2">
                            <input type="checkbox" name="concordo" ><label class="type-of-test"></label>
                        </div>
                    </li>
                <input type="submit" name="cadastrar" value="Gravar Chek-List">
                </ul>
            </form>
        </div>
    </div>
</div>

  • The probability of making a mistake with so many fields is considerable. I advise you to give a var_dump about the $result_cadastro to see if the query is correct or not. You can even execute the mysqli_query followed by or die(mysqli_error($conn)); to realize the error you have, if you are in insert clear-cut

  • place your form

  • First of all thank you for your attention! I put "die" as suggested and received the following message: Notice: Use of Undefined Constant mysqli_query - assumed 'mysqli_query'

  • <label class="form-label"> Responsible: <span class="form-required"> * </span> </label> <div class="form-input"> <select class="form-dropdown" name="responsavel" required> <option value="">&nbsp;</option> <option value="André"> André </option> <option value="Tibolla"> Avelino Tibolla </option> <option value="Service"> Service </option> <option value="client"> Client </option> </select> </div> </li>

  • Your question was not removed, what was removed was the answer. To add more details the question click the edit button or this link

1 answer

0

Good in your form your tag <form> must be this way

<form action='grava_ckecklist.php' method='POST'>

And your inputs must have identical Names to match your POSTS, because the $_POST nothing more than a variable that receives the data coming from the form and then stores it in a variable to insert into the bank. Check if your inputs are like this, as in the example below.

<input type='text' name='responsavel'>
<input type='text' name='placa'>
<input type='text' name='veiculo'>

And so that your POST and receive the data from the form, it must be identical to the names of their inputs, as in the example below.

$responsavel = $_POST['responsavel'];
$placa = $_POST['placa'];
$veiculo = $_POST['veiculo'];

And now we go to INSERT, I took a test here and it worked just with your INSERT, so I’ll put down too

$result_cadastro = "INSERT INTO cadastro (responsavel, placa, veiculo, cor, 
combustivel, pneus, kilometragem, paradiante, teto, traseira, paratraseiro, 
paralamad, paralamae, portam, portac, ptrasd, ptrase, parabrisa, dia, mes, 
ano, nome_cliente, sobrenome, telefone, bairro, cidade, documento, email, 
observacao) VALUES ('$responsavel', '$placa', '$veiculo', '$cor', 
'$combustivel', '$pneus', '$kilometragem', '$paradiante', '$teto', 
'$traseira', '$paratraseiro', '$paralamad', '$paralamae', '$portam', 
'$portac', '$ptrasd', '$ptrase', '$parabrisa', '$dia', '$mes', '$ano', 
'$nome_cliente', '$sobrenome', '$telefone', '$bairro', '$cidade', 
'$documento', '$email', '$observacao')"; 

So check the size you have put of your fields in your database, because if you have indicated for an example that field responsavel table cadastro sera varchar(20), that is, will receive up to 20 characters maximum, and is trying to enter data coming from the form with 21 characters only in this field and the others are all correct, the INSERT will not work, so see the size of your fields in Banco de Dados and do this which I will indicate below in your <input>

If the field has a character limitation, as I will show in the example below

responsavel varchar(20)

Add this to your input as it will cause the field to only receive a maximum of 20 characters, making it impossible to make mistakes when saving data from the form

<input type='text' name='responsavel' maxlength='20'> 

If you still can’t get the expected result that’s yours INSERT in the Banco de Dados do this which I will show you below, after your variable $resultado_cadastro of a echo in the variable of your INSERT, as I will show below

$resultado_cadastro = mysqli_query($conn, $result_cadastro);
echo $result_cadastro;

This will cause the data coming from the form to be displayed on the screen and you can check more clearly where the error is, and if you still can’t see copy the result displayed on the screen and paste it into your database and then it will show what the error is, if you are entering data from caracter em campo inteiro, data of caracter em campo booleano. And then you give one feedback here in my reply ae indicates to me what is happening after all that we will solve.

  • another thing, after q gave an echo $result_register;, appeared on the page grava_cheklist.php in the browser everything I put in the form!!... rs I was moved, but it was not for the database.. rs

  • So do this, copy everything that appeared that I believe is something like INSERT INTO registration ...... and paste in your MYSQL database and there will accuse the error. as I reported in reply

  • Post in your question what appears on the screen when echo $result_registration

  • INSERT INTO registration(id,responsavel, placa, veiculo, cor, combustivel, pneus, kilometragem, paradiante, teto, traseira, paratraseiro, paralamad, paralamae, portam, portac, ptrasd, ptrase, parabrisa, dia, mes, ano, nome_cliente, sobrenome, telefone, bairro, cidade, documento, email, observacao) VALUES ('Customer Service','ilf4923','CORSA','Red','reseva','good','11111111111111111111111111','OK','OK','OK','OK','OK','OK','OK','OK','OK',''OK','13','''November','2017','giva','Braz','0000000','beautiful horizon','Caxias of the south','00000000000','[email protected]','car this ok ok'')

  • @Givabraz It seems that there are quotes, your fields are all varchar ? Post on the question click edit and put your form so I check

Browser other questions tagged

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