php - php 中的 sql 查询

标签 php html mysql sql

我经营一个小型服务台,我需要每月向我的直线经理提供一份工单报告。

我希望升级我们生成此报告的方式,因为目前,我使用 Navicat 每月运行我手动编写的以下查询:

 SELECT
 updates.incidentid AS `Incident ID`,
 updates.bodytext AS `Call Status`,
 updates.duration AS `Total Minutes`,
 software.`name` AS 'Support Type',
 incidents.title AS Description,
 contacts.forenames AS `First Name`,
 contacts.surname AS `Last Name`,
 FROM_UNIXTIME(incidents.opened, '%d.%m.%Y') AS `Date Logged`,
 sites.`name`,
 users.realname
 FROM
 updates
 INNER JOIN incidents ON updates.incidentid = incidents.id
 JOIN contacts ON incidents.contact = contacts.id
 INNER JOIN sites ON sites.id = contacts.siteid
 INNER JOIN users ON incidents.`owner` = users.id
 INNER JOIN software ON incidents.softwareid = software.id
 WHERE
 updates.bodytext = 'Incident Closed'
    AND FROM_UNIXTIME(incidents.opened, '%m') = '07'
            AND FROM_UNIXTIME(incidents.opened, '%Y') = '2016'
 ORDER BY contacts.siteid ASC

我想将这一切放在一个非常简单的网页中,允许用户选择月份和年份,然后为他们提供 HTML 格式的下载。

我最好将其放在 PHP 中还是可以用 HTML 创建它?请友善,我是一个新手,我的思维过程有点偏离。

非常感谢您的时间和耐心!

最佳答案

我将向您展示基本的:首先,一个 HTML 文件,用户在其中输入月份和年份作为数字(注意对“report.php”的调用):

<html>
  <body>
    <form method="post" action="report.php">
      Enter month (1..12) <input type="text" name="month"/>
      <br/>
      Enter year (four digits) <input type="text" name="year"/>
      <br/>
      <input type="submit" value="Display report"/>
    </form>
  </body>
</html>

现在 PHP 文件“report.php”(注意如何在开头捕获月份和年份并将其存储在变量 $month$year 中):

<?php
$month = $_POST["month"]; // PARAMETERS FROM
$year  = $_POST["year"];  // THE HTML FILE.
$cnx = mysqli_connect( "localhost","user","password","databasename");
$data = mysqli_query( $cnx,"YOUR BIG SELECT HERE" ) or die( mysqli_error( $cnx ) );
echo "<table>" .
     "  <tr>" .
     "    <td>Incident id</td>" .
     "    <td>Call status</td>" .
     "    <td>Description</td>" .
     "    <td>Date logged</td>" .
     "    <td>Site name</td>" .
     "    <td>User</td>" .
     "  </tr>";
while ( $row = mysqli_fetch_array( $data ) ) // LOOP TO DISPLAY DATA.
  echo "<tr>" .
       "  <td>{$row["IncidentID"]}</td>" .
       "  <td>{$row["CallStatus"]}</td>" .
       "  <td>{$row["Description"]}</td>" .
       "  <td>{$row["DateLogged"]}</td>" .
       "  <td>{$row["name"]}</td>" .
       "  <td>{$row["realname"]}</td>" .
       "</tr>";
echo "</table>"; // TABLE END.
?>

您必须将文本“YOUR BIG SELECT HERE”替换为您的大查询。这是在查询中插入变量 $month$year 的方法:

 SELECT
 updates.incidentid AS `Incident ID`,
 updates.bodytext AS `Call Status`,
 updates.duration AS `Total Minutes`,
 software.`name` AS 'Support Type',
 incidents.title AS Description,
 contacts.forenames AS `First Name`,
 contacts.surname AS `Last Name`,
 FROM_UNIXTIME(incidents.opened, '%d.%m.%Y') AS `Date Logged`,
 sites.`name`,
 users.realname
 FROM
 updates
 INNER JOIN incidents ON updates.incidentid = incidents.id
 JOIN contacts ON incidents.contact = contacts.id
 INNER JOIN sites ON sites.id = contacts.siteid
 INNER JOIN users ON incidents.`owner` = users.id
 INNER JOIN software ON incidents.softwareid = software.id
 WHERE
 updates.bodytext = 'Incident Closed'
    AND FROM_UNIXTIME(incidents.opened, '%m') = '$month'   ◄■■■■
            AND FROM_UNIXTIME(incidents.opened, '%Y') = '$year'   ◄■■■■
 ORDER BY contacts.siteid ASC

将之前的代码复制粘贴到名为“report.html”和“report.php”的两个文件中,然后在浏览器中打开“report.html”。

有很多事情需要改进,例如,使用 jQuery,您可以改进用户输入月份和年份的方式,但这是另一个问题。

关于php - php 中的 sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38899864/

相关文章:

mysql - 使用jdbc通过ip连接远程mysql,但是ip变成了另一个ip

php - PHP 和 MySQL 的编码问题

php - 使用学生 ID mysql 和 php 创建登录页面

php - 为什么我的标题中有两个 session ID?

php - 使用异步 promise 处理 Guzzle 超时

javascript - 在 li 元素上使用 jQuery 设置 html 会导致错误

javascript - 保持 div 相对于图像定位

html - 即使使用 Google 指南,网站图标也不显示在搜索结果中

php - 在 SQL select 语句中将多个列值合并为单个值

javascript - 错误lib sprintf nodejs restify mysql