如何通过PHP与mySQL在INSERT中添加日期和时间戳? [英] How do add date and time stamp to an INSERT via PHP with mySQL?

查看:168
本文介绍了如何通过PHP与mySQL在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)或die(can not connect);
mysql_select_db($ db_name)或者死(不能选择DB);

$ order =INSERT INTO cartons_added(
type ,
part_no,
add_type,
add_qty,
add_ref,
add_by,
add_notes
)VALUES(
'$ _POST [类型]',
'$ _POST [part_no]',
'$ _POST [add_type]',
'$ _POST [add_qty]',
'$ _POST [add_re f]',
'$ _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);
}
?>


解决方案

您的add_time列设置在数据库?是否为DATETIME格式?



在这种情况下,您可能只需修改您的查询:

  $ order =INSERT INTO cartons_added(type,part_no,add_type,add_qty,
add_ref,add_by,add_notes,add_time)

VALUES
$ _POST [type]',
'$ _POST [part_no]',
'$ _POST [add_type]',
'$ _POST [add_qty]',
'$ _POST [add_ref]',
'$ _POST [add_by]',
'$ _POST [add_notes]',
NOW());

尽管您应该知道执行这样的查询是危险的,因为您信任用户只输入好的事情!谷歌SQL注入来了解更多关于它,mysql_real_escape_string()也是。


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

解决方案

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:

$order = "INSERT INTO cartons_added (type, part_no, add_type, add_qty, 
  add_ref, add_by, add_notes, add_time)

  VALUES
  ('$_POST[type]', 
  '$_POST[part_no]', 
  '$_POST[add_type]', 
  '$_POST[add_qty]', 
  '$_POST[add_ref]', 
  '$_POST[add_by]', 
  '$_POST[add_notes]',
   NOW())";

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.

这篇关于如何通过PHP与mySQL在INSERT中添加日期和时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