在两个表中选择...
我有两个表格,我想用它们来查看我的报告,输入日期后可以得到这些报告。
这是我的表格: 对于客户 - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
两个表之间似乎没有任何共同字段。如何存储客户 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 doingdie(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.
您需要使用 JOIN 将两个表关联起来。根据所提供的信息,
customers.customer_date
到services.date_in
似乎是最有可能的候选者。这假设日期列仅包含日期而不是日期/时间。另请注意,我在查询中没有使用
select *
,您也不应该使用。 ;-)You need to relate the two tables with a JOIN. Based on the information given,
customers.customer_date
toservices.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. ;-)当你对数据库进行查询时,请确保你的日期格式是
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 theyyyy-mm-dd
format顺便说一句,我会将 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.