View Full Version : Allowing multi values to be posted

11-03-2009, 03:20 PM

I have a form which contains a list (allowing multiple values to be selected)


<select name="categories" multiple="multiple" size="7">
<option value="Business Manager/Bursar">Business Manager/Bursar</option>
<option value="Cover Supervisor">Cover Supervisor</option>
<option value="Education Recruitment Consultant">Education Recruitment Consultant</option>
<option value="Middle School">Middle School</option>
<option value="Nursery Nurse">Nursery Nurse</option>
<option value="Playworker">Playworker</option>
<option value="Primary Head Teacher">Primary Head Teacher</option>
<option value="Primary Teacher">Primary Teacher</option>
<option value="School Support Staff">School Support Staff</option>
<option value="Secondary Head Teacher">Secondary Head Teacher</option>
<option value="Secondary Teacher">Secondary Teacher</option>
<option value="SEN Teacher">SEN Teacher</option>
<option value="Teaching Assistant &amp; HLTA">Teaching Assistant &amp; HLTA</option>
<option value="TESOL/TEFL/EAL">TESOL/TEFL/EAL</option>

However it will only allow me to place one value in the database (last selected)...

I have tried the following PHP but to no avail...

if (is_array($categories)) {
$categories = implode (", " , $categories);

I have also searched these forums as I expected to be a common problem, but found no suitable results.

Can anyone advise or kick me in the right direction.

Many Thanks,


11-03-2009, 03:29 PM
See http://onlinetools.org/tricks/using_multiple_select.php

11-03-2009, 03:52 PM
Following that tutorial Ive now got this code:

$categories = $_POST['categories'];
if ($categories) {
foreach ($catgories as $cats); {

The only change being I've omitted the echo as I want to pass it to a database which I do further down in the page..,

$q1 = "INSERT INTO jobseekers (regdate, uname, upass, title, fname, lname, country, zip, address, phone, categories, experience)
VALUES ('$EXmonth/$EXday/$EXyear', '$uname', '$upass', '$mtitle', '$fname', '$lname', '$country', '$zip', '$address', '$phone', '$cats', '$experience')";

However I am receiving the following error:

Warning: Invalid argument supplied for foreach() in /home/teaching/public_html/testpages/process_creg.php on line 18

Is this because the insert statement needs to come within the foreach? e.g.

$categories = $_POST['categories'];
if ($categories) {
foreach ($catgories as $cats); {
// "INSERT INTO jobseekers (categories) VALUES ('$cats')";

Or am I doing something else incorrect?

11-03-2009, 05:00 PM
I can get the values into the database with the following code, the problem is it adds a record for each option that was selected... so if I selected 4 options for the list, then it would add 4 records of the data i entered:

$values = $_POST['categories'];
foreach ($values as $a) {

$EXday = date('d', mktime(0,0,0,0, date(d) + 0, 0));
$EXmonth = date('m', mktime(0,0,0, date(m), date(d) + 0, 0));
$EXyear = date('Y', mktime(0,0,0,date(m) ,date(d) + 0, date(Y)));

$q1 = "INSERT INTO jobseekers (regdate, uname, upass, title, fname, lname, country, zip, address, phone, categories, experience)
VALUES ('$EXmonth/$EXday/$EXyear', '$uname', '$upass', '$mtitle', '$fname', '$lname', '$country', '$zip', '$address', '$phone', '$a', '$experience')";

$r1 = mysql_query($q1) or die('error'. mysql_error());

$q2 = "select * from jobseekers where uname = \"$uname\" ";
$r2 = mysql_query($q2) or die(mysql_error(Error5));
$a2 = mysql_fetch_array($r2);

$q4 = "insert into resumes set
jobseekerid = \"$a2[jobseekerid]\"";

$r4 = mysql_query($q4) or die('error');


Can anyone help me? I only want to have it create one record but list all values under categories in the database! :(