Jump to content


Photo

loop problem


  • Please log in to reply
4 replies to this topic

#1 gerkintrigg

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

Posted 26 August 2006 - 12:05 PM

Hi everyone!

I'm having a problem writing to the database from a loop. I have a dynamically created form, meaning that i don't know how many fields there will be in it and I'm trying to write each field to the database.

I'm using this code:
foreach($_POST as $varName => $value) 
  { 
  if (($value !="Submit") &&($varName!="Submit")){
  mysql_query("INSERT INTO `options` ( `option` , `id` ) 
VALUES ('".$value."', '".$r['option_id']."');");
  echo $varName.' | '.$value.'<br>';
      }
   }
but it's only writing the first field in the list, not subsequent ones.

Can anyone suggest a resolution to it?
Thanks,
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 26 August 2006 - 12:13 PM

foreach($_POST as $varName => $value) 
  { 
  if (($value !="Submit") &&($varName!="Submit")){
  mysql_query("INSERT INTO `options` ( `option` , `id` ) 
VALUES ('".$value."', '".$r['option_id']."');");
  echo $varName.' | '.$value.'<br>';
      }
   }

needs to be

foreach($_POST as $varName => $value) 
  { 
  if (($value !=="Submit") &&($varName!=="Submit")){
  mysql_query("INSERT INTO `options` ( `option` , `id` ) 
VALUES ('".$value."', '".$r['option_id']."');");
  echo $varName.' | '.$value.'<br>';
      }
   }

common mistake using = instead of ==

that *should sort it
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 gerkintrigg

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

Posted 26 August 2006 - 12:20 PM

nope... I did it before using the single = (and in the != context it should be correct), I've changed the code to what you suggested, but it still only writes the first option. ?? weird.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 26 August 2006 - 12:28 PM

try

foreach($_POST as $varName => $value) { 
  if (($value !=="Submit") &&($varName!=="Submit")){
   mysql_query("INSERT INTO `options` ( `option` , `id` ) 
VALUES ('".$value."', '".$r['option_id']."')") or die(mysql_error());;
  echo $varName.' | '.$value.'<br>';
  }
}


just taken off the ; at end of query and also added or DIE which will give us the error generated from mysql if there is a problem..
another way is to not use the query at all and just run

foreach($_POST as $varName => $value) 
  { 
 echo $varName.' | '.$value.'<br>';
   }

see if it returns all :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 gerkintrigg

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

Posted 26 August 2006 - 12:40 PM

the echo on it's own returns everything fine, which makes me think there's not a problem, but the SQL is still only uploading the first one...

Oops... I set the ID field as a primary key, which would mean that it couldn't allow more than one with the same ID, I feel so stupid!

Thanks for your help (it was the OR DIE command that could have saved me a lot more time...) ;o)
Cheers,
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