On Mon, Feb 23, 2009 at 10:25 AM, PJ <[email protected]> wrote:
> I think this is a tough one... and way above my head:
> PLEASE READ ALL OF THE ABOVE TO UNDERSTAND WHAT I AM TRYING TO DO.
> Having a bit of a rough time figuring out how to formulate php-mysql to
> insert data into fields using a multiple dropdown box in a form.
>
> to post I am using the following:
> snip...
> $categoriesIN = $_POST["categoriesIN"];
>
> ...snip...
>
> <select name="$categoriesIN[]" multiple="multiple">
> <OPTION>Choose Categories...</option>
> <OPTION VALUE="<? echo $categoriesIN; ?>">1
> <OPTION VALUE="<? echo $categoriesIN; ?>">2
> <OPTION VALUE="<? echo $categoriesIN; ?>">3
> <OPTION VALUE="<? echo $categoriesIN; ?>">4
> <OPTION VALUE="<? echo $categoriesIN; ?>">5
> </SELECT>
>
> ...snip...
>
> $sql4 = "FOR ( $ii = 0 ; $ii < count($categoriesIN) ; $ii++ )
> INSERT INTO temp (example) $categoriesIN[$ii]" ;
> $result4 = mysql_query($sql4, $db);
> ...snip
>
> this does not work! The other posts work like a charm... but this...
You are confusing the PHP and MySQL.
<?php
$count = count($categoriesIN);
for ($ii = 0; $ii < $count; ++$ii) {
$sql = "INSERT INTO temp (example) VALUES ('" .
mysql_real_escape_string($categoriesIN[$ii]) . "')";
$result = mysql_query($sql, $db);
}
?>
MySQL also has an alternative syntax that you can use to do this in a
single statement:
<?php
$count = count($categoriesIN);
if ($count > 0) {
$sql = "INSERT INTO temp (example) VALUES ";
for ($ii = 0; $ii < $count; ++$ii) {
if ($ii > 0) $sql .= ', ';
$sql .= "('" . mysql_real_escape_string($categoriesIN[$ii]) . "')";
}
}
$result = mysql_query($sql, $db);
?>
Andrew
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php