Jump to content

[SOLVED] form data array - need to update table


vynsane

Recommended Posts

Hi, all...

 

I have a form that is dynamically generated by the fields I have in a table. This table generates the navigation for my site, and I want to create a page that will allow me to reorder and hide/show and activate/deactivate the links. The form is working and I have the form generating an array based on the data collected on submit. I need to know how to break this data apart so I can update my site navigation table with the correct values. The table looks like this:

 

| ID | orderNum | sectionName | link | visible | active |

 

the ID is just a primary auto increment. The orderNum is one of the cells that needs to be updated by the form, as are "visible" and "active". orderNum has a value anywhere between "1" and "12" right now (but could be more depending on sections added...) and visible and active only have a value of either "Y" or "N" (for yes or no, natch...)

 

I have a form that just outputs the array onscreen after submit, and the array looks like this:

 

Array
(
    [orderNum] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
            [9] => 10
            [10] => 11
            [11] => 12
        )

    [visible] => Array
        (
            [0] => Y
            [1] => Y
            [2] => Y
            [3] => Y
            [4] => N
            [5] => Y
            [6] => Y
            [7] => Y
            [8] => Y
            [9] => N
            [10] => N
            [11] => N
        )

    [acitve] => Array
        (
            [0] => Y
            [1] => Y
            [2] => Y
            [3] => Y
            [4] => Y
            [5] => Y
            [6] => Y
            [7] => Y
            [8] => Y
            [9] => Y
            [10] => Y
            [11] => Y
        )

    [submit] => Update Navigation
)

 

obviously, the "0's" go together (1, Y, Y), the "1's" (2, Y, Y), the "5's" (4, N, Y) and so on... how can I break this data apart so as to insert/update them into the correct record?

Okay, cool. Looks pretty straight-forward. Should I use a hidden "ID" input to WHERE against in my UPDATE?

 

EDIT: Hrm... getting an error:

 

Parse error: syntax error, unexpected '=', expecting ')' in path/to/sitenav.php on line 31

 

that's this line:

 

      foreach ($data['orderNum'] as $k = $orderNum) {

 

(I changed $ono to $orderNum just so as to keep things straight in my head...)

Ah, yes, that did the trick. What about adding the ID as a WHERE in the UPDATE? Like this...

 

      foreach ($data['orderNum'] as $k => $orderNum) {
          $visible = $data['visible'][$k];
          $active = $data['active'][$k];
          $ID = $data['ID'][$k];

      $insert = "UPDATE (table name) SET
          orderNum='$orderNum',
          visible='$visible',
          active='$active'
          WHERE ID = $ID";

 

I'm getting an error on submission of the form:

 

Warning: Invalid argument supplied for foreach() in /path/to/sitenav.php on line 31

 

Should I be using "$_POST" instead of "$data"?

It's not updating the table... my "if (mysql_query($insert)) is telling me that the insert is working, but nothing is getting changed. I have the entire thing set up like this at the moment:

 

   if (isset($_POST['submit'])){
      print_r($_POST);
      foreach ($_POST['orderNum'] as $k => $orderNum) {
          $visible = $_POST['visible'][$k];
          $active = $_POST['active'][$k];
          $ID = $_POST['ID'][$k];

          // update using $orderNum, $visible, $active
        $insert = "UPDATE (table name) SET
          orderNum='$orderNum',
          visible='$visible',
          active='$active'
          WHERE ID = $ID";
      }
}

 

It's just not updating anything. I no longer got the error above

 

Warning: Invalid argument supplied for foreach() in /path/to/sitenav.php on line 31

 

using "$_POST" instead of "$data", but should I go back to "$data"?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.