The first one is right (where you get error on line 61). Could you copy&paste the full code for that file again, it seems to be a case problem (i.e. Swordmen/swordmen) but should be able to solve it if you give the latest version of your code
hth
Getting buttons and textboxes within loop
Moderator: General Moderators
Re: Getting buttons and textboxes within loop
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.
if(mysql_affected_rows()>0)
{
$my[$unit] += $qty;
}
}
?>
<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>
Code: Select all
<html>
<title>Database</title>
<br />
<b>Notice</b>: Undefined index: swordmen in <b>C:\Program Files\wamp\www\www\Projektarbete\sidor\Infanteries.php</b> on line <b>62</b><br />
<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">
<tr>
<th>Swordmen</th>
<th>15/5</th>
<th>$1500</th>
<th>$500</th>
<th>0</th>
<th>
<font color="black" valign= "top"><input style='width:45;height:20' type="text" name="qty_Swordmen" size="250" class="textbox" value=""></font>
<input style='width:45;height:30;font-weight:bold' name="Swordmen" type="submit" class="submit" value="Buy!">
</th>
</tr>
<tr>
<th>Macemen</th>
<th>25/2</th>
<th>$1000</th>
<th>$333</th>
<th>0</th>
<th>
<font color="black" valign= "top"><input style='width:45;height:20' type="text" name="qty_Macemen" size="250" class="textbox" value=""></font>
<input style='width:45;height:30;font-weight:bold' name="Macemen" type="submit" class="submit" value="Buy!">
</th>
</tr>
<tr>
<th>Pikemen</th>
<th>20/10</th>
<th>$3000</th>
<th>$1000</th>
<th>0</th>
<th>
<font color="black" valign= "top"><input style='width:45;height:20' type="text" name="qty_Pikemen" size="250" class="textbox" value=""></font>
<input style='width:45;height:30;font-weight:bold' name="Pikemen" type="submit" class="submit" value="Buy!">
</th>
</tr>
</form>
</table>
</html>
"no problem there"
- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: Getting buttons and textboxes within loop
Change it to this
I've added the following lines:
43: $unit2 = "Swordmen";
50: $unit2 = "Macemen";
57: $unit2 = "Pikemen";
and amended the following:
66: $my[$unit2] += $qty;
Because some bits of your code are using the capitalised Swordmen/Macemen/Pikemen and other bits aren't, this is just a bit of a quick hack to make sure the right version of the variable is being used. To be honest it would be better to use only lowercase when referring to them in code, and stick to capitalised only for display, but this should do the trick for now
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";
$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['Pikeman']))
{
// the Pikeman Buy button was clicked - so you need to use qty_Pikemen
$unit = "pikeman";
$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.
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
if(mysql_affected_rows()>0)
{
$my[$unit2] += $qty;
}
}
?>
<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>
43: $unit2 = "Swordmen";
50: $unit2 = "Macemen";
57: $unit2 = "Pikemen";
and amended the following:
66: $my[$unit2] += $qty;
Because some bits of your code are using the capitalised Swordmen/Macemen/Pikemen and other bits aren't, this is just a bit of a quick hack to make sure the right version of the variable is being used. To be honest it would be better to use only lowercase when referring to them in code, and stick to capitalised only for display, but this should do the trick for now
Re: Getting buttons and textboxes within loop
ok ill try it, Thanks alot
Re: Getting buttons and textboxes within loop
Ok, New problem
Firstly the new code worked perfectly, but the last button dont uppdate the database
"explination:"
When i press the third (last) button nothing happens (the page is uppdated but no value is inputted into the database)
Firstly the new code worked perfectly, but the last button dont uppdate the database
"explination:"
When i press the third (last) button nothing happens (the page is uppdated but no value is inputted into the database)
- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: Getting buttons and textboxes within loop
Line 56/57 is incorrect. I've used pikeman instead of pikemen, i.e.:Goofan wrote:When i press the third (last) button nothing happens (the page is uppdated but no value is inputted into the database)
Code: Select all
$unit = "pikeman"; // this should be pikemen not pikeman
$unit2 = "Pikemen";
These two links might be of use:
http://www.oreillynet.com/pub/a/php/200 ... ngPHP.html
http://www.ibm.com/developerworks/library/os-debug/
hth
Re: Getting buttons and textboxes within loop
Thanks, yes i know but ive made a quick look then ive had to do other stuffs and forgotten where ive looked and therefor only covert alot of small areas and as i didnt have the time for bug finding i thought of posting the error and maybe some or you could find the error and well yes you did before me =) so thanks.
Ohh and it now works.
Ohh and it now works.