Page 2 of 7
Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 3:34 am
by Goofan
Hi i got this code new as i tried to rebuild with ure code and mine and .... alot of code i got blinded so i cant find my own fault within it.
it gets a parse error on line 120 and on line 120 i got "nothing" the code ends at line 119 so i know i got a php faul somewhere i just cant figure out where plz help and ohhh thanks for ya help
This code should work shouldn´t it?
all is one page of code just striped it down for easy readings
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());
}
?>
Code: Select all
<?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.
{
$my['Swordmen'] = $row['swordmen'];
$my['Macemen'] = $row['macemen'];
$my['Pikemen'] = $row['pikemen'];
if(isset($_POST['Swordmen']))
{
// the Swordmen Buy button was clicked
$unit = "Swordmen";
$qty = $_POST['qty_Swordmen'];
$sql="UPDATE konto SET swordmen='".$_POST[qty_swordmen]."' WHERE $id=saved_id";//Sätt upp SQL fråga.
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
}
if(isset($_POST['Macemen']))
{
// the Macemen Buy button was clicked
$unit = "Macemen";
$qty = $_POST['qty_Macemen'];
$sql="UPDATE konto SET macemen='".$_POST[qty_macemen]."' WHERE $id=saved_id";//Sätt upp SQL fråga.
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
}
if(isset($_POST['Pikeman']))
{
// the Pikeman Buy button was clicked - so you need to use qty_Pikemen
$unit = "Pikeman";
$qty = $_POST['qty_Pikemen'];
$sql="UPDATE konto SET pikemen='".$_POST[qty_pikemen]."' WHERE $id=saved_id";//Sätt upp SQL fråga.
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
}
}
?>
Code: Select all
}
?>
<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-->
Code: Select all
<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>
<?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="<?php echo $my[$row1['Units']]; ?>" size="250" class="textbox" value=""></font>
<input style='width:45;height:30;font-weight:bold' name="<?php echo $my[$row1['Units']]; ?>" type="submit" class="submit" value="Buy!">
</th>
</tr>
<?php endwhile; ?>
</table>
</html>
Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 3:44 am
by iankent
Morning Goofan
At a quick glance there's four things I'd suggest - in your queries (all three!) you do this:
pikemen/swordmen/macemen='".$_POST[qty_pikemen]."' WHERE $id=saved_id
Firstly, I assume macemen, pikemen and swordmen are all numerical columns? In that case, you don't want single quotation marks around the value you're inserting, though you do want to make sure its a number you're putting in.
Secondly, on your WHERE clause you're saying WHERE $id=saved_id - I think you should be using WHERE id=$saved_id (notice the position of the $).
Thirdly, you refer to qty_pikemen as $_POST[qty_pikemen], but earlier in the code we've assigned it to $qty_pikemen
And finally, you're setting the value to $qty_pikemen rather than adding $qty_pikemen to the existing value, so if the qty is 0 then you 'un-buy' them rather than buying nothing
So, try updating all three UPDATE queries to something like the following:
if(!is_numeric($qty_pikemen)) $qty_pikemen = 0; // make sure its a number
$sql="UPDATE konto SET swordmen=swordmen+".$qty_swordmen." WHERE id=$saved_id";//Sätt upp SQL fråga.
hth
Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 4:09 am
by Goofan
Morning to you to =) ill try ure code as i get home from work =) ohh im learning so much here today =)
"at work im learning about vb 6.0

i mix the code sometimes aswell =(

Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 4:18 am
by Goofan
Be patiant with me as im learning
now ive just made a few adjustments... does it look correct? "i cant check the code in a notpad++ as the work computer doesnt got one and i cant see if it works or not basicly im retyping it here in the forum
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());
}
?>
Code: Select all
<?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.
{
$my['Swordmen'] = $row['swordmen'];
$my['Macemen'] = $row['macemen'];
$my['Pikemen'] = $row['pikemen'];
}
if(isset($_POST['Swordmen']))
{
// the Swordmen Buy button was clicked
$unit = "Swordmen";
$qty = $_POST['qty_Swordmen'];
}
if(isset($_POST['Macemen']))
{
// the Macemen Buy button was clicked
$unit = "Macemen";
$qty = $_POST['qty_Macemen'];
}
if(isset($_POST['Pikeman']))
{
// the Pikeman Buy button was clicked - so you need to use qty_Pikemen
$unit = "Pikeman";
$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.
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
}
?>
Code: Select all
}
?>
<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-->
Code: Select all
<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>
<?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="<?php echo $my[$row1['Units']]; ?>" size="250" class="textbox" value=""></font>
<input style='width:45;height:30;font-weight:bold' name="<?php echo $my[$row1['Units']]; ?>" type="submit" class="submit" value="Buy!">
</th>
</tr>
<?php endwhile; ?>
</table>
</html>
Changed the code abit more...
Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 4:19 am
by iankent
Goofan wrote:"at work im learning about vb 6.0

i mix the code sometimes aswell =(

I know what you mean, difficult to separate out the languages at times lol. Especially when they're all so similar - I do the same with javascript and php lol!
and yes, your changes look like they should do the trick, but hard to say until you've tried running it

Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 4:21 am
by Goofan
ok thanks in advance then =) i will try it as i get home however at the top where the changes where did u see my comment? whether i need them all or if it ...

