SELECTING in two tables... - php

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.

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.

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'

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

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.

Related

SQL pulling data based on date

I have a database full of store information and personal transactions. I have two webpages. One takes user input for the store name and runs a query pulling all transaction info for that store. I have another page almost identical that takes input for a start date and an end date and then runs a query for transactions between that date. The one for the store name works perfectly, but the one for the date doesn't display anything. I am still learning html/php/sql but I know exactly how the first page works so I cannot see why the same doesn't work for the second. If someone could help me, I'd appreciate it.
Here is my first page for the store name search that works.
<?php
$transaction = $_REQUEST["StoreName"];
require_once 'login.php';
$connection = mysqli_connect(
$db_hostname, $db_username,
$db_password, $db_database);
$sql = "SELECT * FROM PURCHASE WHERE StoreName LIKE '%".$transaction."%'";
$result = $connection->query($sql);
?>
Purchases Made From <?php echo $transaction ?>
<table border="2" style="width:100%">
<tr>
<th width="15%">Item Name</th>
<th width="15%">Item Price</th>
<th width="15%">Purchase Time</th>
<th width="15%">Purchase Date</th>
<th width="15%">Category</th>
<th width="15%">Rating</th>
</tr>
</table>
<?php
if($result->num_rows > 0){
// output data of each row
while($rows = $result->fetch_assoc()){ ?>
<table border="2" style="width:100%">
<tr>
<td width="15%"><?php echo $rows['ItemName']; ?></td>
<td width="15%"><?php echo $rows['ItemPrice']; ?></td>
<td width="15%"><?php echo $rows['PurchaseTime']; ?></td>
<td width="15%"><?php echo $rows['PurchaseDate']; ?></td>
<td width="15%"><?php echo $rows['PurchaseCategory']; ?></td>
<td width="15%"><?php echo $rows['Rating']; ?></td>
</tr>
<?php
}
}
?>
And here is the page for the date search that returns no data.
<?php
$startDate = $_REQUEST["StartDate"];
$endDate = $_REQUEST["EndDate"];
require_once 'login.php';
$connection = mysqli_connect(
$db_hostname, $db_username,
$db_password, $db_database);
$sql = "SELECT * FROM PURCHASE WHERE PurchaseDate BETWEEN '%".$startDate."%' and '%".$endDate."%'";
$result = $connection->query($sql);
?>
Purchases Made Between <?php echo $startDate ?> and <?php echo $endDate ?>
<table border="2" style="width:100%">
<tr>
<th width="15%">Item Name</th>
<th width="15%">Item Price</th>
<th width="15%">Purchase Time</th>
<th width="15%">Purchase Date</th>
<th width="15%">Category</th>
<th width="15%">Rating</th>
</tr>
</table>
<?php
if($result->num_rows > 0){
// output data of each row
while($rows = $result->fetch_assoc()){ ?>
<table border="2" style="width:100%">
<tr>
<td width="15%"><?php echo $rows['ItemName']; ?></td>
<td width="15%"><?php echo $rows['ItemPrice']; ?></td>
<td width="15%"><?php echo $rows['PurchaseTime']; ?></td>
<td width="15%"><?php echo $rows['PurchaseDate']; ?></td>
<td width="15%"><?php echo $rows['PurchaseCategory']; ?></td>
<td width="15%"><?php echo $rows['Rating']; ?></td>
</tr>
<?php
}
}
?>
Obviously the initial search pages that link to both of these output pages differ a bit more but I can confirm that the date output page is receiving the correct dates from the search page.
EDIT: Here is the search page for the date.
<!DOCTYPE html>
<html>
<head>
<title>Output 2</title>
</head>
<body>
<h1>Required Output 2</h1>
<h2>Transaction Search By Date</h2>
<br/>
<br/>
<form action="outputout2.php" method="get">
Start Date: <input type="date" name="StartDate">
End Date: <input type="date" name="EndDate">
<input name="Add Merchant" type="submit" value="Search">
</form>;
</body>
</html>
This sort of problem is usually the result of misformatted dates.
Your query, if I'm reading it right, says this when you've finished assembling it in php and sent it to the SQL server:
SELECT *
FROM PURCHASE --wrong!
WHERE PurchaseDate BETWEEN '%5/1/2016%' and '%5/8/2016%'
This is not going to work.
For one thing, the % signs only work with the LIKE operator, not on equality and inequality operators.
For another thing, different makes and models of database server software use differently formatted date strings. MySQL and SQL Server will work fine with string formats like 2016-05-01. But you have to look up the appropriate ways of doing this for the kind of SQL software product you are using. This kind of stuff is not very vendor-portable.
Third: your PurchaseDate column in your table needs to have a date-like datatype for this kind of thing to work.
Fourth: if your PurchaseDate items are timestamps -- that is, if that have both dates and times -- you can't reliably use the BETWEEN operator. Here's why: Suppose you have a value of 2016-05-08 11:50:00 When you compare that to an end date 2016-05-08, it lies beyond the end date. So, what you really need is this:
WHERE PurchaseDate >= '2016-05-01'
AND PurchaseDate < '2016-05-09' -- the day AFTER your end date
That construct performs the same as BETWEEN and doesn't screw up the last days' data.
Edit so it's MySQL you're using.
Workbench is a client program. So is PHP. The server is MySQL. When you do an operation like
DateColumn < 'string constant'
MySQL implicitly converts the string constant to a date. If the format of the string constant is close enough to ISO 8601 for the server to figure out what it means, it will convert it. 2016-12-13 is exactly right. 2016-5-31 is close enough for MySQL, and so is 2016-5-5. 2016/12/31 doesn't work. Neither does 5/5/16.
Germane to your question is this: MySQL cannot convert %2016-5-5% (with the percent signs) to a date.
When MySQL can't convert a date, it comes up with NULL. Comparisons with NULL values are weird. And, in the immortal words of Hunter S. Thompson, "when the going gets weird, the weird turn pro". NULL weirdness is probably the root cause of your empty result set in the query you showed.

