Register exact time in the comic

Asked

Viewed 30 times

0

Good morning, I’m a beginner programmer, and I’m developing a sales web app, where I need to insert in the comic the exact time of sale, but I’m not getting.

Javascript code that generates real-time 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 anyone can help me, I’d appreciate it.

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 one 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 hypotheses it would be useless to have "extra" code just to record the current date and time in the database.

  • haha, vlw, John, I’m starting, and I still don’t know anything...

Browser other questions tagged

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