and then the last code where i changed ure input boxes.... the php code does it look ok or is it needed to be changed?
-Htgh "Happy to get help"

Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 4:27 am
by iankent
Sorry, missed that, but you seem to have 'undone' some of the earlier changes, i.e. these bits:
Code: Select all
if(isset($_POST['Pikeman']))
{
// the Pikeman Buy button was clicked - so you need to use qty_Pikemen
$unit = "Pikeman";
$qty = $_POST['qty_Pikemen'];
$sql="UPDATE konto SET pikemen='".$_POST[qty_pikemen]."' WHERE $id=saved_id";//Sätt upp SQL åga.
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
}
and have gone back to the case where you're always updating all three regardless of which button is clicked (i.e., if the user enters a 1 in all three boxes and clicks any of the submit buttons, all three get updated regardless)
Where we'd assigned the info to $unit and $qty, you don't need to put the SQL query inside the if(isset($_POST) bits, it can go at the end after you've assigned all three - means only having one update query in the code not three, i.e.
Code: Select all
if(isset($_POST['Swordmen']))
{
// the Swordmen Buy button was clicked
$unit = "Swordmen";
$qty = $_POST['qty_Swordmen'];
}
if(isset($_POST['Macemen']))
{
// the Macemen Buy button was clicked
$unit = "Macemen";
$qty = $_POST['qty_Macemen'];
}
if(isset($_POST['Pikeman']))
{
// the Pikeman Buy button was clicked - so you need to use qty_Pikemen
$unit = "Pikeman";
$qty = $_POST['qty_Pikemen'];
}
if(isset($unit)) { // if isset($unit) then one of the three buttons was clicked
if(!is_numeric($qty)) { $qty = 0; } // if $qty isn't a number, set it to 0 to protect your database from injection
$sql="UPDATE konto SET $unit=$unit + $qty WHERE id=$saved_id";//Sätt upp SQL fråga.
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
}
hope that makes sense

Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 4:33 am
by Goofan
ok so i made the changes at the old scripting above. does it look correct now
-SUPER THANKS FOR ALL URE HELP AND PATIENT
Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 4:37 am
by iankent
I think so, although two things: you seem to be missing a } on line 24 of the second block of code (the closing bracket on the intiial while($row) bit, and also, on the last bit where you run the update query:
Code: Select all
if(isset($unit)) { // if isset($unit) then one of the three buttons was clicked
if(!is_numeric($qty)) { $qty = 0; } // if $qty isn't a number, set it to 0 to protect your database from injection
$sql="UPDATE konto SET $unit=$unit + $qty WHERE id=$saved_id";//Sätt upp SQL fråga.
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
}
would possibly be better written as this:
Code: Select all
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 id=$saved_id";//Sätt upp SQL fråga.
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
}
that way, you're only running a query if $unit has been set
and $qty is a number greater than 0. In the original version (my fault sry!) the query would be run even if $qty is 0, which is a waste of resources as clearly adding 0 won't affect your data

Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 4:42 am
by Goofan
ok thanks however

the bit that got this WHERE id=$saved_id"; I dont get why u want the $ on the other side as it is said earlier in the code:
Code: Select all
$sql="SELECT * FROM konto WHERE saved_id=$id"; //selecting all from DB "Konto" [b]where saved_id is the same as in the array $id[/b]
im getting a saved id from the $id and im matching it with the saved_id that i got in my database isnt it correct if i set the $ on the id side instead of as u said the saved_id.. as the id is the array and that the saved_id is a database table...
im so sorry for all mine "however"
hope u understand
Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 4:44 am
by iankent
Oops, i'd corrected your earlier mistake of
to
when it should have been
even though there wasn't a mistake in the first place... my fault :p I'm just used to having the column on the left and the value on the right rather than the other way around
well spotted!!
Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 4:46 am
by Goofan
ok so now its all fine (i hope) ill know more when i get home in about 6 hours

Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 10:51 am
by Goofan
ok still dont work and i have no idé why i got no error nothing...
have "we" forgotten something?
code and alot of other stuffs will get into here now...
this time i dont split it up...
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.
{
$my['Swordmen'] = $row['swordmen'];
$my['Macemen'] = $row['macemen'];
$my['Pikemen'] = $row['pikemen'];
}
if(isset($_POST['Swordmen']))
{
// the Swordmen Buy button was clicked
$unit = "Swordmen";
$qty = $_POST['qty_Swordmen'];
}
if(isset($_POST['Macemen']))
{
// the Macemen Buy button was clicked
$unit = "Macemen";
$qty = $_POST['qty_Macemen'];
}
if(isset($_POST['Pikeman']))
{
// the Pikeman Buy button was clicked - so you need to use qty_Pikemen
$unit = "Pikeman";
$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.
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
}
?>
<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>
<?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="<?php echo $my[$row1['Units']]; ?>" size="250" class="textbox" value=""></font>
<input style='width:45;height:30;font-weight:bold' name="<?php echo $my[$row1['Units']]; ?>" type="submit" class="submit" value="Buy!">
</th>
</tr>
<?php endwhile; ?>
</table>
</html>
Something looking strange ask and ill explain, something u wanna re-write post it and tell me from what line to what line u rewrote and plz tell me if i need to input something somewhere or just uppdate something (the code)
Under you will see my database and then my (design) as it shows for the public.
Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 10:58 am
by Goofan
Im not sure if my row 103-105 is correct
Re: Getting buttons and textboxes within loop
Posted: Wed Nov 18, 2009 2:58 pm
by Goofan
what?