Page 1 of 1

Linking an update button to a certain text field.

Posted: Mon Oct 02, 2006 6:35 am
by impulse()
I have a page that's a bank simulation which has 2 tables. The first table contains a unique ID to the customer, their name and address. The second table has the unique customer ID and the customers balance.
I have writen some code that places a text box and a "modify balance" button next to each entry but when I modify the balance it modifys every entry. Is there a possible way to do this so depending on which submit button the user presses it updates only a certain DB entry relating to that button, in a loop function?

The code I have so far is:

Code: Select all

$query = mysql_query("SELECT
                            b.custID, b.balance, c.id, c.fName, c.sName, c.addr
                      FROM
                            balance as b
                      LEFT JOIN
                            custDetails as c
                      ON
                            b.custID=c.id");

?><form method="post" action="join2.php"><?php

while ($results = mysql_fetch_array($query)) {
  echo "<br><br>ID: ", $results['id'];
  echo "<br>Name: ", $results['fName'], " ", $results['sName'];
  echo "<br>Balance: ", $results['balance'];?>
  <input type="text" name="balanceMod">
  <input type="submit" value="Modify Balance">

  <?php
  } ?></form><?php

  $modBalance = $_REQUEST["balanceMod"];

  if (isset($_REQUEST["balanceMod"])) {
    $insert = mysql_query("UPDATE balance SET balance='$modBalance'");
    echo "Updated";
    }

Posted: Mon Oct 02, 2006 6:57 am
by volka
Use multiple forms, one for each record, and add the id as input/hidden

Code: Select all

<?php while ($results = mysql_fetch_array($query)) { ?>
	<form method="post" action="join2.php">
		<p>
			<input type="hidden" name="userid" value="<?php echo $results['id']; ?>" />
			...
			<input type="submit" value="Modify Balance">
		</p>
	</form>
<?php } ?>

Posted: Mon Oct 02, 2006 8:47 am
by impulse()
It took me a while to understand how it work and I have a semi-working version. If I type a value in a box it doesn't get updated untill I press the button again. Here's an example so you can understand better:

http://stesbox.co.uk/php/test/join2.php

Here's the code:

Code: Select all

while ($results = mysql_fetch_array($query)) { ?>

<form method="post" action="join2.php">
<?php echo $results['id'], "<br>", $results['fName'], " ", $results['sName'], "<br>", "\$", $results['balance']; ?>
  <input type="hidden" name="userid" value="<?php echo $results['id']; ?>">
  <input type="text" name="balanceMod"><br>
  <input type="submit" value="Mod Balance">
  </form>
  <?php
}

  $modBalance = $_REQUEST["balanceMod"];
  $boxID = $_REQUEST["userid"];

  if (isset($_REQUEST["balanceMod"])) {
    $insert = mysql_query("UPDATE balance SET balance='$modBalance' WHERE custID='$boxID'");
    echo "Updated";
    }

Posted: Mon Oct 02, 2006 9:05 am
by volka
Your update code is after your display code.
Therefore the old values are pulled from the db and printed before they are updated.

This might be the result of a misunderstanding of the http-request/php-response life-cycle. Try

Code: Select all

<?php
echo '<div>debug: ', date('H:i:s'), '</div>';
echo '<div>debug: fetching records</div>';
while ($results = mysql_fetch_array($query)) { ?>
<form method="post" action="join2.php">
<?php echo $results['id'], "<br>", $results['fName'], " ", $results['sName'], "<br>", "\$", $results['balance']; ?>
  <input type="hidden" name="userid" value="<?php echo $results['id']; ?>">
  <input type="text" name="balanceMod"><br>
  <input type="submit" value="Mod Balance">
  </form>
<?php
}
echo '<div>debug: done fetching records</div>';

echo '<div>debug: update yes/no?</div>';
$modBalance = $_REQUEST["balanceMod"];
$boxID = $_REQUEST["userid"];

if (isset($_REQUEST["balanceMod"])) {
	$insert = mysql_query("UPDATE balance SET balance='$modBalance' WHERE custID='$boxID'");
	echo "Updated";
} 
echo '<div>debug: done.</div>';
?>