在两个表中选择...

发布于 2024-10-20 07:45:06 字数 2107 浏览 1 评论 0原文

我有两个表格,我想用它们来查看我的报告,输入日期后可以得到这些报告。

这是我的表格: 对于客户 - customer_date、lastname、firstname

对于服务 - room_number、date_in、date_out

现在这是我的代码:似乎它无法从我的表中获取任何行有了

<?php 

$conn = mysql_connect("localhost","root","");

mysql_select_db('irm',$conn);

if(isset($_GET['Submit'])){

$customer_date = $_GET['customer_date'];
}

?>
<form method="get">
<table width="252" border="0">
  <tr>
    <td width="98">Choose Date:</td>
    <td width="144"><label>
  <input onclick="ds_sh(this);" name="customer_date" id="customer_date" readonly="readonly" style="cursor: text" />
</label></td>
 </tr>
 <tr>
<td align="right"><input type="submit" value="Submit" /></a></td>
<td></td>
</tr>
</table>
</form>  

<form>
<?php

    $tryshow = "SELECT * FROM customers,services WHERE customer_date = '$customer_date' ";

    $result = @mysql_query($tryshow,$conn)
            or die("cannot view error query"); 

    if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print...";
}
while($row=mysql_fetch_assoc($result)){
?>
<table width="700" border="0">
    <tr>
      <td width="100">Customer Date:</td>
      <td width="100">Last Name</td>
      <td width="100">First Name</td>
      <td width="100">Room Number</td>
      <td width="100">Date In</td>
      <td width="100">Date Out</td>
    </tr>
    <tr>
      <td><?php echo $row["customer_date"]; ?></td>
      <td><?php echo $row['lastname']; ?></td>
      <td><?php echo $row['firstname']; ?></td>
      <td><?php echo $row['room_number']; ?></td>
      <td><?php echo $row['date_in']; ?></td>
      <td><?php echo $row['date_out']; ?></td>
   </tr>
  </table>
<?php }?>
</form>

这个我就可以获得任何客户的报告谁在该日期签到。

我需要一些建议。希望你能尽快回复我。

I have two tables that I want to use for viewing my reports which I can get after inputting a date.

Here are my tables: for customers - customer_date, lastname, firstname

for services - room_number, date_in, date_out

Here is my code now : it seems that it can't get any rows from my table

<?php 

$conn = mysql_connect("localhost","root","");

mysql_select_db('irm',$conn);

if(isset($_GET['Submit'])){

$customer_date = $_GET['customer_date'];
}

?>
<form method="get">
<table width="252" border="0">
  <tr>
    <td width="98">Choose Date:</td>
    <td width="144"><label>
  <input onclick="ds_sh(this);" name="customer_date" id="customer_date" readonly="readonly" style="cursor: text" />
</label></td>
 </tr>
 <tr>
<td align="right"><input type="submit" value="Submit" /></a></td>
<td></td>
</tr>
</table>
</form>  

<form>
<?php

    $tryshow = "SELECT * FROM customers,services WHERE customer_date = '$customer_date' ";

    $result = @mysql_query($tryshow,$conn)
            or die("cannot view error query"); 

    if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print...";
}
while($row=mysql_fetch_assoc($result)){
?>
<table width="700" border="0">
    <tr>
      <td width="100">Customer Date:</td>
      <td width="100">Last Name</td>
      <td width="100">First Name</td>
      <td width="100">Room Number</td>
      <td width="100">Date In</td>
      <td width="100">Date Out</td>
    </tr>
    <tr>
      <td><?php echo $row["customer_date"]; ?></td>
      <td><?php echo $row['lastname']; ?></td>
      <td><?php echo $row['firstname']; ?></td>
      <td><?php echo $row['room_number']; ?></td>
      <td><?php echo $row['date_in']; ?></td>
      <td><?php echo $row['date_out']; ?></td>
   </tr>
  </table>
<?php }?>
</form>

With this I can get a report of any customer who checks in on that date.

I need some advice. Hope you can answer me soon.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瑶笙 2024-10-27 07:45:06

两个表之间似乎没有任何共同字段。如何存储客户 A 在日期 C 位于 B 房间的事实?要进行 SQL 连接,被连接的表必须至少有一个公共字段。

同样,不要只是说 die("cannot view error query") ,这对于调试目的完全没有用,而是尝试执行 die(mysql_error() ,这将给出 同样,如果查询确实有效

,那么您将为找到的每一行输出整个 HTML 表,并且表头和页脚数据应该位于获取循环之外。

You don't appear to have any fields in common between the two tables. How do you store fact that customer A was in room B on date C? To do an SQL join, the tables being joined have to have at least one field in common.

As well, instead of just saying die("cannot view error query"), which is utterly useless for debugging purposes, try doing die(mysql_error(), which will give you the exact reason the query failed.

As well, if the query DOES work, then you're outputting an entire HTML table for each row found. You should have the table headers and footers data OUTSIDE of the fetch loop.

悟红尘 2024-10-27 07:45:06

您需要使用 JOIN 将两个表关联起来。根据所提供的信息,customers.customer_dateservices.date_in 似乎是最有可能的候选者。这假设日期列仅包含日期而不是日期/时间。

另请注意,我在查询中没有使用 select *,您也不应该使用。 ;-)

SELECT c.customer_date, c.lastname, c.firstname,
       s.room_number, s.date_in, s.date_out
    FROM customers c
        INNER JOIN services s
            ON c.customer_date = s.date_in
    WHERE c.customer_date = '$customer_date' 

You need to relate the two tables with a JOIN. Based on the information given, customers.customer_date to services.date_in seems to be the most likely candidate. This assumes that the date columns hold only a date and not a date/time.

Also notice that I'm not using select * in my query and neither should you. ;-)

SELECT c.customer_date, c.lastname, c.firstname,
       s.room_number, s.date_in, s.date_out
    FROM customers c
        INNER JOIN services s
            ON c.customer_date = s.date_in
    WHERE c.customer_date = '$customer_date' 
云雾 2024-10-27 07:45:06

当你对数据库进行查询时,请确保你的日期格式是yyyy-mm-dd

Mysql只理解这种格式的日期,这样你就必须只比较这种格式的日期格式。

您的 $customer_date 应采用 yyyy-mm-dd 格式

When your making query to database make sure your date format is yyyy-mm-dd

Mysql understand date in this format only so that you have to compare the date format in this format only.

your $customer_date should be in the yyyy-mm-dd format

情感失落者 2024-10-27 07:45:06

顺便说一句,我会将 customer_date 更改为更有意义的内容,例如“date_in”。 (当名称是可预测的时,这是一件好事!)您不需要指定它是客户,因为它已经在客户表中。

As an aside, I'd change customer_date to something more meaningful such as "date_in." (It's a good thing when the names are predictable!) You don't need to specify that it's the customer since it's in the customer table already.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文