Jump to content


Photo

Foreach Looping - Post variables


  • Please log in to reply
3 replies to this topic

#1 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 01 August 2006 - 04:21 PM

Hi!!

I'm trying to loop using "foreach" to grab all of my posted variables and update my database. It worked fine in this code:
foreach($_POST as $varName => $value) 
  { 
  if (($value !="Submit") &&($varName!="Submit")){
  mysql_query("UPDATE `postage` SET `category` = '".$value."' WHERE `id` =".$varName." LIMIT 1 ;");
      }
   };

but now I'm trying to update two fields using the same method for each run through of the loop... as the fields in the form are automatically generated, can anyone suggest a way that I can name the fields so as to grab all the information in one go?

I tried using preg_match and split the name up by naming it something like name_1 and splitting it at the underscore, but I'm rubbish with all the preg commands and I need guidance.

Can anyone understand what I mean and off help please?
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#2 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 01 August 2006 - 04:33 PM

Try to explain this once more, you're trying to do multiple updates at once so something like...
UPDATE $table SET $col1 = $val1, $col2 = $val2 WHERE $colid = $id
And you want to use the POST data to as the column-values?  If so then I think one approach might be...
$q = 'UPDATE ' . $table . ' SET ';
foreach ($_POST as $key => $val)
{
$q .= $key . " = '" . $val . "', \n";
}
$q .= 'WHERE id = ' . $id;
Note that you'll want to remove the trailing ", " from the set portion of the query... I'll let you fiddle with that though.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 August 2006 - 04:47 PM

I have found that the easiest way of doing this is to use a switch statement and a temporary array:
<?php
$qtmp = array(); // to hold the "set" pieces of the query
foreach($_POST as $k=>$v)
    switch ($k) {
        case 'field1':
        case 'field2':
              $qtmp[] = $k . " = '" . mysql_real_escape_string($v) . "'";
              break;
    }
if (!empty($qtmp)) {
   $q = "update tablename set " . implode(', ',$qtmp) . " where id = " . $idl;
   $rs = mysql_query($q) or die("Problem with query: $q<br>" . mysql_error());
}
?>
Using the implode() function alliviates the need to worry about any trailing commas...

Ken

#4 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 01 August 2006 - 05:36 PM

Hiya Buyocat & Super Guru Ken... *Bows deeply*

I'm happy with Ken's reply - Buyocat, thanks for yours too, but I'd like to know what to call each field in the initial form.

Currently I'm doing this:

<?php 
		  while ($post_r=mysql_fetch_array($post_sql)){
		  echo'<tr bgcolor="#FFFFFF">
            <td class="text"><input name="'.$post_r['format'].'_1" type="text" class="search" value="'.$post_r['format'].'"></td>
            <td class="text"><input name="'.$post_r['weight_min'].'_1" type="text" class="search" value="'.$post_r['weight_min'].'"> - <input name="'.$post_r['weight_max'].'" type="text" class="search" value="'.$post_r['weight_max'].'">g</td>
            <td class="text">£<input name="'.$post_r['id'].'_1" type="text" class="search" value="'.$post_r['first'].'"></td>
            <td class="text">£<input name="'.$post_r['id'].'_2" type="text" class="search" value="'.$post_r['second'].'"></td>
          </tr>';}
		  ?>

will that work or do I need to do something clever with the naming conventions?
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users