/    Sign up×
Community /Pin to ProfileBookmark

trying to match database tables

I’m trying to retrieve data fields from two tables in the same database and create a printout out from the records. I get no data from either?

[code=php]<?php
include(“getinvno.php”);
$rate = $row[‘rate’];
mysql_connect(“localhost”, “root”, “”);
mysql_select_db(posdb) or die(“Unable to select database”);
$query = “SELECT * FROM saledata AS t1 INNER JOIN postable AS t2 WHERE t1.itemno = t2.itemno
AND t2.itemno = ‘{$itemno}'”;
$result=mysql_query($query);
$num=mysql_numrows($result);
echo “<center><font size=+2><b> Kirk Pawn, Gun & Archery</font><br />”;
echo “9921 Old Bearskin Rd<br />”;
echo “Hunters Den, Alabama 32145<br /></center>”;
echo “customer# “.$data[‘invno’];
echo “.. </b><br />”;
echo “tax rate “.$data[‘rate’];
echo “.. </b><br />”;
echo “<table cellspacing=1 cellpadding=1 border=0>
<tr>
<th>Qty</th>
<th></th>
<th></th>
<th>Unit</th>
<th>Amt</th>
<tr>
<th>Sold</th>
<th>item#</th>
<th>item</th>
<th>Price</th>
<th>Due</th>
<tr>
<TH colspan=5>———————————————————————————————</TH>
</tr>”;
while($row = mysql_fetch_array($result))
{
$tax = $sellprice * $rate;
$taxtotal = $taxtotal + $tax;
$amtdue = $sellprice * $sellqty;
$amtdue = $amtdue + $tax;
$total = $total + $amtdue;
$onhand – $onhand – $sellqty;
echo “<tr>”;
echo “<td>” . $row[‘sellqty’] . “</td>”;
echo “<td>” . $row[‘itemno’] . “</td>”;
echo “<td>” . $row[‘itemdescr’] . “</td>”;
echo “<td align=right>” . $row[‘sellprice’] . “</td>”;
echo “<td align=right>” . $row[‘amtdue’] . “</td>”;
}
echo “<tr>”;
echo “<TH
colspan=5>========================================================</TH>”;
echo “<tr>”;
echo “<td>Tax</td>”;
echo “<td></td>”;
echo “<td></td>”;
echo “<td></td>”;
echo “<td align=right>” . sprintf(“%.2f”,$taxtotal) .
“</td>”;
echo “<tr>”;
echo “<TH
colspan=5>========================================================</TH>”;
echo “<tr>”;
echo “<td>Total</td>”;
echo “<td></td>”;
echo “<td></td>”;
echo “<td></td>”;
echo “<td align=right>” . sprintf(“%.2f”,$total) .
“</td>”;
echo “</tr>”;
echo “</table>”;
$sql = “UPDATE postable SET
onhand = ‘” . mysql_real_escape_string($onhand) . “‘
WHERE itemno='”.$_POST[“itemno”].”‘”;
mysql_query($sql) or die(“Update query failed.”);
echo “<b>We thank you for your business</b><br />”;
?> [/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@eval_BadCode_Jun 05.2011 — You wrote the query wrong, to join (other than cross or strait join) you need something to join

[U][B][SIZE="5"]ON[/SIZE][/B][/U]

Here:

[code=php]
$resource = mysql_query(sprintf("
SELECT *
FROM saledata
INNER JOIN postable
ON saledata.itemno = postable.itemno
WHERE postable.itemno=&#37;d",$itemno));

if(mysql_errno()) die(mysql_error());

[/code]


You can use mysql_error(); to get a hint about what went wrong with a query.

Cheers

Edit:

Also fix this:

$sql = "UPDATE postable SET

onhand = '" . mysql_real_escape_string($onhand) . "'

WHERE itemno='".$_POST["itemno"]."'";

You need to cast $_POST['itemno'] to int- otherwise it's still a string... Like this:

WHERE itemno='".(int) $_
POST['itemno']."'";
×

Success!

Help @ckdoublenecks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...