如何使用 mySQL 通过 PHP 将日期和时间戳添加到 INSERT 中?
我有一个使用 mySQL 的插入脚本。我需要将创建记录的日期和时间放在“add_time”列中。
任何人都可以告诉我如何修改现有脚本来执行此操作吗?我需要单独的 PHP 脚本吗?
我希望日期出现在标准格式: 09/25/11 6:54 AM
<?
$host="XXXXXXXXXXX";
$username="XXXXXXX";
$password="XXXXXXX";
$db_name="naturan8_hero";
$tbl_name="cartons_added";
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$order = "INSERT INTO cartons_added (
type,
part_no,
add_type,
add_qty,
add_ref,
add_by,
add_notes
) VALUES (
'$_POST[type]',
'$_POST[part_no]',
'$_POST[add_type]',
'$_POST[add_qty]',
'$_POST[add_ref]',
'$_POST[add_by]',
'$_POST[add_notes]'
)";
$result = mysql_query($order);
if ($result) {
$part_no = $_REQUEST['part_no'] ;
$add_qty = $_REQUEST['add_qty'];
header("location: inv_fc_add_success.php?part_no=" . urlencode($part_no) . "&add_qty=" . urlencode($add_qty));
}
else {
header("location: inv_fc_add_fail.php");
}
?>
I have an insert script with mySQL. I need to place the date and time when the record is created in the 'add_time" column.
Can anyone show me how to modify my existing script to do this? Do I need a separate PHP script?
I would like the date to appear in standard formt: 09/25/11 6:54 AM
<?
$host="XXXXXXXXXXX";
$username="XXXXXXX";
$password="XXXXXXX";
$db_name="naturan8_hero";
$tbl_name="cartons_added";
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$order = "INSERT INTO cartons_added (
type,
part_no,
add_type,
add_qty,
add_ref,
add_by,
add_notes
) VALUES (
'$_POST[type]',
'$_POST[part_no]',
'$_POST[add_type]',
'$_POST[add_qty]',
'$_POST[add_ref]',
'$_POST[add_by]',
'$_POST[add_notes]'
)";
$result = mysql_query($order);
if ($result) {
$part_no = $_REQUEST['part_no'] ;
$add_qty = $_REQUEST['add_qty'];
header("location: inv_fc_add_success.php?part_no=" . urlencode($part_no) . "&add_qty=" . urlencode($add_qty));
}
else {
header("location: inv_fc_add_fail.php");
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您根本不需要从 PHP 向该列插入数据:
因此,将 add_time 列定义更改为
,然后当您插入新行时,它将自动填充该列,
请参阅: MySql 时间戳初始化手册
You do not need to insert data to that column from PHP at all:
So change the add_time column definition to
and then it will automatically populate the column for you when you insert a new row
See: MySql Manual on Timestamp Initializations
您在数据库中设置了“add_time”列吗?是DATETIME格式吗?
在这种情况下,您可以像这样修改您的查询:
尽管您应该意识到执行这样的查询是危险的,因为您相信用户只输入好的东西!谷歌“SQL注入”来了解更多相关信息,还有mysql_real_escape_string()。
You got the "add_time" column set up in your database? Is it of DATETIME format?
In that case you may just modify your query like this:
Though you should be aware that executing queries like this is dangerous as you trust the user to input only nice things! Google "SQL Injection" to find out more about it, mysql_real_escape_string(), too.
将“时间”列添加到“cartons_added”表中。
并以此作为订单
Add 'time' column to your 'cartons_added' table.
And use this as order
我喜欢 Sam 的答案,因为它是最好的 - 作为替代方案,但是,您可以根据您的要求使用
date("m/d/YH:i A");
- 即。它将输出 MM/DD/YYYY HH:MM AM/PM。虽然如果您使用 Sam 的方法,它将需要更少的代码,但您需要将其转换以获得您想要的格式。I like Sam's answer, as it is best - as an alternative, however, you could use
date("m/d/Y H:i A");
as per your requirements - ie. it will output MM/DD/YYYY HH:MM AM/PM. Though if you use Sam's method, it will require less code, but you'd need to convert it to get you desired format.