Hello:

I have a select box which offers the user the option of selecting multiple selections.

I'm a bit lost as to how --if multiple selections are made, to save these selections into mysql.

here is the code of my select box:

<form name="theForm">
<select size=11 name=servicearea id=select multiple onMouseDown="GetCurrentListValues(this);" onchange="FillListValues(this);" style="width:200;">  
  <option value="Air Intake">Air Intake</option>
  <option value="Brake">Brake</option>
  <option value="Clutch">Clutch</option>
  <option value="Cooling System">Cooling System</option>
  <option value="Oil Change">Oil Change</option>
  <option value="Driveshaft & Axle">Driveshaft & Axle</option>
  <option value="Engine Mechanical">Engine Mechanical</option>
  <option value="Exhaust">Exhaust</option>
  <option value="Steering">Steering</option>
  <option value="Suspension">Suspension</option>
  <option value="Transmission">Transmission</option>
  <option value="Other">Other</option>
  </select>
  <br>
  <font face=arial size=1><a href="javascript:SelectAllList(document.theForm.servicearea);" style="color:blue;">select
  all</a> &nbsp;&nbsp;<a href="javascript:DeselectAllList(document.theForm.servicearea);" style="color:blue;">deselect
  all</a></font>
</form>

Any thoughts on this is appreciated!
Mossa

In order to store multiple selections, you can send the data as an array. In your case use the name of the select box as servicearea[] instead of servicearea.

When you use the $servicearea=$_GET['servicearea']; in the PHP code, the $servicearea variable becomes an array with all the selected values. You can then use the to store the required data into your MySQL DB.

A few tips though as to your style of coding. Programming in HTML has its own semantics. Even though the HTML program would work fine if they are not followed, it is always better to follow the Web Standards.

1. Always enclose the attributes in quotes ("").
2. You have not specified if you want a method="post" or method="get" in your form.
3. Specify where the form will take you using the action attribute.
4. The JavaScript in the <a> tag for select/deselect all has a <b></b> in it.

etc. etc. etc.

But anyways, I hope you get your code working because that's more important that Valid markup.

Thanks for the replies!

I have managed to get it working, but need a bit of tweaking.
Here is what I have

<?php
//error_reporting(0);
//error_reporting(E_ERROR | E_WARNING | E_PARSE);
error_reporting(E_ALL);

$host = "localhost";
$login_name = "-----";
$password = "----";

$link=mysql_connect("$host","$login_name","$password");		
mysql_select_db("shop", $link); 
//or die("Could not find database");
//echo mysql_errno() . ": " . mysql_error(). "\n";

$servicearea= $_POST['servicearea'];
if( is_array($servicearea)){
while (list ($key, $val) = each ($servicearea)) 
{
echo "$val <br>";
$mysql_query=("INSERT servicesrendered SET servicearea='$val'");
$result=mysql_query($mysql_query); 
if(!$result){die("Error: ".mysql_error());
}}}
else{echo "not array";}
?>

my select box:

<form name="theForm" method="post" action="select_multiple_box2.php">
<select size=11 name=servicearea[] id=select multiple onMouseDown="GetCurrentListValues(this);" onchange="FillListValues(this);" style="width:200; height:100">  
  <option value="Air Intake">Air Intake</option>
  <option value="Brake">Brake</option>
  <option value="Clutch">Clutch</option>
  <option value="Cooling System">Cooling System</option>
  <option value="Oil Change">Oil Change</option>
  <option value="Driveshaft & Axle">Driveshaft & Axle</option>
  <option value="Engine Mechanical">Engine Mechanical</option>
  <option value="Exhaust">Exhaust</option>
  <option value="Steering">Steering</option>
  <option value="Suspension">Suspension</option>
  <option value="Transmission">Transmission</option>
  <option value="Other">Other</option>
  </select>
  
  <font face=arial size=1><a href="javascript:SelectAllList(document.theForm.select);" style="color:blue;">select
  all</a> &nbsp;&nbsp;<a href="javascript:DeselectAllList(document.theForm.select);" style="color:blue;">deselect
  all</a></font>
  <input type=submit>
  
</form>

php is inserting the selection into db --with each selection on a separate row. I would like each insert to be inserted in one row --in a text box in mysql. is this doable? if so, I appreciate some help.

Best
Mossa

This is because you are using "INSERT servicesrendered SET servicearea='$val'" in a while loop, so PHP gets each value individually and puts it into the DB.

To avoid this, create a string with each selection in a CSV (Comma Separated Value) format and then put that string in the DB. Later when you want the data you can easily pull it out from the DB and convert it into an array using PHP's explode command.

Here is how to do it.

$servicearea= $_POST['servicearea'];
$servicearea_str=implode(",",$servicearea);
$mysql_query="INSERT servicesrendered SET servicearea='$servicearea_str'";
$result=mysql_query($mysql_query);

This will put it into the DB in one record with commas.

This is because you are using "INSERT servicesrendered SET servicearea='$val'" in a while loop, so PHP gets each value individually and puts it into the DB.

To avoid this, create a string with each selection in a CSV (Comma Separated Value) format and then put that string in the DB. Later when you want the data you can easily pull it out from the DB and convert it into an array using PHP's explode command.

Here is how to do it.

$servicearea= $_POST['servicearea'];
$servicearea_str=implode(",",$servicearea);
$mysql_query="INSERT servicesrendered SET servicearea='$servicearea_str'";
$result=mysql_query($mysql_query);

This will put it into the DB in one record with commas.

Very interesting. I thank you for your thoughts on this. I'm giving it a try now...will advise of the outcome shortly.

Thanks again!
Mossa

I got it straighten out. A great thanks to sudeepjd.

Much oblidge!
Mossa

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.