How to insert dates in a table from a database using PHP? [duplicate]

0

I am creating a form where I need to save the dates that are placed inside the Inputs in a MySQL database table, but as I am now apprehending PHP, I am having difficulties, because with the code I always have to press to send the information the date field is zeroed 0000-00-00 after registration.

I need a PHP code to link the date, name and e-mail information I can save normally.

Example of what I'm using:

<html>
<head>
<Title>Exemplo</Title>
</head>
<body>
    <form action="Conecta.php" methodh="POST">
        Nome<input type="text" name="nome"/>
        E-mail<input type="email" name="email"/>
        Data de nascimento<input type="text" name="data_nasc"/>
        <input type="submit" name="Enviar"/>
    </form>
</body>
</html>

This is the PHP code I'm using, but it only saves name and email ....

<?php
$servername = "localhost";
$database = "Cadastro";
$username = "Root";
$password = "xxxxx";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";

// RECEBENDO OS DADOS PREENCHIDOS DO FORMULÁRIO !

$nome = $_POST ["nome"]; 
$email = $_POST ["email"]; 
$data_nasc = $_POST ["data_nasc"]; 
$data = date("Y-m-d",strtotime(str_replace('/','-',$data)));  

$newslleter = "INSERT INTO newslleter (nome,email,data_nasc) VALUES ('$nome','$email','$data_nasc')";

if (mysqli_query($conn, $newslleter)) {
      echo "New record created successfully";
} else {
      echo "Error: " . $usuarios . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>
    
asked by anonymous 25.06.2018 / 23:53

1 answer

0

Simple lack of friend attention, but you just right here $data = date("Y-m-d",strtotime(str_replace('/','-',$data_nasc))); and when you insert you enter the variable $ data, in place of $ data_nasc in the query. $newslleter = "INSERT INTO newslleter (nome,email,data_nasc) VALUES ('$nome','$email','$data')";

    
26.06.2018 / 01:07