如何使用 mySQL 通过 PHP 向 INSERT 添加日期和时间戳?

How do add date and time stamp to an INSERT via PHP with mySQL?(如何使用 mySQL 通过 PHP 向 INSERT 添加日期和时间戳?)
本文介绍了如何使用 mySQL 通过 PHP 向 INSERT 添加日期和时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 mySQL 的插入脚本.我需要将创建记录的日期和时间放在add_time"列中.

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.

谁能告诉我如何修改我现有的脚本来做到这一点?我需要单独的 PHP 脚本吗?

Can anyone show me how to modify my existing script to do this? Do I need a separate PHP script?

我希望日期以标准格式显示:09/25/11 6:54 AM

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

推荐答案

您在数据库中设置了add_time"列吗?是DATETIME格式吗?

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())";

尽管您应该知道执行这样的查询是危险的,因为您相信用户只会输入好东西!谷歌SQL 注入"以了解更多信息,mysql_real_escape_string() 也是如此.

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.

这篇关于如何使用 mySQL 通过 PHP 向 INSERT 添加日期和时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)