What time is it in bd

0

Hello, I'm a beginner programmer, and I'm developing a sales web app, where I need to insert the exact time of the sale into the bd, but I'm not getting it.

  

Code in javascript that generates time in real time:

    <script language="javascript">

        function showTimer() {

            var time=new Date();

            var hour=time.getHours();

            var minute=time.getMinutes();

            var second=time.getSeconds();

            if(hour<10) hour ="0"+hour;

            if(minute<10) minute="0"+minute;

            if(second<10) second="0"+second;

            var st=hour+":"+minute+":"+second;

            document.getElementById("timer").innerHTML=st;
        } function initTimer() {

            setInterval(showTimer,1000);
        }
    </script>
  

Form:

<div id=timer align="center"></div>

<input type="hidden" name="timer">
  

And the connection to the Bank:

<?php

$connect = mysqli_connect("localhost", "root", "", "narguile");

$number = count($_POST["input_quantidade_venda"]);

$soma_venda = $_POST['soma_venda'];

$data = $_POST['data'];

$hora = $_POST['timer'];

$sql = "INSERT INTO vendas(preco_venda, data, hora) VALUES";
$sql .= "('$soma_venda', '$data', '$hora')";
mysqli_query($connect, $sql);

$id_venda = mysqli_insert_id($connect);

if($number > 0) {

    for($i=0; $i<$number; $i++) {

        if(trim($_POST["input_quantidade_venda"][$i] != '')) {

            $sql2 = "INSERT INTO venda_produto(quantidade, id_venda) VALUES('".mysqli_real_escape_string($connect, $_POST["input_quantidade_venda"][$i])."', '$id_venda')";
            mysqli_query($connect, $sql2);
        }  
    }

    echo "Venda cadastrada!";  
}  

else {

    echo "Não rolou";  
}
?>

If someone can help me, I appreciate it.

    
asked by anonymous 22.12.2018 / 12:54

1 answer

1

CURDATE() and CURTIME()

// ...
$sql = "INSERT INTO vendas(preco_venda, data, hora) VALUES";
$sql .= "('$soma_venda', CURDATE(), CURTIME())";
mysqli_query($connect, $sql);
// ...

In the definition of the table vendas a DEFAULT

CREATE TABLE vendas
(
    // ...
    data DATETIME DEFAULT CURRENT_TIMESTAMP
)

ALTER TABLE with DEFAULT

ALTER TABLE vendas MODIFY COLUMN data DATETIME DEFAULT CURRENT_TIMESTAMP

In any of these scenarios, you would have to have "extra" code only to save the current date and time in the database.

    
22.12.2018 / 13:32