2014-10-31 42 views
0

我的HTML表单是这样的:如何插入时间格式在MySQL

<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> 

upload_data.php

include 'connect.php'; 
$total_container = $_POST["total_container"]; 

if (!empty($total_container)){ 
    $fill="INSERT INTO `container`(`total_container`, `time`)VALUES($total_container,'blabla')"; 
    $data=mysql_query($fill); 
    if(isset($data)){ 
     echo "<script>alert ('Great');</script>"; 
    } else { 
     echo "<script>alert('No...');</script>"; 
    } 
} 

我要填写我的数据库表(容器)是这样的:

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 | 
  1. id_block is auto_increment
  2. 对于每个total_container,其乘以20秒的时间字段。

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

+1

参考值就存储为一个int,只是做$ total_container * 20,当你得到它,你可以很容易地计算得到secounds/min/hours? – Naruto 2014-10-31 15:07:56

+1

由于你的用户参数没有[正确转义](http://bobby-tables.com/php),所以这看起来恐怖不安全。您应该**绝不**将'$ _POST'数据直接放入查询中。这造成了一个巨大的[SQL注入漏洞](http://bobby-tables.com/)。另外,'mysql_query'是一个过时的接口,不应该被使用,它将被从PHP中删除。像[PDO这样的现代化替代品并不难学](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/)。像[PHP The Right Way](http://www.phptherightway.com/)这样的指南解释了最佳实践。 – tadman 2014-10-31 15:51:58

回答

3

试试这个

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

SEC_TO_TIME

+0

太棒了,现在它已经在为我工作了。非常感谢你。顺便说一下,TIME(SEC_TO_TIME($ total_container * 20))和SEC_TO_TIME($ total_container * 20)之间有什么不同。他们都给出了相同的值。 – 2014-10-31 15:43:49

+0

我错了。抱歉。这是一个尝试在这里:-) – 2014-10-31 15:47:03

+0

@SinagamanEidelbert你问了很多问题,但自从你第一次注册以来一直没有接受。请访问Meta/Stack ** http://meta.stackexchange.com/a/5235/**上的以下页面,了解如何接受给定的答案,并对所有解决了编码问题的**做相同的处理问题。这就是Stack系统的工作原理。 – 2014-10-31 15:55:27

0
<?php 
$timeRaw = $total_container * 20; 
$time = gmdate("H:i:s", $timeRaw); 
$fill="INSERT INTO `container`(`total_container`, `time`)VALUES($total_container,'$time')"; 
?> 

希望它适合你。

+0

只有'$ timeRaw'少于24小时时,这才能正常工作。 – 2014-10-31 15:14:13

+0

好的。非常感谢您的帮助。我也会试试这个.. – 2014-10-31 15:44:56

0

首先,由于您使用的是旧的MySQL扩展,因此总是会转义您从用户那里收到的数据。否则,你可能会接受SQL注入。

下面是构建正确的SQL查询的代码(为了更好地理解,我省略了对empty($total_container)的检查)。

//Get data from user and escape 
$total_container=mysql_real_escape_string($_POST['total_container']); 
//Calculate time in seconds * 20 
$time_in_seconds=$total_container*20; 
//format as hh:mm:ss. 
//Using gmdate('H:i:s',$time_in_seconds) for this is ok, 
//but only if your $time_in_seconds is never going to exceed 24 hours 
$time=sprintf('%02d:%02d:%02d', 
     ($time_in_seconds/3600), ($time_in_seconds/60%60), $time_in_seconds%60); 
//build SQL query 
$fill="INSERT INTO `container` (`total_container`, `time`) 
     VALUES($total_container,'$time')"; 
0

添加

$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)。

1

更改您这样的代码:

<?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>"; 
         } 
} 

?>