Jump to content

Archived

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

gerkintrigg

Foreach Looping - Post variables

Recommended Posts

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: [code]foreach($_POST as $varName => $value)
  {
  if (($value !="Submit") &&($varName!="Submit")){
  mysql_query("UPDATE `postage` SET `category` = '".$value."' WHERE `id` =".$varName." LIMIT 1 ;");
      }
  };[/code]

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?

Share this post


Link to post
Share on other sites
Try to explain this once more, you're trying to do multiple updates at once so something like...
[code]UPDATE $table SET $col1 = $val1, $col2 = $val2 WHERE $colid = $id[/code]
And you want to use the POST data to as the column-values?  If so then I think one approach might be...
[code]
$q = 'UPDATE ' . $table . ' SET ';
foreach ($_POST as $key => $val)
{
$q .= $key . " = '" . $val . "', \n";
}
$q .= 'WHERE id = ' . $id;
[/code]
Note that you'll want to remove the trailing ", " from the set portion of the query... I'll let you fiddle with that though.

Share this post


Link to post
Share on other sites
I have found that the easiest way of doing this is to use a switch statement and a temporary array:
[code]<?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());
}
?>[/code]
Using the implode() function alliviates the need to worry about any trailing commas...

Ken

Share this post


Link to post
Share on other sites
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:

[code]<?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>';}
  ?>[/code]

will that work or do I need to do something clever with the naming conventions?

Share this post


Link to post
Share on other sites

×

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.