Page 1 of 1

Whats wrong with this code?

Posted: Sun Jun 16, 2002 8:51 pm
by virgil
Hi,
This is driving me crazy.

How do I get multiple selected option values from a dropdown list, in a form, to insert into a table? I only seem to be able to get the first selection sent to the database. Or if I make it an array...... i.e.:

<select size="3" name="sport_type[]" multiple>
<option>baseball </option>
<option>football</option>
<option>basketball</option>
<option>soccer </option>

.......I get the just the word Array in the table column. I take this to mean that the multiple selected option values are stored, in the table column, as an array. When I use FOREACH to extract the data from the array, from the database, I still get just the word Array returned! MYSQL_FETCH_ROW does the same thing. What is wrong? Do I have to maybe nest FOREACH statements? I tried but just got an argument error.
How does it work? Actual code below:


FORM THAT SENDS DATA TO MySQL DATABASE....

<select size="3" name="date_day[]" multiple>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>




PHP RESULTS QUERY PAGE....


$query = "SELECT date_day FROM eventpost
$result = mysql_query($query);


while ($dayarray=mysql_fetch_array($result))

{
foreach ($dayarray as $day)

{
echo"<br><b>The days are </b>"."$day";
}
}


THE ACTUAL RESULTS ARE AS FOLLOWS:


The days are Array


I want it to return:

The days are 1
The days are 2
The days are 4 ...etc.


Thanks for any advice.

Posted: Sun Jun 16, 2002 11:42 pm
by volka
hmmmm...I think I didn't get the question quite. $sql = "INSERT INTO table1 (sports) VALUES ({$_POST['sport_type']})"; will replace $_POST['sport_type'] by it's string-representation as in print. For arrays this is the string 'Array'.
To see the difference try

Code: Select all

<?php print($_SERVER); print('<hr/>'); print_r($_SERVER); ?>
(all code for php-version>=4.1.0 and register_globals=Off)
From

Code: Select all

<form method="POST">
<select size="3" name="sport_type&#1111;]" multiple> 
<option>baseball </option> 
<option>football</option> 
<option>basketball</option> 
<option>soccer </option> 
</select>
<input type="submit"/></form>
you get the single values by

Code: Select all

foreach($_POST&#1111;'sport_type'] as $val)
    print($val.'<br/>');
If doing so you have to store the values in seperate rows.
i.e. (MySQL.version >=3.22.5)

Code: Select all

$sql = 'INSERT INTO table1 (sports) VALUES ('.implode(')(', $_POST&#1111;sport_type]).')';
If they all must be stored in a single row you have to create a proper string-representation. For arrays this may be achieved by using implode or serialize

IMPLODE!

Posted: Mon Jun 17, 2002 8:04 am
by virgil
IMPLODE! :D :D :D
Thats it!
Thanks Volka!