使用PHP将日期记录为时间戳&;SQL

使用PHP将日期记录为时间戳&;SQL,php,timestamp,unix-timestamp,Php,Timestamp,Unix Timestamp,第一次在这里发帖,请耐心等待。下面是我正在制作的页面代码: <?php require_once('Connections/reps.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VER

第一次在这里发帖,请耐心等待。下面是我正在制作的页面代码:

<?php require_once('Connections/reps.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

  mysql_select_db($database_reps, $reps);
  $query_bonusset = "SELECT bonus FROM staff WHERE id = '".$_POST['staffMember']."'";
  $bonusset = mysql_query($query_bonusset, $reps) or die(mysql_error());
  $row_bonusset = mysql_fetch_assoc($bonusset);
  $totalRows_bonusset = mysql_num_rows($bonusset);


  mysql_select_db($database_reps, $reps);
  $query_listprice = "SELECT Price FROM products WHERE id = '".$_POST['product']."'";
  $listprice = mysql_query($query_listprice, $reps) or die(mysql_error());
  $row_listprice = mysql_fetch_assoc($listprice);
  $totalRows_listprice = mysql_num_rows($listprice);

  $insertSQL = sprintf("INSERT INTO orders (ID, customerName, staffMember, SalesPrice, Price, AgeOfPayment, product, Quantity, orderDate, bonus) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['ID'], "int"),
                       GetSQLValueString($_POST['customerName'], "text"),
                       GetSQLValueString($_POST['staffMember'], "text"),
                       GetSQLValueString($_POST['SalesPrice'], "int"),
                       GetSQLValueString($row_listprice['Price'], "int"),
                       GetSQLValueString($_POST['AgeOfPayment'], "int"),
                       GetSQLValueString($_POST['product'], "text"),
                       GetSQLValueString($_POST['Quantity'], "int"),
                       GetSQLValueString($_POST['orderDate'], "int"),
                       GetSQLValueString($row_bonusset['bonus'], "int"));

  mysql_select_db($database_reps, $reps);
  $Result1 = mysql_query($insertSQL, $reps) or die(mysql_error());

  $insertGoTo = "vieworders.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_reps, $reps);
$query_customers = "SELECT * FROM customers WHERE Status = 'Active'";
$customers = mysql_query($query_customers, $reps) or die(mysql_error());
$row_customers = mysql_fetch_assoc($customers);
$totalRows_customers = mysql_num_rows($customers);

mysql_select_db($database_reps, $reps);
$query_products = "SELECT * FROM products";
$products = mysql_query($query_products, $reps) or die(mysql_error());
$row_products = mysql_fetch_assoc($products);
$totalRows_products = mysql_num_rows($products);

mysql_select_db($database_reps, $reps);
$query_staff = "SELECT * FROM staff";
$staff = mysql_query($query_staff, $reps) or die(mysql_error());
$row_staff = mysql_fetch_assoc($staff);
$totalRows_staff = mysql_num_rows($staff);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Site Title</title>
<?php


require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");

?>

</head>

<body>

<div id='wrapper'>
<div id='top'><div id='logo'></div></div>
<div id='content'>
<h1>Site Name</h1>
<h2>Change This</h2>
<div id='left-nav'>

<?php include("left-nav.php"); ?>

</div>
<div id='main'>


<div id ="prodtable">
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Customer Name:</td>
      <td><select name="customerName">
        <?php 
do {  
?>
        <option value="<?php echo $row_customers['ID']?>" ><?php echo $row_customers['Name']?></option>
        <?php
} while ($row_customers = mysql_fetch_assoc($customers));
?>
      </select></td>
    </tr>
    <tr> </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Sold by: </td>
      <td><select name="staffMember">
        <?php 
do {  
?>
        <option value="<?php echo $row_staff['ID']?>" ><?php echo $row_staff['StaffName']?></option>
        <?php
} while ($row_staff = mysql_fetch_assoc($staff));
?>
      </select></td>
    </tr>
    <tr> </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Sales Price (€): </td>
      <td><input type="text" name="SalesPrice" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Product:</td>
      <td><select name="product">
        <?php 
do {  
?>
        <option value="<?php echo $row_products['ID']?>" ><?php echo $row_products['ProductName']?></option>
        <?php
} while ($row_products = mysql_fetch_assoc($products));
?>
      </select></td>
    </tr>
    <tr> </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Quantity:</td>
      <td><input type="text" name="Quantity" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Order Date:</td>
      <td><input type="text" name="orderDate" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record" /></td>
    </tr>
  </table>
  <input type="hidden" name="ID" value="" />
  <!-- Removed list price as it was declared above -->
  <input type="hidden" name="AgeOfPayment" value="" />
  <!-- Removed bonus as it was decalred above -->
  <input type="hidden" name="MM_insert" value="form1" />
</form>
</div>
<p>&nbsp;</p>
</div>
<div id='bottom'></div>
</div>


</body>
</html><?php
mysql_free_result($customers);

mysql_free_result($products);

mysql_free_result($staff);
?>

网站名称
站点名称
改变这个
销售人:
售价(欧元):
产品:
数量:
订购日期:

我希望做的是,一旦我提交表单,它将采用输入的日期,并将其转换为SQL数据库上的Unix时间戳

在正确的方向上给予任何帮助或刺激都是非常好的

提前感谢

我认为strotime()可以帮助您:


使用日期解析格式()将发布的日期转换为数组。。。

然后使用mktime()生成unix时间戳

  • 没有办法从任意格式转换数据。没有魔法。即使有人告诉你有。所以,首先你必须先确定日期格式
  • 将日期转换为unix时间戳是没有意义的。日期的时间戳是不明确的,使用起来很费力。那何必费心呢?为什么不使用
    日期
    字段类型来存储日期呢。听起来很明智,是吗
  • 您真正需要的是验证输入的日期,并将其转换为
    2013-04-10
    格式,然后插入

    我的建议

    • 使用谷歌上你能找到的任何日期选择器来填写表格
    • 使用
      explode()
      +
      checkdate()
      strotime()
      验证输入的值
    • 将值作为字符串插入日期类型的字段中
    顺便说一下,有一种更好的方法可以不用GetSQLValueString函数来构建查询

    首先,您可以使用sprintf本身的强大功能:

    paraQuery("INSERT INTO orders VALUES (%d,%s,%s,%d,%d,%d,%s,%d,%d,%d",
               $_POST['ID'], 
               $_POST['customerName'], 
               $_POST['staffMember'],
               $_POST['SalesPrice'], 
               $row_listprice['Price'], 
               $_POST['AgeOfPayment'], 
               $_POST['product'], 
               $_POST['Quantity'],
               $_POST['orderDate'], 
               $row_bonusset['bonus']
               );
    
    (paraQuery是来自的函数)

    接下来,对于长插入,最好使用辅助函数,而不是手动写入:

    $fields = explode(",","ID,customerName,staffMember,SalesPrice,Price,AgeOfPayment,product,Quantity,orderDate,bonus");
    $query  = "INSERT INTO orders SET ".dbSet($fields);
    

    (dbSet是from的一个函数)

    为了让您知道,空字符串也不等于null,请只发布必要的代码来说明您的问题。在这种情况下,似乎没有任何代码可以满足要求。您应该使用PDO和prepared语句、mysql_query()和friends is so 2007;)谢谢,工作很好:)