How to send each client ID data from Database to each different email

I m struggling to send Each ClientID Information from Database to their specific Email Address.
My training exercise was to use this SQL (See Below) that have 3 different tables that are as follow: Client, Sites, Job_Cards.
Now I have create a SQL that gives me the output that I need. But my problem is based at the fact that 1 clientID may have one or Many SiteID. one SiteID may have one and many Job_Card#.
When I create my PHP Script the Data Displays as follow: Link on a HTML page.
My Question is in 2 Parts:
How can I format the table to Display ONLY 1 ClientID or ClientName with the list of Job#, site name and Description of this single Client?
How may I send an Email to Each of the ClientName Without Duplicate ID. And What php script will be good to use to send Emails to Each Clients?
Here is my PHP Script and My SQL Statment:
$sqlSelect = "SELECT DISTINCT(cl.client_id),
client_name,
DATE(jb.date),
jb.job_number,
jb.repair,
st.site_name
FROM
job_cards jb
INNER JOIN
clients cl ON (cl.client_id = jb.client_id)
LEFT JOIN
sites st on (st.site_id = jb.site_id)
WHERE
jb.completed = 1
AND cl.client_id = jb.client_id
AND jb.date >= DATE_ADD(DATE(NOW()), INTERVAL - 30 DAY)
ORDER BY cl.client_name ASC";
//echo $sqlSelect;
$tresult = mysql_query($sqlSelect);
//die("ss");
//$dataCount = mysql_num_rows($result);
while($userData = mysql_fetch_assoc($tresult)){
if($i%2==0)
$classname = 'evenRow';
else if($i%2==1)
//extract($userData);
?>
</table>
</td>
</tr>
<tr>
<td align='center' height="30" style="font-size:14px;">
<b><?php echo $userData['client_name'];?></b>
</td>
<td align='center'></td>
<td>
<table width='100%' cellpadding= '1' border='0'>
<thead>
<tr>
<td style="font-size:13px; text-align:Left;"><b>Date</b></td>
<td style="font-size:13px; text-align:Left;"><b>Job #</b></td>
<td style="font-size:13px; text-align:left;"><b>Site name</b></td>
<td style="font-size:13px; text-align:left;"><b>Description</b></td>
</tr>
</thead>
<tr class='<?php if(isset($classname)) echo $classname;?>'>
<tr>
<td>
<?php echo mysql_real_escape_string ($userData['DATE(jb.date)']); ?>
</td>
<td>
<?php echo mysql_real_escape_string($userData['job_number']);?>
</td>
<td>
<?php echo mysql_real_escape_string($userData['site_name']);?>
</td>
<td>
<?php echo mysql_real_escape_string($userData['repair']);?>
</td>
</tr>
<?php $i++;
}
?>
</tr>
</table>
<table width='100%' cellpadding= '1' border='1'>
<tr>
</tr>
</table>
Please May Some 1 Help me. I tried What I could But Still I cannot send emails and the Table format doesn't display the way I want.
Thank.

How to fetch the values using inner join

