Page 2 of 2
Re: Collect from 2 datatable in 1 querry
Posted: Sat Nov 28, 2009 2:40 pm
by daedalus__
let's see the updated code?
if you want to be able to buy units of all three types then you will have to run queries for each unit.
Re: Collect from 2 datatable in 1 querry
Posted: Sat Nov 28, 2009 7:17 pm
by Goofan
still the same...
Code: Select all
<?php
// Get the database connector stuff
include "../login/database.php";
// Build our query
$sql1 = "SELECT * FROM Units";
// 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 = "select buyfor from units ";
$result = mysql_query($sql);
$buyfor = mysql_result($result,0);
$product_price = $buyfor * $qty;
if ($pengar >= $product_price)
{
echo "You have succesfully bought $qty $unit ";
$sql="UPDATE konto SET $unit=$unit + $qty & $pengar= ($pengar - $product_price) WHERE saved_id=$id";//Sätt upp SQL fråga.
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
if(mysql_affected_rows()>0)
{
$my[$unit2] += $qty;
}
}
else
{
echo "You dont afford that many.";
}
}
?>
<html>
<head>
<link href="../css/style.css" rel="stylesheet" type="text/css">
<title>Infantrie</title>
</head>
<body bgcolor="#A4A8B0">
<style type="text/css"> <!--selecting the style type-->
<!--
a:link {text-decoration: none; color: black}
a:visited {text-decoration: none; color: black}
a:active {text-decoration: none; color: black}
a:visited {text-decoration: none; color: black}
.textbox { background-color: #DEB887; }
.submit { background-color: #DEB887; }
-->
</style> <!--ending the style type-->
<p>
Är du villig att köpa soldater? Tror du att du är man nog att göra det?
</p>
<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>
</html>
look throught the code and so on..

Re: Collect from 2 datatable in 1 querry
Posted: Sat Nov 28, 2009 8:21 pm
by daedalus__
Re: Collect from 2 datatable in 1 querry
Posted: Sun Nov 29, 2009 3:12 pm
by Goofan
If this is the change of the syntax u were speaking of then no it aint either working
Code: Select all
$sql="UPDATE konto SET $unit=$unit + $qty, $pengar=$pengar - $product_price WHERE saved_id=$id";//Sätt upp SQL fråga.
Else please explain exacly what it is that i dont got correct..
Re: Collect from 2 datatable in 1 querry
Posted: Sun Nov 29, 2009 3:50 pm
by daedalus__
what did the error say?
and like i said before
if you say $sql = "UPDATE konto SET $unit = $unit + $qty, $pengar = $pengar - $product_price WHERE savid_id = $id"
let's say
Code: Select all
$unit = 0x01; // unit id number
$qty = 23;
$pengar = 80000;
$product_price = 20000;
then you are saying
Code: Select all
$sql = "UPDATE konto SET 0x01 = 0x01 + 23, 80000 = 80000 - 20000
when you use double-quotes to enclose a string, variables are changed from their identifier to their value.
you need to double check the command you are running on the sql server to make sure it is correct:
Code: Select all
$sql = "UPDATE konto SET unit = ($unit * 23), pengar = ($pengar - 20000)
echo $sql;
$result = mysql_query($sql)
if (!$result)
{
echo 'failure: ' . mysql_error();
}
http://php.net/manual/en/function.mysql-query.php
i tried to find some swedish (i hope thats correct) language tutorials but i was unsuccessful. im not sure how many languages you speak but i know that the php manual is available in german and polish if you understand either of those better than english.
Re: Collect from 2 datatable in 1 querry
Posted: Mon Nov 30, 2009 2:24 am
by Goofan
Code: Select all
$sql="UPDATE konto SET $unit=$unit + $qty WHERE saved_id=$id";//Sätt upp SQL fråga.
this code work but when i add the last $pengar=$pengar - Product_price then i get the error
unit is unit.... it is checking what submit button that was used from those 3 i got.. so for exampel...
$sql="UPDATE konto SET $"button2"=$"button2" + $"what number that have been enterd into the textbox.
so the error is that something with the extra ,$pengar=$pengar - Product_price ;is not working.
the button number is set if u look into the text.
so if this is not what u mean by:
$sql = "UPDATE konto SET 0x01 = 0x01 + 23, 80000 = 80000 - 20000
you need to double check the command you are running on the sql server to make sure it is correct:
swedish is my "first" language my second is english third would be german (dont speak that much german though)
i understand english very well but i dont see what u mean thats all... as i look into my code i look as if it were correct i dont get the "wrong" in it
i dont know how else to "double check the button as u described.

Re: Collect from 2 datatable in 1 querry
Posted: Mon Nov 30, 2009 3:33 am
by Goofan
ohh sorry now i got what u ment by double check...
yes its all correct... (i see it as if it were all corrected, found out after that it wasnt)
Error is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '800000=800000 - 18000 WHERE saved_id=2' at line 1
Re: Collect from 2 datatable in 1 querry
Posted: Mon Nov 30, 2009 4:01 pm
by daedalus__
goofan the query isn't correct.
im not here to just fix your code. im trying to show you how to fix it yourself.
yeah there is a problem with
Code: Select all
$string = "$pengar = $pengar - $product_price"
$pengar is a variable
when you place a variable inside of double quotes like this
it replaces the name of the variable with the value of the variable. understand?
Code: Select all
$variable = "value1";
$string = "this is a string with a $variable in it";
will be processed as
Code: Select all
$string = "this is a string with a value1 in it";
http://php.net/manual/en/language.types.string.php
so like i said before
if you say
Code: Select all
$unit = 0x01; // unit id number
$qty = 23;
$pengar = 80000;
$product_price = 20000;
$id = 1;
$sql = "UPDATE konto SET $unit = $unit + $qty, $pengar = $pengar - $product_price WHERE savid_id = $id";
then after it is processed it will be
Code: Select all
$sql = "UPDATE konto SET 0x01 = 0x01 + 23, 80000 = 80000 - 20000 WHERE savid_id = 1";
you want to say something like
Code: Select all
$sql = "UPDATE konto SET $unit_qty = $current + $purchased, pengar = $pengar - $product_price WHERE savid_id = $id";
im running out of ways to explain it
Code: Select all
echo "You have succesfully bought $qty $unit";
$sql="UPDATE konto SET $unit=$unit + $qty WHERE saved_id=$id";//Sätt upp SQL fråga.
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
v------------------------------------------------------------------------------------------------- without the $
$sql="UPDATE konto SET pengar=$pengar - $product_price WHERE saved_id=$id";//Sätt upp SQL fråga.
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
no:
Code: Select all
$sql="UPDATE konto SET $pengar=$pengar - $product_price WHERE saved_id=$id";//Sätt upp SQL fråga.
yes:
Code: Select all
$sql="UPDATE konto SET pengar=$pengar - $product_price WHERE saved_id=$id";//Sätt upp SQL fråga.
Re: Collect from 2 datatable in 1 querry
Posted: Tue Dec 01, 2009 1:53 am
by Goofan
ohh ok ill see if i can make it work "now as i do undertand alot more".
I can tell you no more then i were blinded by the code i mean 44 pages of code and about 400 rows of code on each page it is alot of code "you do get blind from the code" but thanks for ure patient. i think i can manage it now.
Re: Collect from 2 datatable in 1 querry
Posted: Tue Dec 01, 2009 12:34 pm
by Goofan
made it and got it now

Re: Collect from 2 datatable in 1 querry
Posted: Tue Dec 01, 2009 9:57 pm
by daedalus__
thats good to hear. you're welcome!