Data is not going to Database Mysql/PHP

Asked

Viewed 32 times

2

The page does not show any error, I already put the ini_set('display_errors', 1), but no errors appear, it loads, but it also does not redirect to my final page, it is only a blank page

<?php

if (!isset($_SERVER['HTTP_REFERER'])) { //CSRF
    header('Location: index.php');

}

$servidor = '...';
$usuario = '...';
$senha = '...';
$banco = '...';
$conexao = mysqli_connect($servidor,$usuario,$senha,$banco);

if (mysqli_connect_errno())
  {
  echo "Erro ao conectar: " . mysqli_connect_error();
  }

if (isset($_POST["submit"])) {
    $email = htmlspecialchars($_POST['email']);
    $endereco = htmlspecialchars(($_POST['endereco']));
    $cep = htmlspecialchars(($_POST['cep']));
    $estado = htmlspecialchars(($_POST['estado']));
    $nome = htmlspecialchars(($_POST['nome']));
    $sql = mysqli_query($conexao,"INSERT INTO dados (email, endereco, cep, estado, nome) VALUES ('$email', '$endereco','$cep','$estado','$nome')
");
    header("Location: retorno.php");

}

?>

HTML code:

<form name="Form1" class="needs-validation" novalidate action="api.php" method="post">

            <input type="hidden" name="total" id="total" value="20">
            <div class="row">
              <div class="col-md-6 mb-3">
                <label for="firstName">Primeiro Nome</label>
                <input type="text" class="form-control" name="firstName" id="firstName" placeholder="" value="" required>
                <div class="invalid-feedback">
                  Insira um nome válido!
                </div>
              </div>
            </div>

            <div class="mb-3">
              <label for="email">Email <span class="text-muted">(Opcional)</span></label>
              <input type="email" class="form-control" name="email" id="email" placeholder="[email protected]">
              <div class="invalid-feedback">
                Insira um email válido!
              </div>
            </div>

            <div class="mb-3">
              <label for="address">Endereço</label>
              <input type="text" class="form-control" name="address" id="address" placeholder="Rua Exemplo N1234" required>
              <div class="invalid-feedback">
                Insira um endereço válido!
              </div>
            </div>

            <div class="row"> 
              <div class="col-md-3 mb-3">
                <label for="zip">CEP</label>
                <input type="text" class="form-control" id="zip" id="zip" placeholder="" required>
                <div class="invalid-feedback">
                  Insira um CEP Válido!
                </div>
              </div>
            </div>
            <hr class="mb-4">

            <button class="btn btn-primary btn-lg btn-block" type="submit">Enviar</button>
          </form>

1 answer

2

Some mistakes may be causing this:

<input type="text" class="form-control" name="firstName" id="firstName" placeholder="" value="" required>
<input type="email" class="form-control" name="email" id="email" placeholder="[email protected]">
<input type="text" class="form-control" name="address" id="address" placeholder="Rua Exemplo N1234" required>
<input type="text" class="form-control" id="zip" id="zip" placeholder="" required>

Note here in this input that it has two fields ID and has no field NAME. The PHP will use the field NAME of inputs to create the array with past values, i.e. PHP the offsets of $_POST shall be the same as those placed on NAME in the form entries:

$email = htmlspecialchars($_POST['email']);
$endereco = htmlspecialchars(($_POST['endereco']));
$cep = htmlspecialchars(($_POST['cep']));
$estado = htmlspecialchars(($_POST['estado']));
$nome = htmlspecialchars(($_POST['nome']));

Note that here in the example taken from your post you use offsets [""] that do not exist in NAME in the form inputs, it should be like this

$firstName = htmlspecialchars($_POST['firstName']);
$address = htmlspecialchars(($_POST['address']));
$zip = htmlspecialchars(($_POST['zip']));
$email= htmlspecialchars(($_POST['email']));

Check in the other fields.

And the main

#SUBMIT SEM O ATRIBUTO NAME
<button class="btn btn-primary btn-lg btn-block" type="submit">
#O PHP NUNCA VAI ACHAR O $_POST["submit"]
if (isset($_POST["submit"])) {


#SUBMIT COM O ATRIBUTO NAME
<button class="btn btn-primary btn-lg btn-block" type="submit" name="submit">
#O PHP VAI ACHAR O $_POST["submit"]
if (isset($_POST["submit"])) {

Browser other questions tagged

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