【问题标题】:Date sorting from input in php from sql?从sql中的php中输入日期排序?
【发布时间】:2014-07-30 04:13:37
【问题描述】:

我正在使用 html 和 php 的组合为我自己的小商店做销售记录。
我想有时间选择输入(选择时类似于 2014 年 3 月、2014 年 4 月)
这是我的 index.php

<?php
$con=mysqli_connect("192.168.1.248","a","a","services");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM pcsales");
echo "<h3><marquee><b>====PC Sales====</b></marquee></h3>";
echo "<button type='button' name='add' onClick='add()'>Add</button>";
echo "<script type='text/javascript'>
function add()
{
window.location='./edit'; 
}
</script>";
echo "<button type='button' name='edit' onClick='edit()'>Edit</button>";
echo "<script type='text/javascript'>
function edit()
{
window.location='./edit/edit.html'; 
}
</script>";
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Type</th>
<th>If Others</th>
<th>Brand</th>
<th>Description</th>
<th>Date Sold</th>
<th>Serial No.</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td><p style='font-size:12px'>" . $row['ID'] . "</p></td>";
  echo "<td>" . $row['Type'] . "</td>";
  echo "<td>" . $row['Types'] . "</td>";
  echo "<td>" . $row['Brand'] . "</td>";
  echo "<td><p style='font-size:12px'>" . $row['Description'] . "</p></td>";
  echo "<td><p style='font-size:12px'>" . $row['Selldate'] . "</p></td>";
  echo "<td><p style='font-size:12px'>" . $row['Serial'] . "</p></td>";
  echo "</tr>";
}

echo "</table>";
echo "<h3><marquee direction=right><b>====PC Sales====</b></marquee></h3>";
mysqli_close($con);
?>

我在 mysql 中使用 TIMESTAMP。 这是输出

ID 类型 如果是其他 品牌描述 销售日期 序列号 1 笔记本 Acer E1 添加 2GB DDR3 1600 RAM 2014-06-25 11:57:58 123456789 2 其他 AIO Asus N/A 2014-07-25 12:52:12 987654321 3 桌面 Trendsonic 完整规格已列出。 2014-07-30 09:55:10 不适用

当我在文本框上放置一个范围时(如果可以选择更方便) 示例:

2014 年 7 月

那么输出应该是

ID 类型 如果是其他 品牌描述 销售日期 序列号 2 其他 AIO Asus N/A 2014-07-25 12:52:12 987654321 3 桌面 Trendsonic 完整规格已列出。 2014-07-30 09:55:10 不适用

如果只是

2014
被插入,那么它将输出 2014 年的所有内容 有可能这样做吗?

注意:我不关心漏洞利用,因为它在内部使用。

【问题讨论】:

  • "注意:我不关心漏洞利用,因为它在内部使用。"因为没有一家公司有过心怀不满的员工。
  • 好吧,我没有真正的雇员,因为所有的“工人”都是我的家人 :D

标签: php mysql sql date


【解决方案1】:

最好使用 datepicker 并从那里获取变量中的日期并进行一些查询。 选择 * 来自表名 WHERE MONTH(dateColumnname) = $month AND YEAR(dateColumnname) = $year; 示例:选择 * FROM testerpractice WHERE MONTH(datess) = '03' AND YEAR(datess) = '2017'; 祝你好运...

【讨论】:

    【解决方案2】:

    最好使用下拉列表(标签)来表示日、月和年(默认为 01 表示日,Jan 表示月份,当前年份表示年份),并将它们的值连接起来以获取搜索字符串。您将拥有权宜之计,并且能够轻松构建查询。

    【讨论】:

    • 你有例子吗?因为我是 php 和 mysql 的新手 D:
    • 喜欢 POST on url 清楚地整理出 index.php?month=jan&year=2014 类似的东西?
    • 不,没有 PHP 示例。但是在 POST 之后,您应该能够从下拉列表中获取值。 index.php?month=01&amp;year=2014 是 GET,而不是 POST。
    • 我的错 :D 我是新手~
    • 感谢您的回答:D
    【解决方案3】:

    -问题已解决- 我的解决方案(不是那个“专业”) 我做了一个 sort.html 和一个 sort.php

    sort.html 包含

    Year:<input type="text" value="" name="year"/>
    Month:<input type="text" value="" name="month"/>
    

    虽然 sort.php 包含

    $year = mysqli_real_escape_string($con, $_POST['year']);
    $month = mysqli_real_escape_string($con, $_POST['month']);
    $result = mysqli_query($con,"SELECT * FROM pcsales WHERE Selldate >= '$year-$month-01 00:00:00' AND Selldate <= '$year-$month-31 23:59:59'");
    

    这可能是其他可能遇到与我相同问题的人的参考~

    【讨论】:

      猜你喜欢
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 2015-07-26
      • 2023-03-21
      相关资源
      最近更新 更多