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

发布于 2024-12-06 07:00:01 字数 1096 浏览 2 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

内心旳酸楚 2024-12-13 07:00:01

您根本不需要从 PHP 向该列插入数据:

TIMESTAMP 和 DATETIME 列可以自动初始化并更新为当前日期和时间(即当前时间戳)。

因此,将 add_time 列定义更改为

add_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP

,然后当您插入新行时,它将自动填充该列,

请参阅: MySql 时间戳初始化手册

You do not need to insert data to that column from PHP at all:

TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp).

So change the add_time column definition to

add_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP

and then it will automatically populate the column for you when you insert a new row

See: MySql Manual on Timestamp Initializations

本王不退位尔等都是臣 2024-12-13 07:00:01

您在数据库中设置了“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()。

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.

情话墙 2024-12-13 07:00:01

将“时间”列添加到“cartons_added”表中。

并以此作为订单

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

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

Add 'time' column to your 'cartons_added' table.

And use this as order

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

  VALUES
  ('$_POST[type]', 
  '$_POST[part_no]', 
  '$_POST[add_type]', 
  '$_POST[add_qty]', 
  '$_POST[add_ref]', 
  '$_POST[add_by]', 
  '$_POST[add_notes]',
  '".time().")";
很酷又爱笑 2024-12-13 07:00:01

我喜欢 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文