Php 如何在MySQL中插入时间格式

Php 如何在MySQL中插入时间格式,php,mysql,Php,Mysql,我的表单html如下所示: <html> <head> <body> <form action='upload_data.php' method="POST" enctype="multipart/form-data"> <input type="text" name="total_container"> <button id="submit">

我的表单html如下所示:

<html>
    <head>
    <body>
        <form action='upload_data.php' method="POST" enctype="multipart/form-data">
            <input type="text" name="total_container">
            <button id="submit">Add</button>    
        </form>
    </body>
    </head>
</html>
id_block  | total_container | time     |
---------------------------------------
1         | 1               | 00:00:20 |
2         | 2               | 00:00:40 |
3         | 3               | 00:01:00 |
4         | 6               | 00:02:00 |
<?php
include 'connect.php';
$total_container = $_POST["total_container"];
$time = date("H:i:s", $total_container);
if (!empty($total_container)){
$fill="INSERT INTO `container`(`total_container`, `time`)VALUES($total_container,'$time ')";
$data=mysql_query($fill);
if(isset($data))
            {
                echo "<script>alert ('Great');</script>";
            }
            else
            {
                echo "<script>alert('No...');</script>";
                        }
}

?>
  • id_块是自动递增的
  • 对于每个total_容器,其时间字段乘以20秒

  • 那么,在我的查询中应该怎么做呢?我想自动填充时间字段,乘以20秒。

    首先,总是转义从用户接收的数据,因为您使用的是旧的MySQL扩展。否则,您将接受SQL注入

    <?php
    $timeRaw = $total_container * 20;
    $time = gmdate("H:i:s", $timeRaw);
    $fill="INSERT INTO `container`(`total_container`, `time`)VALUES($total_container,'$time')";
    ?>
    
    下面是一段构建正确SQL查询的代码(为了更好地理解,我省略了检查
    empty($total\u container)


    首先,始终转义从用户接收的数据,因为您使用的是旧的MySQL扩展。否则,您将接受SQL注入

    下面是一段构建正确SQL查询的代码(为了更好地理解,我省略了检查
    empty($total\u container)

    添加

    并将查询编辑为

    $fill="INSERT INTO `container`(`total_container`, `time`) 
           VALUES ($total_container,'$time')"; 
    
    您还可以使用
    gmdate(“H:i:s”,$time)
    而不是
    $time='00:00:'。$time
    您使用的方法取决于表结构。如果时间是一个文本字段,则可以同时使用这两个字段。但是如果时间是一个时间字段,最好使用第二个示例(gmdate)。

    添加一个

    $time = $total_container*20; $time = '00:00:'.$time;
    
    并将查询编辑为

    $fill="INSERT INTO `container`(`total_container`, `time`) 
           VALUES ($total_container,'$time')"; 
    
    您还可以使用
    gmdate(“H:i:s”,$time)
    而不是
    $time='00:00:'。$time
    
    您使用的方法取决于表结构。如果时间是一个文本字段,则可以同时使用这两个字段。但如果时间是一个时间字段,最好使用第二个示例(gmdate)。

    如下更改代码:

    <html>
        <head>
        <body>
            <form action='upload_data.php' method="POST" enctype="multipart/form-data">
                <input type="text" name="total_container">
                <button id="submit">Add</button>    
            </form>
        </body>
        </head>
    </html>
    
    id_block  | total_container | time     |
    ---------------------------------------
    1         | 1               | 00:00:20 |
    2         | 2               | 00:00:40 |
    3         | 3               | 00:01:00 |
    4         | 6               | 00:02:00 |
    
    <?php
    include 'connect.php';
    $total_container = $_POST["total_container"];
    $time = date("H:i:s", $total_container);
    if (!empty($total_container)){
    $fill="INSERT INTO `container`(`total_container`, `time`)VALUES($total_container,'$time ')";
    $data=mysql_query($fill);
    if(isset($data))
                {
                    echo "<script>alert ('Great');</script>";
                }
                else
                {
                    echo "<script>alert('No...');</script>";
                            }
    }
    
    ?>
    

    如下更改代码:

    <html>
        <head>
        <body>
            <form action='upload_data.php' method="POST" enctype="multipart/form-data">
                <input type="text" name="total_container">
                <button id="submit">Add</button>    
            </form>
        </body>
        </head>
    </html>
    
    id_block  | total_container | time     |
    ---------------------------------------
    1         | 1               | 00:00:20 |
    2         | 2               | 00:00:40 |
    3         | 3               | 00:01:00 |
    4         | 6               | 00:02:00 |
    
    <?php
    include 'connect.php';
    $total_container = $_POST["total_container"];
    $time = date("H:i:s", $total_container);
    if (!empty($total_container)){
    $fill="INSERT INTO `container`(`total_container`, `time`)VALUES($total_container,'$time ')";
    $data=mysql_query($fill);
    if(isset($data))
                {
                    echo "<script>alert ('Great');</script>";
                }
                else
                {
                    echo "<script>alert('No...');</script>";
                            }
    }
    
    ?>
    
    试试这个

    INSERT INTO `container`(`total_container`, `time`) 
    VALUES($total_container, SEC_TO_TIME($total_container*20) ) 
    
    请参阅参考资料,尝试此操作

    INSERT INTO `container`(`total_container`, `time`) 
    VALUES($total_container, SEC_TO_TIME($total_container*20) ) 
    

    请参阅参考资料

    只需将该值存储为int并执行$total_container*20,当您获取该值时,您就可以轻松计算出秒/分钟/小时?这看起来非常不安全,因为您的用户参数不是。您不应该将
    $\u POST
    数据直接放入查询中。这造成了巨大的灾难。另外,
    mysql\u query
    是一个过时的接口,不应该使用,它正在从PHP中删除。一个现代的替代品。像这样的指南解释了最佳实践。只需将值存储为int并执行$total_container*20,当您得到它时,您就可以轻松地计算出秒/分钟/小时?这看起来非常不安全,因为您的用户参数不是。您不应该将
    $\u POST
    数据直接放入查询中。这造成了巨大的灾难。另外,
    mysql\u query
    是一个过时的接口,不应该使用,它正在从PHP中删除。一个现代的替代品。类似这样的指南解释了最佳实践。这仅在
    $timeRaw
    小于24小时时正确。这仅在
    $timeRaw
    小于24小时时正确。太好了,现在它对我有效。非常感谢你。顺便问一下,时间(秒到秒的时间($total_container*20))和秒到秒的时间($total_container*20)有什么不同。这两个都给出了相同的值。@SinagamanEidelbert自从你第一次注册以来,你已经问了很多问题,但没有接受任何问题。访问Meta/Stack上的下一页,了解如何接受给出的答案,并对解决编码问题的所有答案执行相同的操作。这就是堆栈系统的工作原理。太好了,现在它对我起作用了。非常感谢你。顺便问一下,时间(秒到秒的时间($total_container*20))和秒到秒的时间($total_container*20)有什么不同。这两个都给出了相同的值。@SinagamanEidelbert自从你第一次注册以来,你已经问了很多问题,但没有接受任何问题。访问Meta/Stack上的下一页,了解如何接受给出的答案,并对解决编码问题的所有答案执行相同的操作。这就是堆栈系统的工作方式。