Saving PHP and HTML data

Asked

Viewed 72 times

0

Next, I have a form here when filling it I use PHP to validate the data and save in the Mysql database. However, I enter the normal date and when viewing the table of the saved data, the date is the only one that is not stored. Someone could give a HELP. Thanks!

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
    $msgErro = "";

  $nomeMarca = $modelo = $mei = $operadora = $datafabricao = "";

  $nomeMarca          = filtraEntrada($_POST["nomeMarca"]);     
  $modelo             = filtraEntrada($_POST["modelo"]);
  $mei                = filtraEntrada($_POST["mei"]);
  $operadora          = filtraEntrada($_POST["operadora"]);
  $datafabricao       = filtraEntrada($_POST["datafabri"]);

  try
    {    
    // Função definida no arquivo conexaoMysql.php
    $conexao = conectando();

    $sql = "
      INSERT INTO Celular (DataFabricacao, MEI, Modelo, NomeMarca, Operadora)
      VALUES ('$datafabricao', $mei, '$modelo', '$nomeMarca', '$operadora');
    ";

    if (! $conexao->query($sql))
      throw new Exception("Erro na inserção de dados: " . $conexao->error);

    $formProcSucesso = true;
  }
    catch (Exception $e)
    {
        $msgErro = $e->getMessage();
    }
}

<!DOCTYPE html>
<html lang="pt-br">
<head>
  <title>Exercício 02</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Cadastro Celular</h2>
  <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">

    <div class="form-group">
      <label for="nomeMarca">Nome Marca:</label>
      <input type="text" class="form-control" placeholder="Informe a marca do Celular" name="nomeMarca" id="nomeMarca" required>
    </div>

    <div class="form-group">
      <label for="">Modelo:</label>
      <input type="text" class="form-control" placeholder="Informe o modelo do Celular" name="modelo" id="modelo" required>
    </div>

    <div class="form-group">
      <label for="">MEI:</label>
      <input type="number" class="form-control" placeholder="Informe o MEI" name="mei" id="mei" required>
    </div>

    <div class="form-group">
      <label for="operadora">Operadora:</label>
      <select name="operadora" id="operadora" class="form-control" required>
        <option value="tim">TIM</option>
        <option value="vivo">VIVO</option>
        <option value="claro">CLARO</option>
        <option value="claro">ALGAR</option>
        <option value="claro">NEXTEL</option>
        <option value="claro">OI</option>
      </select>
    </div>

    <div class="form-group">
      <label for="datafabri">Data Fabricação:</label>
      <input type="date"  class="form-control" placeholder="Data Fabricação do Celular" name="datafabri" id="udatafabri" required>
    </div>   

    <button type="submit" class="btn btn-default">Enviar</button>
  </form>

function filtraEntrada($dado) 
{
  $dado = trim($dado);
  $dado = stripslashes($dado);
  $dado = htmlspecialchars($dado);

  return $dado;
}

-

if ($arrayCelulares != "")
    {

      foreach ($arrayCelulares as $celular)
      {       
        echo "
        <tr>
          <td>$celular->nomeMarca</td>
          <td>$celular->modelo</td>
          <td>$celular->mei</td>
          <td>$celular->operadora</td>
          <td>$celular->datafabri</td>
          <td><a href= 'excluirCelular.php?MEI=$celular->mei'>Excluir celular</a> </td>     
        </tr>      
      }
    }

-

class Celular 
{
  public $nomeMarca;
  public $modelo;
  public $operadora;
  public $mei;
  public $dataFabri;
}

function getCelular($conexao)
{
  $arrayCelulares = [];

  $SQL = "
  SELECT * FROM Celular
  ";

  $resultado = $conexao->query($SQL);
  if (! $resultado)
    throw new Exception('Ocorreu uma falha ao gerar o relatorio de testes: ' . $conexao->error);

  if ($resultado->num_rows > 0)
  {
    while ($row = $resultado->fetch_assoc())
    {
      $celular = new celular();

      $celular->nomeMarca         = $row["NomeMarca"];
      $celular->modelo            = $row["Modelo"];
      $celular->mei               = $row["MEI"];
      $celular->operadora         = $row["Operadora"];
      $celular->dataFabri         = $row["DataFabricacao"];
      $arrayCelulares[] = $celular;
    }
  }
  return $arrayCelulares;
  • 1

    Strange I managed to insert. The column DataFabricacao is on timestamp, date or datetime?

  • 1

    date, fuck it! Oh, yeah ?

  • 1

    ta in the format YYYY-MM-DD?

  • yes @Weessmith, I was able to fix the following non-inclusion problem, however, now I created a table with php and html to put such data, and the date is the only one not to appear. Do you have any idea why?

  • Give a echo $sql; and see the final statement. If you have brought everything ok, try to run straight in Mysql, so you see the error. And if you don’t bring it correctly, you can see and identify where it can be (post/get, quotes, format, etc )

  • @RBZ did not make sense here the error pointed out, and the error is to connect with the bank..

  • @Gabriel I see no error in the connection if he said this "...when viewing the table of the saved data, the date is the only q is left without being stored..."

  • @RBZ the date ta stored in the database, the problem is, when creating a table with the database data in the table the only field that does not appear is the date. That’s where the problem is.

  • Wow that roll! How are you setting the value of input date ?

  • is without value friend

  • I guess you’re not inserting in the format you set in the database

  • Post where you process this data: <td>$celular->datafabri</td>. Or rather, post that return: $arrayCelulares. This one of yours foreach is strange.

  • @RBZ made. From a look

  • @Andersonhenrique in the bank it is in US format, as I do for it in the table in such format then ?

  • @Gabriel gives a print_r($arrayCelulares). So it makes it easier to know if you’re bringing the value.

  • @RBZ can let I believe you are not understand, bye and Thanks!

  • 3

    @If Gabriel has solved your problem, it would be nice to inform. If we are asking for more information, it is because your question does not contain enough details to identify. The requests in the comments serve exactly for this, request more information. But if you want to quit, no problem, we’ll be here if you want a hand.

  • @RBZ did not solve, however, if I can, I update yes with the resolution.

  • @Gabriel Do the following $data = new Datetime(str_replace("/", "-",$_POST['datafabri'])); $datafabricao = filtraEntrate($data->format('Y-m-d')); If it doesn’t work do what the RMZ said, post the print_r return

  • @Gabriel, then report how the variables are saved and how you’re bringing them

  • 1

    I got what I wanted, in the Insert instead of 'datafabricao' I put NOW() and it worked. Thanks to everyone and sorry !

Show 16 more comments
No answers

Browser other questions tagged

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