Jump to content

While loop and update


kdalts

Recommended Posts

Hi folks... I really didn't know how to title this thread so I hope it makes sense :P

 

Here is what I am trying to do:

Create an editable list of items, price and stock using the 'while' loop - this I can easily do like this...

 

echo "<table width=\"100%\" cellspacing=0>";

while($row = mysql_fetch_array($result))

  {

  echo "<tr>";

  echo "<td width=40% class=item_list>$row[mbp_name]</td>";

  echo "<td align=right width=20% class=item_list>";

  echo "<input value=\"$row[mbp_price]\" size=\"2\" maxlength=\"5\" type=\"text\" name=\"mbp_price\" /></td>";

  echo "<td align=right width=20% class=item_list>";

  echo "<input value=\"$row[mbp_stock]\" size=\"2\" maxlength=\"5\" type=\"text\" name=\"mbp_stock\" /></td>";

  echo "<td align=right width=20% class=item_list>" . $row['mbp_stock']*$row['mbp_price'] . "</td>";

  echo "</tr>";

  }

echo "</tr></table>";

 

If I have the update outside the loop then there are only 3 fields that contain a value after the loop is completed  ie: mbp_item = the last record name of the table, mbp_price=the last record price and mbp=the last record stock.  If I have the update inside the loop the table will update each record but the user may decide to cancel - so then the data in the table is wrong.

 

As an extra feature, I would also be inetersted to know the best way to check that an entered value is valid before the user moves the cursor to a new field?

 

Does anyone have a clue what the best way to do this please?

 

Link to comment
Share on other sites

  • 2 weeks later...

Umm. The post by inversesoft123 in this thread must have been intended for some other thread, because it has absolutely nothing to do with your problem.

 

You are reusing the same exact names in each set of similar form fields and the last set of fields in the form using those names will be what is submitted.

 

You should use an array for the field names. See this link - http://us3.php.net/manual/en/faq.html.php#faq.html.arrays Once you do this, you can simply use php array functions, such as foreach(), to iterate over the arrays of data from the form.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.