//the form from which my vars come
while($each_item = mysql_fetch_array($get_items_query)) { 
echo ("...some stuff... <input type='hidden' name='id[]' value='".$each_item['id']."'>
<input type='text' size='2' maxlength='6' name='stock[]' value='".$each_item['numstock']."'>... more stuff..."); 
}

$stock = $_POST['stock'];
$id = $_POST['id'];

//make sure none of the fields are empty
foreach ($stock as $stockarray) {
if ($stockarray == "") { 
echo ("<b>You cannot leave any fields empty! Update Aborted.</b>");
exit; }

//new loop to update the database if first loop is ok.
foreach ($id as $idarray) { 
mysql_query("UPDATE products SET numstock = '$stockarray' WHERE id = '$idarray'") or die(mysql_error()); 
}

Ok so I haveeee a form thats created dynamically by pulling data from a database. basically im pulling up data from each product on my list, n sticking the current values in an editable text field. Then once the submit button is pressed i want to grab all of the vars in the "stock" field and update them to the posted vars. thats what I have so far. it works upto the last loop with the update in.. where it grabs the last posted stock number n puts it in every field instead of sticking each one in the field with the corrisponding id.. yes I realise this is what Im telling it to do, but I dont know what it should be? Im pretty sure its the "foreach ($id as $idarray) thats wrong...

    Try this:

    foreach ($id as $key => $idarray) { 
        mysql_query("UPDATE products SET numstock = '" . $stockarray[$key] . 
                    "' WHERE id = '" . $idarray . "'") 
            or die(mysql_error()); 
    }

    Incidentally, you names the arrays "stock" and "id", then name their individual elements with the suffix "array". This seems kind of backward and could lead to confusion as a script gets more complex, especially for someone else trying to understand the code.

      Yea I realised that n swapped them around since I posted this 🙂 Ok so err.. Ive made those changes n it still doesnt work =/ Heres what I have

      <input type='hidden' name='id[]' value='".$each_item['id']."'>
      <input type='text' name='stock[]' value='".$each_item['numstock']."'>
      
      $stockarray = $_POST['stock'];
      $idarray = $_POST['id'];
      
      foreach ($stockarray as $stock) {
      if ($stock == "") { 
      echo ("<b>You cannot leave any fields empty! Update Aborted.</b>");
      exit; }
      }
      foreach ($idarray as $key => $id) { 
      mysql_query("UPDATE products SET numstock = '".$stock[$key]."' WHERE id = '".$id."'") or die(mysql_error()); 
      }
      

        By "still doesnt work", I assume you mean it's doing the same as before. In that case the next thing I'd do is echo out the values you're inserting into the form fields, then echo the count and/or values of the "$_POST" variables. Also I'd change "if ($stock == "")" to "if (!empty($stock))" or "if (!$stock)", as either of those will catch more. If the variables are as expected and the different "if" doesn't help, then... 😕

          I dont understand? The $stock == "" is not the part im having a problem with- that works perfectly. The problem is with the next bit.

          Arite so under the foreach $idarray as $key bit -

          First i made it so the first value in the stock field is 1, second is 2 etc.
          echoing $stock[$key] brings up 15
          echoing $stock brings up a list of the stock values from 1 to 15
          echoing $id brings up the list of ids.

          This is right no? But Im still not getting anything, its not inserting any values.

            I suggested changing the "stock ==" part just to make absolute certain that the count of the stock array was equal to the count of the id array. That wouldn't do what I thought, but taking the array counts would. Anyway...

            So you mean the behavior has changed? Originally you said it was inserting the last posted stock number, but now it's not inserting any values?

            Just trying to help you figure it out here.

            Edit: Change "$stock" to "$stockarray" in your update query.

              Sorry I wasnt very clear. I did mean that it was inserting the last stock value, which makes sense I suppose because thats what I was telling it to do wasnt it? Changing it to

              mysql_query("UPDATE products SET numstock = '".$stockarray[$key]."' WHERE id = '".$id."'")
              

              has fixed the problem, thanks.
              I tried changing the if stock == "" part to both of the values you suggested, n both decided that I had empty fields, even when I didnt, possibly because they pick up zeros? That wasnt my intention, I just want to make sure no fields have been left blank. But as I understand it if (!empty($stock)) would mean "if stock isnt empty" would it not? I tried (empty($stock)) also but again it seems to think Ive blank fields where I dont. Anyway that part does seem to work as I had it originally, so I guess I'll leave it alone.

              One last question, is there an easy way to refresh the data in the text fields after the query, so that the user can see the new updated values? A meta refresh doesnt work, and of course if you hit update or refresh manually again it will reinsert the original values because theyre the ones showing.

                I suggested changing the "if" part because I was thinking that a blank ("") text input wouldn't even exist in the array, but I was "misremembering". Sorry to throw you off on that tangent.

                If you want to show the updated values, why not just echo them out as they're being inserted:

                mysql_query("UPDATE products SET numstock = '".$stockarray[$key]."' WHERE id = '".$id."'")
                echo 'Updated stock for ID #' . $id . ': ' . $stockarray[$key] . '<br />';

                  Hmm.. I could do that.. but then Id haveto hide the form beacause Im using if (isset($submit) { //do stuff to submit the data as apposed to sending it to a new page... Id rather refresh it so they appear in place.. kinda? can that not be done?

                    Hum. I expect this is disgustingly bad coding, but I fixed it by echoing the results of a second query "get_items_now" if the submit button has been pressed. Any improvements would be appriciated.

                      Write a Reply...