Re: insert array values

From: Date: Fri, 06 Mar 2009 21:47:03 +0000
Subject: Re: insert array values
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-289496@lists.php.net to get a copy of this message
On Fri, Mar 6, 2009 at 3:00 PM, PJ <af.gourmet@videotron.ca> wrote: > I've been racking my little peanut-brain as well as big Google with > little hope... > I can retrieve the array from the multiple select dropdown box but I > can't quite manage to insert the data which is just id numbers for a table. > I've tried some while stuff but doesn't work. I know I don't have the > right syntax on anything at this point. Here's an effort that doesn' work.. > Dont know if I should be using $categoriesIN[] type of stuff... > but my tries were from web illustrations... > the outcommented line returns the values of the array... but how to pass > them to the query? > > foreach ($categoriesIN as $category) { >    //"$category<br>"; > $sql = "INSERT INTO book_categories ( book_id, category ) >    VALUES( book.id WHERE title = $titleIN, $category )"; >        $result = mysql_query($query, $db);; >    } Phil, you don't want to use an INSERT if you're just updating an existing row. You can't do an INSERT and use the current row's values (book.id/book_id/whatever) in your insert, since there IS no current row. I'm assuming what you're trying to do is add a record to an associative table that links Categories to Books, but only if that Book title matches your $title string. Try this: foreach($categoriesIN as $category) $sql = "update book_categories set category = '$category' where book_id in (select id from book where title = '$titleIN')"; If that's not what you're going for, and you just wanted to update something's category, try this: foreach($categoriesIN as $category) $sql = "update book_categories set category = '$category' where book_id = '$somevalue'"; Perhaps it would help us help you if you would explain what it is you're trying to accomplish with this query? -- // Todd

« previous php.general (#289496) next »