Collect from 2 datatable in 1 querry
Posted: Fri Nov 27, 2009 7:26 am
Hi i got a problem that i cant manage to get around...
i got this code and im trying to collect from 2 datatables and use them in 1 querry
look int the text for
-------------->
MY PROBLEM
<--------------
then i got some more code further down the page:
this is only one page....
i got this code and im trying to collect from 2 datatables and use them in 1 querry
look int the text for
-------------->
MY PROBLEM
<--------------
Code: Select all
<?php
// Get the database connector stuff
include "../login/database.php";
// Build our query
$sql1 = "SELECT * FROM infantries";
// Get the result if there is one
// DO NOT die() IN PRODUCTION!!!
if (!$result1 = mysql_query($sql1))
{
die('The query<br /><strong>' . $sql1 . '</strong><br />failed:<br />' . mysql_error());
}
?>
<?php
$id =(isset($_GET['saved_id'])) ? (int)$_GET['saved_id'] : false;
if($id !== false)
{
$sql="SELECT * FROM konto WHERE saved_id=$id"; //selecting all from DB "Konto" where saved_id is the same as in the array $id
}
else
{
echo "NO saved_id!";
}
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tall. //hämtar all info från tabell
while($row = mysql_fetch_array( $result )) //hämtar info från tabell.
{
$pengar=$row['pengar'];
$my['Swordmen'] = $row['swordmen'];
$my['Macemen'] = $row['macemen'];
$my['Pikemen'] = $row['pikemen'];
}
echo "<p>$pengar</p>";
if(isset($_POST['Swordmen']))
{
// the Swordmen Buy button was clicked
$unit = "swordmen";
$unit2 = "Swordmen";
$qty = $_POST['qty_Swordmen'];
}
if(isset($_POST['Macemen']))
{
// the Macemen Buy button was clicked
$unit = "macemen";
$unit2 = "Macemen";
$qty = $_POST['qty_Macemen'];
}
if(isset($_POST['Pikemen']))
{
// the Pikeman Buy button was clicked - so you need to use qty_Pikemen
$unit = "pikemen";
$unit2 = "Pikemen";
$qty = $_POST['qty_Pikemen'];
}
if(isset($unit) && is_numeric($qty) && $qty > 0)
{ // if isset($unit) then one of the three buttons was clicked
$sql="UPDATE konto SET $unit=$unit + $qty WHERE saved_id=$id";//Sätt upp SQL fråga.
$sql="UPDATE konto SET $pengar - $Buyfor WHERE saved_id=$id";//Sätt upp SQL fråga.
<--------------
MY PROBLEM
---------------
i need it to say $pengar(containing 800 000) - $Buyfor(containing the price of the thing)
so that if the costumer buys a thing for 300 it will become 799 700
'This it cant find ($Buyfor) i know that in my database infantries it exists
$pengar is in the second datatable and it is also existing
-------------->
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
if(mysql_affected_rows()>0)
{
$my[$unit2] += $qty;
}
}
?>
this is only one page....
Code: Select all
<table border="1">
<tr>
<th>Units</th>
<th>AtkArm</th>
<th>Buyfor</th>
<th>Sellfor</th>
<th>You Own</th>
<th>Buy</th>
</tr>
<form action="#" method="post">
<?php while ($row1 = mysql_fetch_assoc($result1)): ?>
<tr>
<th><?php echo $row1['Units'] ;?></th>
<th><?php echo $row1['AtkHp'] ;?></th>
<th><?php echo $row1['Buyfor'] ;?></th>
<th><?php echo $row1['Sellfor'] ;?></th>
<th><?php echo $my[$row1['Units']]; ?></th>
<th>
<font color="black" valign= "top"><input style='width:45;height:20' type="text" name="qty_<?php echo $row1['Units']?>" size="250" class="textbox" value=""></font>
<input style='width:45;height:30;font-weight:bold' name="<?php echo $row1['Units']?>" type="submit" class="submit" value="Buy!">
</th>
</tr>
<?php endwhile; ?>
</form>
</table>