the foreach() structure is a loop and you can't use it to assign to a variable.

Please do read the manual as it really does help you if you re stuck!

What you need to do is arrange the query around the loop itelse, so that the variables 
are the 
objects that are in the loop and the things that stay single ("INSERT INTO <table>") 
*do* stay 

So something like the following:

//----start off the query
$sql1 = "INSERT INTO <table_name> (";
//----start looping thru the array
foreach($array as $key=>$val) {
//---declare data to insert (helps if you have the same database fieldnames as array 
key names!)
$sql2 .= "$key,";
//----remove the last auto-generated comma from array key (field) list and add closing 
$sql3 = substr($sql2,0,-1) . ")";
//----start insertion of values list, wrapping comma's and quotes around $val
$sql4 = "VALUES (" . "'" . $val . "'" . ",";
//----remove the last auto-generated comma from values list and add closing bracket
$sql5 = substr($sql4,0,-1) . ")";
}//----end loop
//----build the parts to become a whole query and add the SQL 'WHERE' clause
$sql = $sql1 . $sql3 . $sql5 . "WHERE X=Y";

That should do it for ya.

On Tue, 11 Dec 2001 14:59:27 +0100 es said <[EMAIL PROTECTED]> wrote:

> hello again,
> russ gave me a wonderful tip, but now i'm standing in front of the next
> problem:
> if i try to insert these values out of the array into the table of the db,
> i
> probably don't know the correct way.
> this is how i tried it:
> /*
> this works fine!
> */
> foreach($werbeform as $key=>$val) {
> echo "<p><b>$key: </b>" . "$val</p>";
> }
> /*
> but this doesn't get the data into the db_table called 'werbe':
> */
> $anfrage_werbe=foreach($werbeform as $key=>$val) {
> "INSERT INTO werbe (id_main,werbeform) VALUES ('$id','$val')";
> };
> can anybody tell me what i'm doing wrong?
> the values of the checkboxes should be added to the db-table each as a new
> line, e.g. not all into one field.
> -----Ursprungliche Nachricht-----
> Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]Im Auftrag von Russ
> Michell
> Gesendet: Dienstag, 11. Dezember 2001 12:24
> An: es said
> Betreff: Re: [PHP-DB] how to access checkboxes & get them into database
> I don't think you need to get into global variables unless you *really*
> need
> to. In this case it
> sounds as if you don't.
> OK so you have an array of checked values, so what's next? You wanna get
> those variables outta that
> array in your process script, and perform whatever it is you want (inserts,
> updates etc) upon those
> variables.
> so:
> foreach($array as $key=>$val) {
> echo "<p><b>$key: </b>" . "$val</p>";
> }
> This will echo the array's keys (labels, whatever) and their associated
> values to the page. (just
> so you can see what's going on)
> You can now use these vars ($key and $val) to perform your DB inserts
> updates etc.
> I hope that was of use to you.
> Regards:
> Russ
> On Tue, 11 Dec 2001 12:14:01 +0100 es said <[EMAIL PROTECTED]> wrote:
> > hi there,
> > this time i've got another problem with my set of database-tables:
> > i created a database (containing 6 tables) and some html-sheets for the
> in-
> > and output. so far, so good.
> > some values should be added to ONE of those tables table by checking
> > checkboxes in a form.
> > okay, the checked values of the checkboxes go into an array, which is
> given
> > to the processing php-sheet as globals, but furtheri don't get yet.
> > can someone please help me?
> > all thanx !!!
> > cornelia
> >
> "Believe nothing - consider everything"
> Russ Michell
> Anglia Polytechnic University Webteam
> Room 1C 'The Eastings' East Road, Cambridge
> w:

  "Believe nothing - consider everything"       
  Russ Michell
  Anglia Polytechnic University Webteam
  Room 1C 'The Eastings' East Road, Cambridge

PHP Database Mailing List (
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to