-1

第一次在这里发问题所以请多多包涵。以下是我正在制作的页面的代码:

<?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 时间戳。

任何朝着正确方向的帮助或刺激都会很棒。

提前致谢

4

3 回答 3

1
  1. 无法从任意格式转换数据。没有魔法。即使有人告诉你有。所以,首先你必须先确定日期格式。
  2. 将日期转换为 unix 时间戳是没有意义的。日期的时间戳会模棱两可且使用起来很麻烦。那又何苦呢?为什么不使用 DATE字段类型来存储日期。听起来很明智,嗯?

您真正需要的是验证输入的日期并将其转换为2013-04-10格式然后插入。

我会建议什么。

  • 使用您可以在谷歌上找到的任何日期选择器来填写表格。
  • explode()使用+checkdate()或验证输入的值strtotime()
  • 将值作为字符串插入 DATE 类型的字段

顺便说一句,有一种更好的方法来构建查询,无需 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 是这个问题的一个函数)

于 2013-04-10T09:21:40.890 回答
0

我认为 strtotime() 可以在这里帮助你:

http://php.net/manual/en/function.strtotime.php

于 2013-04-10T09:14:28.113 回答
-1

使用 date_parse_from_format() 将您发布的日期转换为数组... 参见 php.net

然后使用 mktime() 生成 unix 时间戳 见 php.net

于 2013-04-10T09:17:43.750 回答