Hello PHPB,

I have a form that populates a table, and creates a select only if the value of that cell equals none. The options of the select are populated dynamically as well. There are over 100 options.

My trouble is when I go to submit the form, I'm unable to distinguish between the selects for the update. It only sees the last select in the post which makes sense because all of the selects share the same name.

I guess I'm missing another evaluation of some sort...

How can I modify my code to also generate a dynamic input select name for that particular select?

What should the update script look like to catch the select $_POSTs?

This example shows two items in the lower table, they are both categorized as NONE. Thus you will see two different select boxes.

This example though, shows five.

The full source code is here, and yes I'm a newbie so my code will probably make you die a little inside. Apologies

The php snippet that does the select generation looks like this currently...

else    {
                echo '<td>';
                echo '<select style="width:300px" name="category" id="category">';
                echo '<option selected="true" value="0">SELECT A CATEGORY</option>';

//Otherwise get the list of options for a select

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");

$result = mysql_query("SELECT * from cat_wff");
$numResults = mysql_numrows($result);
for($i=0;$i<$numResults;$i++) {
echo '<option value="'.mysql_result($result,$i,"CAT_WFF").'">';
echo mysql_result($result,$i,"Category").'</option>'."\n";
}
                        echo '</select></td>';

Thank you in advance if you are able to help me understand.

Respectfully,
Aaron

    name="category[]"

    then you have an category array

      Thanks dagon.

      I started to try that but I thought that was only for multiple selects in the same select?

      Guess I could have tried anyway.

      Lemme go plug that in..

      Hmm it's still not treating each select differently, I'm still only seeing the last drop down selection in the POST.

      Now each select is named "category[]"

      I think I need someway to count the times the category = none, then increment and append each one to the name of the new select. Getting there though, I just can't grasp.

        print_r($_POST);

        and see what you get

          Indeed I get an array returned, like magic!! :p

          Each row is uniquely identified by a hidden input called "equip_id" that will determine the update and it looks like these all apply to the same equip_id though.

          [equip_id] => 32221 [category] => Array ( [0] => 1 [1] => 3 [2] => 6 [3] => 4 [4] => 4 )

          When really it should be

          equip_id = 32221 - category = 14
          equip_id = 32222 - category = 96
          equip_id = 32223 - category = 112
          and so on...

          So do I need to give the hidden field similar brackets in order for it to work the same way when dumping the array?

          I'm going to go try it.

          Thanks dagon, again.

          Respectfully,
          Aaron

            echo '<select style="width:300px" name="category['.$row['equip_id'].']" id="category">'; 

              Interpolation... DUH.
              /smacks head

              Which gets me to the next step...

              For the update script.

              How can I set a variable when I'm not sure what the equip_id is going to be going in?

              Do I trim the word 'category' away somehow leaving just the equip_id?

              In other words how will the script know to use category32221 for this equip_id row but category33209 for another?

              Here's the update script

              $cat = $_POST['category'];
              $eqid = $_POST['equip_id'];
              
              //clean input from web
              $category = mysql_real_escape_string($cat);
              $eqid = mysql_real_escape_string($equipid);
              
              // Insert the description table data
              $query = "UPDATE description SET subject ='$subject', description ='$description' WHERE onsid ='$onsid'";
              $result = mysqli_query($dbc, $query) or die( "An error has ocured: " .mysql_error (). ":" .mysql_errno ());
              $aquery = "UPDATE awd_updates SET CAT_WFF ='$cat' WHERE equip_id ='$eqid'";
              $awdupdate = mysqli_query($dbc, $aquery) or die( "An error has ocured: " .mysql_error (). ":" .mysql_errno ());
              mysqli_close($dbc);

              I can tell you aren't one for many words when it comes to newbies and if I've bugged you enough I understand. I'll keep banging on it.

              I sincerely appreciate your insight though.

              Regards,
              Aaron

                shouldent it be:

                $cat = $_POST['category'][$_POST['equip_id']];

                if you used the

                name="category['.$row['equip_id'].']

                above.

                  Yes you are correct.
                  I updated the script but posted old, I just don't seem to understand how the rows get distinguished on post to the update script.

                  the dump looks like...
                  [equip_id] => 32221 [category] => Array ( [32220] => 3 [32212] => 5 [32225] => 9 [32230] => 15 [32238] => 62 )

                  now using...

                  $cat = $_POST['category'][$_POST['equip_id']];
                  $equipid = $_POST['equip_id'];
                  print_r($_POST);

                  I'm trying to achieve a single equip_id and one single category value for each row of the table if that the select is generated in. Each select should only have one equip id and one cat value going to the update script.

                  [equip_id] = 32220, category = 14
                  [equip_id] = 009, category = 153

                  Guessing my problem is two fold.

                  For each select, set two variables, the equip_id and the category value and then get them both to the update script. Then in the update script, run the update for each row of equip_id and cat value.

                  The example links I posted earlier are just going the print_r statement on submit if you wanted to see the table structure.

                  Regards,
                  Aaron

                    I think I've worked it out.

                    I kept

                    name="category['.$row['equip_id'].']"

                    And in the update script used

                    $cat = $_POST['category'];

                    And then

                    foreach ($cat as $key => $value)
                     echo $key.'=>'.$value.'<br />';

                    Which made the output like so...

                    32220=>14
                    32212=>34
                    32225=>65
                    32230=>106
                    32238=>35

                    Pretty sure I can work with the $keys and $values in another loop to do the sql insertions using this...

                    foreach ($cat as $key => $value){
                    $aquery = "UPDATE awd_updates SET CAT_WFF ='$value' WHERE equip_id ='$key'";
                    mysqli_query($dbc, $aquery) or die( "An error has ocured: " .mysql_error (). ":" .mysql_errno ());
                    }

                    Thanks for helping dagon, I do appreciate your time.

                    Respectfully,
                    Aaron

                      Write a Reply...