Re: insert array values

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

 



PJ wrote:
> 9el wrote:
>> On Sat, Mar 7, 2009 at 5:37 AM, PJ <af.gourmet@xxxxxxxxxxxx
>> <mailto:af.gourmet@xxxxxxxxxxxx>> wrote:
>>
>>     I've done some rethingking and this may be the direction to go:
>>
>>     What I need to wind up with is something like this:
>>
>>     $sql = "INSERT INTO book_categories ( book_id, category )
>>        VALUES( '$autoID', '$categoriesID[0]' ),
>>        ( '$autoID', '$categoriesID[1]' ),
>>        ( '$autoID', '$categoriesID[2]' ),
>>        ( '$autoID', '$categoriesID[3]' )
>>        ( '$autoID', '$categoriesID[4]' )";
>>
>>     Does it make sense?
>>     How do I pass the values to the query?
>>
>>
>>
>> You can run a php loop inside the insert  clause.
>> But if its already existing record then it woud be UPDATE clause.
>>
> Could I have a small hint as to how to do that. I'll check the manual
> and search G in the meantime.  Right now, it's Friday night and I'm
> about to get whammied by the wife... ;-)      have a good night & let's
> hope for a brighter tomorrow, tomorrow, tomorrowwww
> 


To do something like what you are describing above, you would do the following:

<?php

# Setup your DB connection etc...


# Build insert statement for your book
$sql = "INSERT INTO books (title, author, etc...) VALUES ('To kill a mocking bird', 'Harper Lee', etc...)";

# Execute insert statement
if ( ( $results = mysql_query($sql, $dblink) ) !== false ) {

	# Grab last insert ID for my thread
	$last_id = mysql_insert_id($dblink);

	# Check to see if any categories were choosen
	if ( $categoriesIN ) {
		$values = array();

		# Loop through each category and build VALUES entry...
		foreach ( $categoriesIN as $k => $id ) {

			# Build VALUES entry, plus run $id through escape function for sanity
			$values[] = "( {$last_id}, ".mysql_real_escape_string($id)." )";
		}

		# Join the VALUES entries just created and separate them with a comma
		$sql = "INSERT INTO book_categories ( book_id, category ) VALUES " . join(', ', $values);

		# Execute book 2 categories SQL entry
		if ( ( $results = mysql_query($sql, $dblink) ) === false ) {

			# If it fails, show me why
			echo 'Book to category entry failed: '.mysql_error($dblink);
		}
	}
} else {
	# Show why my book SQL entry failed
	echo "Book insert failed: ".mysql_error($dblink);
}

?>

That is about as best as can be explained.

BTW - I took away the quotes around your values in your second insert statement.  They are numbers and numbers should not have quotes around them.

Hopefully this works, or guides you in the direction of the answer you seek.

-- 
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
       and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
    by William Shakespeare

-- 
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