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.
Whats wrong with this code?
Moderator: General Moderators
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(all code for php-version>=4.1.0 and register_globals=Off)
Fromyou get the single values by
If doing so you have to store the values in seperate rows.
i.e. (MySQL.version >=3.22.5)
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
To see the difference try
Code: Select all
<?php print($_SERVER); print('<hr/>'); print_r($_SERVER); ?>From
Code: Select all
<form method="POST">
<select size="3" name="sport_typeї]" multiple>
<option>baseball </option>
<option>football</option>
<option>basketball</option>
<option>soccer </option>
</select>
<input type="submit"/></form>Code: Select all
foreach($_POSTї'sport_type'] as $val)
print($val.'<br/>');i.e. (MySQL.version >=3.22.5)
Code: Select all
$sql = 'INSERT INTO table1 (sports) VALUES ('.implode(')(', $_POSTїsport_type]).')';