我有这样的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>
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 |
所以,我应该在查询中做什么?,我想自动填充时间字段,乘以20秒。
答案 0 :(得分:3)
试试这个
INSERT INTO `container`(`total_container`, `time`)
VALUES($total_container, SEC_TO_TIME($total_container*20) )
请参阅SEC_TO_TIME
的参考资料答案 1 :(得分: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>";
}
}
?>
答案 2 :(得分:0)
<?php
$timeRaw = $total_container * 20;
$time = gmdate("H:i:s", $timeRaw);
$fill="INSERT INTO `container`(`total_container`, `time`)VALUES($total_container,'$time')";
?>
希望它适合你。
答案 3 :(得分: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')";
答案 4 :(得分: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)。