0

有人可以告诉我如何在提交到此页面时自动添加日期吗?DB id 是“table1”,字段 id 是“date”。

我的代码:

<?php

if (isset($_POST["submit"]) && $_POST["submit"] == "Submit")
{
    for ($count = 1; $count <= 9; $count++)
    {
        $fields[$count] = "";
        if (isset($_POST["field" . $count . ""]))
        {
            $fields[$count] = trim($_POST["field" . $count . ""]);
            //echo $fields[$count] . "<br />";
        }
    }

    $con = mysql_connect("local", "user", "pass");
    mysql_select_db("DB", $con);

    $carriername = mysql_real_escape_string($_POST['carriername']);
    $fromzip = mysql_real_escape_string($_POST['fromzip']);
    $tozip = mysql_real_escape_string($_POST['tozip']);
    $typeofequipment = mysql_real_escape_string($_POST['typeofequipment']);
    $weight = mysql_real_escape_string($_POST['weight']);
    $length = mysql_real_escape_string($_POST['length']);
    $paymentamount = mysql_real_escape_string($_POST['paymentamount']);
    $contactperson = mysql_real_escape_string($_POST['contactperson']);
    $loadtype = mysql_real_escape_string($_POST['loadtype']);

    $insert = "INSERT INTO table1 (`carriername` ,`fromzip` ,`tozip` ,`typeofequipment` ,`weight` ,`length` ,`paymentamount` ,`contactperson` ,`loadtype`) VALUES('$carriername' ,'$fromzip' ,'$tozip' ,'$typeofequipment' ,'$weight' ,'$length' ,'$paymentamount' ,'$contactperson' ,'$loadtype');";
    mysql_query($insert) or die(mysql_error());

    $select = "SELECT `carriername` ,`fromzip` ,`tozip` ,`typeofequipment` ,`weight` ,`length` ,`paymentamount` ,`contactperson` ,`loadtype` FROM `table1` ORDER BY `paymentamount` DESC;";
    $result = mysql_query($select) or die(mysql_error());
}
?>
</script>
<style ="text-align: center; margin-left: auto; margin-right: auto;"></style>
</head>
<body>
<div
 style="border: 2px solid rgb(0, 0, 0); margin: 16px 20px 20px; width: 400px; background-color: rgb(236, 233, 216); text-align: center; float: left;">
<form action="" method="post";">
  <div
  style="margin: 8px auto auto; width: 300px; font-family: arial; text-align: left;"><br>
  <table style="font-weight: normal; width: 100%; font-size: 12px;"
 border="1" bordercolor="#929087" cellpadding="6" cellspacing="0">
   <table
 style="font-weight: normal; width: 100%; text-align: right; font-size: 12px;"
 border="1" bordercolor="#929087" cellpadding="6" cellspacing="0">
    <tbody>
    <tr>
    <td style="width: 10%;">Carrier:</td><td>
 <?php  

$con = mysql_connect("local", "user", "pass");
    mysql_select_db("DB", $con);

$query=("SELECT * FROM table2"); 

$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() ); 
echo "<select name=carriername>";   

while($row=mysql_fetch_array($result)){  

echo "<OPTION VALUE=".$row['carriername'].">".$row['carriername']."</OPTION>"; 
} 

echo "</select>";  


?>
              </td>
        </tr>
        <tr>
              <td style="width: 35%;">Pick Zip:</td><td> <input id="fromzip" name="fromzip" maxlength="50"
 style="width: 100%;" type="text">
        </tr>
        <tr>
              <td style="width: 35%;">Drop Zip:</td><td> <input id="tozip" name="tozip" maxlength="50"
 style="width: 100%;" type="text">
        </tr>
        <tr>
              <td style="width: 35%;">Load Type:</td><td> <input id="loadtype" name="loadtype" maxlength="50"
 style="width: 100%;" type="text">
        </tr>
        <tr>
              <td style="width: 35%;">Rate:</td><td> <input id="paymentamount" name="paymentamount" maxlength="50"
 style="width: 100%;" type="text">
        </tr>
          </tbody>
  </table>
  <p style="text-align: center;"><input name="submit" value="Submit"
 class="submit" type="submit"></p>
  </div>
</form>
</div>
<p style="margin-bottom: -20px;">&nbsp;</p>
</body>
4

4 回答 4

2

您可以在查询中使用CURRENT_TIMESTAMP函数,如下所示:

INSERT INTO table1 (carriername, ..., CURRENT_TIMESTAMP)

如果您的字段是类型TIMESTAMP

如果它是 type DATE,你可以使用NOW().

您也可以将表配置为该字段的默认值(CURRENT_TIMESTAMP插入时将其留空:

CREATE TABLE  `database`.`test` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

如果您想在 PHP 中执行此操作,请插入/格式化您的日期: date("m/d/y g:i A");

于 2012-11-06T18:06:00.613 回答
0

我没有看到date您插入到 table1 中的列的引用。如果要插入当前日期,请将插入值设置为 NOW()。

于 2012-11-06T18:03:20.260 回答
0

如果您无论如何都要更新您的表格,您可以执行一些简单的操作,例如使用NOW(). 这方面的一个例子是:

UPDATE table1 SET date=NOW()

如果您走这条路线,请确保您的字段date是类型。DATETIME您还可以TIMESTAMP在编辑条目时使用它来更新字段。要创建它,只需创建一个列并将数据类型设置为 TIMESTAMP:

CREATE TABLE test (
    last_edited TIMESTAMP 0 ON UPDATE CURRENT_TIMESTAMP
);

这将创建一个表 (test) 和一个列 (last_edited) 并将默认值设置为0. 每当以任何方式编辑此记录时,last_edited 列都会更新为时间和日期。

查看此链接以获取更多信息:http ://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

于 2012-11-06T18:07:16.633 回答
0

在phpMyAdmin中将您的日期列设置为DATETIME并将其默认值设置为'CURRENT TIMESTAMP',通过这样做,每当插入一行时,日期就会获取当前时间值并自动与该行一起保存,无需为日期设置值以这种方式在每个插入列上...

于 2012-11-06T18:07:47.833 回答