I have two tables, from that i need to fetch each row value, and print it like below.
<table id="contact" class="display">
<thead>
<tr>
<td>Name</td>
<td>Business Email</td>
<td>Mobile No</td>
<td>Company Name</td>
<td>Message</td>
</tr>
</thead>
<tr>
<td><?php echo $result['0']; ?></td>
<td><?php echo $result['1']; ?></td>
<td><?php echo $result['2']; ?></td>
<td><?php echo $result['3']; ?></td>
<td><?php echo $result['4']; ?></td>
</tr><?php
}
?>
</table>
I have tried
SELECT GROUP_CONCAT(field_value)
FROM `table2` GROUP BY `submit_time`
But it will make problem in explode step.
If comma separator came in message field, explode function increase array value, i cant fetch the correct value and print it.
Kindly share the idea to fix this.
You should have a primary key or atleast an auto increment key on the first table in order to associate it with the second table. The plugin you are using might be using it by ordering it. So every table 2 entry has a table 1 entry ordered by timestamp. What you have to do is create a new table1 column called ID and make it auto increment, it wont effect the working of the plugin. Then you can use a custom query like this.
SELECT $wpdb->table1.*
FROM $wpdb->table1
INNER JOIN $wpdb->table2 ON ($wpdb->table1.ID = $wpdb->table2.ID)
WHERE $wpdb->table2.field_name = 'text-506'

mysql_fetch_assoc returns duplicated rows...

My fetch_assoc returns duplicated rows. It seems that it multiplies itself. I have 4 inputs in my table and it returns 16.
Here is my code.... Please help me. I think I got the looping wrong.
<?php
$tryshow =" 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' ";
$result = #mysql_query($tryshow,$conn)
or die(mysql_error());
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print...";
}
?>
<form>
<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>
<?php while($row=mysql_fetch_assoc($result)){ ?>
<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>
<?php }?>
</table>
Thanks in advance.
-renz
You have two choices, if the query runs the same in phpMyAdmin. First, you can clean your data. If this is possible, its the best direction to go. Better data makes better applications possible. If you can't do much for the data integrity then you need to account for it. The simplest way would be to add a distinct to your query....
SELECT distinct 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'
your on clause
ON c.customer_date = s.date_in
should be on a unique key
ON c.room_number = s.room_number

PHP mysql multi-table search returning different table and field data row after row

I am building a social networking site that is dedicated to nightclubs and night events.
Among other tables, I have a users table, events table and establishments table.
I am really intrigued with how Facebook in particular is able to query and return matches of not just users but also pages, ads etc row after row. Im sure most who are reading this have tried the facebook search
My question is in my case, should I:
Perform 3 separate LIKE %search% on each of the tables on search.php.
Draw up 3 separate tables to show the results of what matches in the relevant queries which are collapsed when empty(on the same search.php) ie
In search.php
//query users table
$query_user = "SELECT user_first_name, user_last_name, username, picture_thumb_url, avatar FROM users JOIN picture ON users.user_id = picture.user_id
AND picture.avatar=1 ORDER BY users.user_id";
$result_users = mysql_query($query_user, $connections) or die(mysql_error());
$row_result_users = mysql_fetch_assoc($wid_updates);
//query events table
$query_event = "SELECT event_thumb_url, event_name, event_venue, event_date FROM event WHERE event_name LIKE '%".$search_value."%'";
$event = mysql_query($query_event, $connections) or die(mysql_error());
$row_event = mysql_fetch_assoc($event);
$totalRows_event = mysql_num_rows($event);
//query establishments table
$query_establishment = "SELECT establishment_thumb_url, establishment_name, location_id, establishment_pricing FROM establishment
WHERE establishment_name LIKE '%".$search_value."%'";
$establishment = mysql_query($query_establishment, $connections) or die(mysql_error());
$row_establishment = mysql_fetch_assoc($establishment);
$totalRows_establishment = mysql_num_rows($establishment);
My html:
<table max-width="500" name="users" border="0">
<tr>
<td width="50" height="50"></td>
<td width="150"></td>
<td width="150"></td>
<td width="150"></td>
</tr>
</table>
<table width="500" name="events" border="0">
<tr>
<td width="50" height="50"><a href="#profile.php"><img src="Images/<?php echo $row_event['event_thumb_url']; ?>"
border="0" height="50" width="50"/></a></td>
<td width="150"><?php echo $row_event['event_name']; ?></td>
<td width="150"><?php echo $row_event['event_venue']; ?></td>
<td width="150"><?php echo $row_event['event_date']; ?></td>
</tr>
</table>
<table width="500" name="establishments" border="0">
<tr>
<td width="50" height="50"><a href="#profile.php"><img src="Establishment_Images/<?php echo $row_establishment['establishment_thumb_url']; ?>"
border="0" height="50" width="50"/></a></td>
<td width="150"><?php echo $row_establishment['establishment_name']; ?></td>
<td width="150"><?php echo $row_establishment['location_id']; ?></td>
<td width="150"><?php echo $row_establishment['establishment_pricing']; ?></td>
</tr>
</table>
I haven't populated the PHP echo's for the user table.
This is just to give you an idea of what I am trying to do. Any assistance?
Id say there would be many different ways to go about this, however I would search all tables at once, and display the results in order of relevance. To be honest I have never needed to do this for more than two tables, but perhaps the following link will help you with your MySQL queries link text
Although this link is for full-text searching rather than the LIKE function

Categories