Re: insert array values

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



haliphax wrote:
> On Fri, Mar 6, 2009 at 4:07 PM, PJ <af.gourmet@xxxxxxxxxxxx> wrote:
>> haliphax wrote:
>>> On Fri, Mar 6, 2009 at 3:00 PM, PJ <af.gourmet@xxxxxxxxxxxx> 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?
>> Hmmm, maybe I'm chasing my own tail...
>> following the trail of execution is tough... :'(
>> I'm trying to enter a new book into the database and that means I have
>> to enter the info for the relational tables as well.
>> I think I stumbled here a bit as what I need is the id that is inserted
>> in the db before inserting this category stuff.
>> As I look at the code, it looks like i have to find the
>> "last-entered-id" for the book that is being entered.
>
> Well, there are two ways you can do that with PHP/MySQL. First, you
> can do it on the DB server. Second, you can do it in PHP after your
> insert query has been performed.
>
> MySQL:
> SELECT LAST_INSERT_ID();
>
> PHP:
> mysql_query("insert into tablename(foo, bar) values('foo', 'bar');", $db);
> echo mysql_insert_id();
>
> If you're just trying to tie a book to existing categories, I would
> insert the book and then separately insert the records in the
> associative table (book_categories).
>
> Pseudocode:
> insert book into book table
> grab last inserted id with mysql_insert_id()
> for each category book is a part of
> {
> insert book (via last inserted id) and category IDs into book_categories
> }
>
> If you're trying to insert a book and a category and then link them
> together, I would do this:
>
> Pseudocode:
> insert book into book table
> grab last inserted book id with mysql_insert_id()
> insert category into category table
> grab last inserted category id with mysql_insert_id()
> insert last book and last category IDs into book_categories
>
> Hope this helps,
Well, I did this but it don't woik...
foreach($categoriesIN as $category)
/* $sql = "update book_categories set category = '$category' where
book_id = '$autoid'";*/
$sql = "INSERT INTO book_categories ( book_id, categories_id )
VALUES ( '$autoid', '$category' )";
$result = mysql_query($sql, $db);
neither one; the INSER enter only the last value but misses the other 3
that were there; there should be like 4 book_ids with 1 and the
correspongind catgories_id should contain 1,2, 4,5 respectively...

-- 
unheralded genius: "A clean desk is the sign of a dull mind. "
-------------------------------------------------------------
Phil Jourdan --- pj@xxxxxxxxxxxxx
http://www.ptahhotep.com
http://www.chiccantine.com/andypantry.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux