Jump to content

[SOLVED] Update MySql db from 2 Arrays


sej

Recommended Posts

Good day all,

 

I'm building a CMS and I'm having a problem updating a MySql db using 2 arrays that are being sent to php from flash. What I'm trying to accomplish is a section reordering method where when the user drags the section to a different position number in the section list, flash calculates the new section numbers associated with the section id's in the db and passes the changes to php in 2 arrays (sectIDArray & renumArray). The only way I can seem to get this to work is:

 

if ($_POST['action'] == 'sectionReorder') {

  // reorder section nums

  $sectIDArray = split ( ',', $_POST['sectIDArray']);

  $renumArray = split ( ',', $_POST['renumArray']);

  for ( $i=0;$i<count($sectIDArray);$i++){

    $var = $db .$i;

  ${$var} = new Database('server','username','password','dbname');

  $sql = 'UPDATE sections SET section_num = "'.$renumArray[$i].'" WHERE section_id = '.$sectIDArray[$i];

  ${$var}->query($sql);

  ${$var}->close();

  }

  $reorder = 'Sections have been reordered.';

  $output = 'duplicate=n&message='.urlencode($reorder);

  // display revised list

  $revisedList = 'SELECT * FROM sections'.$sectionOrder;

  echo $output .= '&'.getSectionList($revisedList);

}

 

But it would obviously be better and more efficent to only open 1 db connection so I've been trying to get this to work:

 

if ($_POST['action'] == 'sectionReorder') {

  // reorder section nums

  $sectIDArray = split ( ',', $_POST['sectIDArray']);

  $renumArray = split ( ',', $_POST['renumArray']);

  $db = new Database('server','username','password','dbname');

  for ( $i=0;$i<count($sectIDArray);$i++){

    $sql = 'UPDATE sections SET section_num = "'.$renumArray[$i].'" WHERE section_id = '.$sectIDArray[$i];

  }

  $db->query($sql);

  $db->close();

  $reorder = 'Sections have been reordered.';

  $output = 'duplicate=n&message='.urlencode($reorder);

  // display revised list

  $revisedList = 'SELECT * FROM sections'.$sectionOrder;

  echo $output .= '&'.getSectionList($revisedList);

}

 

But all this does is modify the last array element in the db and doesn't alter the other changed values. What am I doing wrong? Any help would be greatly appreciated.

 

Take care,

shayne

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.