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;
Strange I managed to insert. The column
DataFabricacao
is on timestamp, date or datetime?– Andrei Coelho
date, fuck it! Oh, yeah ?
– Gabriel
ta in the format YYYY-MM-DD?
– Wees Smith
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?
– Gabriel
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
@RBZ did not make sense here the error pointed out, and the error is to connect with the bank..
– Gabriel
@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
@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.
– Gabriel
Wow that roll! How are you setting the
value
ofinput
date ?– rbz
is without value friend
– Gabriel
I guess you’re not inserting in the format you set in the database
– Anderson Henrique
Post where you process this data:
<td>$celular->datafabri</td>
. Or rather, post that return:$arrayCelulares
. This one of yoursforeach
is strange.– rbz
@RBZ made. From a look
– Gabriel
@Andersonhenrique in the bank it is in US format, as I do for it in the table in such format then ?
– Gabriel
@Gabriel gives a
print_r($arrayCelulares)
. So it makes it easier to know if you’re bringing the value.– rbz
@RBZ can let I believe you are not understand, bye and Thanks!
– Gabriel
@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
@RBZ did not solve, however, if I can, I update yes with the resolution.
– Gabriel
@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
– Anderson Henrique
@Gabriel, then report how the variables are saved and how you’re bringing them
– Wees Smith
I got what I wanted, in the Insert instead of 'datafabricao' I put NOW() and it worked. Thanks to everyone and sorry !
– Gabriel