Jump to content

Storing Multiple Select values in MySQL


alan_k

Recommended Posts

I have a web page I'm building that at one point has a drop down list that the user can select multiple choices. I have looked online and seen several examples of this but in trying none have succeeded in storing them in a mysql key that I put in a table. I use the select tag to start the array and then use _POST method to store array in variable. The two files are as follows:Gather info in a form with this: <

li class="form-line" data-type="control_dropdown" id="id_12">            <label class="form-label form-label-left form-label-auto" id="label_12" for="input_12"> Please Specify One or More Periods When the Classroom is Available if Applicable </label>            <div id="cid_12" class="form-input">              <select class="form-dropdown" style="width:150px" id="input_12" name="q12_pleaseSpecify12[]" multiple="multiple">                <option value="">  </option>                <option selected="selected" value="Period 1"> Period 1 </option>                <option value="Period 2"> Period 2 </option>                <option value="HR"> HR </option>                <option value="Period 3"> Period 3 </option>                <option value="Period 4"> Period 4 </option>                <option value="1st 5th (11:50-12:39)"> 1st 5th (11:50-12:39) </option>                <option value="2nd 5th (12:19-1:07)"> 2nd 5th (12:19-1:07) </option>                <option value="Period 6th"> Period 6th </option>                <option value="Period 7th"> Period 7th </option>              </select>            </div>          </li>

and then _POST with this in second page Insert.php:

    $FName=$_POST['FName_val'];    $LName=$_POST['LName_val'];    $Dept=$_POST['q4_department'];    $Rm_Number=$_POST['q5_roomNumber'];    $Prob_Cat=$_POST['q11_problemCatregory'];    $Rm_Avail=implode(',', $_POST['q12_pleaseSpecify12']);    $Problem=$_POST['q8_explainProblem'];    $Tkt_Status=$_POST['Tkt_Status_val'];
        print_r ($Rm_Avail);

Then Insert it with:

   $query = "INSERT INTO TT_Form VALUES('', '$FName','$LName',    '$Dept','$Rm_Number','$Prob_Cat', '$Rm_Avail', '$Problem', '$Tkt_Status', '$TT')";

I am getting no output from the print_r statement. Any suggestions welcome.

Link to comment
Share on other sites

I'm sorry when I change to:

$FName=$_POST['FName_val'];$LName=$_POST['LName_val'];$Dept=$_POST['q4_department'];$Rm_Number=$_POST['q5_roomNumber'];$Prob_Cat=$_POST['q11_problemCatregory'];$Rm_Avail=$_POST['q12_pleaseSpecify12'];$Problem=$_POST['q8_explainProblem'];$Tkt_Status=$_POST['Tkt_Status_val'];print_r ($_POST);

Iget on Insert.php:

Array ( [formID] => 42796558181164 [FName_val] => aa [LName_val] => aassaaa [q4_department] => sss [q5_roomNumber] => [q6_email6] => [q12_pleaseSpecify12] => Period 6th [q8_explainProblem] => [Tkt_Status_val] => Open )

 

As you can see I'm only getting last selected value.

Edited by alan_k
Link to comment
Share on other sites

I tested with and without square brackets. The square brackets seem to be required to receive an array on the server.

 

It's working fine for me.

 

This is my <select> element:

<select class="form-dropdown" style="width:150px" id="input_12" name="q12_pleaseSpecify12[]" multiple="multiple">

This is what I have on the server

print_r($_POST['q12_pleaseSpecify12']);
Link to comment
Share on other sites

Yes you are right. Testing it on my development server I get:

Array ( [formID] => 42796558181164 [FName_val] => aaa [LName_val] => asaa [q4_department] => Math [q5_roomNumber] => 223 [q6_email6] => [q11_problemCatregory] => Software [q12_pleaseSpecify12] => Array ( [0] => Period 2 [1] => HR [2] => Period 4 ) [q8_explainProblem] => [Tkt_Status_val] => Open )

But now I have the problem of the table is printing out the word Array in the cell where I want the periods a classroom are available should be. Looking in MySQL I see that is the value stored in Rm_Avail.My insert looks like:

try    {    $link=new PDO($dsn, $username,$password);    }catch (PDOException $e)    {    $error_message=$e->getMessage();    echo "<h1>Resource Unavailable. Please Contact the System Administrator</h1>";    }$query = "INSERT INTO TT_Form VALUES('', '$FName','$LName','$Dept','$Rm_Number','$Prob_Cat', '$Rm_Avail', '$Problem', '$Tkt_Status', '$TT')";$result = $link->query($query);

I'm guessing I have to now do something with implode which I tried earlier but did not work. Any suggestions on how to store the multiple values in mysql field much appreciated.

Edited by alan_k
Link to comment
Share on other sites

Exactly what I was doing. I used

<?phpinclude 'dbinfo.inc.php';$FName=$_POST['FName_val'];$LName=$_POST['LName_val'];$Dept=$_POST['q4_department'];$Rm_Number=$_POST['q5_roomNumber'];$Prob_Cat=$_POST['q11_problemCatregory'];$Rm_Avail=$_POST['q12_pleaseSpecify12'];$Problem=$_POST['q8_explainProblem'];$Tkt_Status=$_POST['Tkt_Status_val'];print "$Rm_Avail";print_r ($_POST);try    {    $link=new PDO($dsn, $username,$password);    }catch (PDOException $e)    {    $error_message=$e->getMessage();    echo "<h1>Resource Unavailable. Please Contact the System Administrator</h1>";    }$query = "INSERT INTO TT_Form VALUES('', '$FName','$LName','$Dept','$Rm_Number','$Prob_Cat', '$Rm_Avail', '$Problem', '$Tkt_Status', '$TT')";$result = $link->query($query);

But nothing prints out for $Rm_Avail on my insert.php page. Do I have to some how convert the $_POST['q12_pleaseSpecify12'] into a string so $Rm_Avail can handle it by using that implode function or something similar?

Link to comment
Share on other sites

Got it. Used this code

<?phpinclude 'dbinfo.inc.php';$FName=$_POST['FName_val'];$LName=$_POST['LName_val'];$Dept=$_POST['q4_department'];$Rm_Number=$_POST['q5_roomNumber'];$Prob_Cat=$_POST['q11_problemCatregory'];$Rm_Avail=implode(",",$_POST['q12_pleaseSpecify12']);$Problem=$_POST['q8_explainProblem'];$Tkt_Status=$_POST['Tkt_Status_val'];print "$Rm_Avail";print_r ($_POST);try    {    $link=new PDO($dsn, $username,$password);    }catch (PDOException $e)    {    $error_message=$e->getMessage();    echo "<h1>Resource Unavailable. Please Contact the System Administrator</h1>";    }$query = "INSERT INTO TT_Form VALUES('', '$FName','$LName','$Dept','$Rm_Number','$Prob_Cat', '$Rm_Avail', '$Problem', '$Tkt_Status', '$TT')";$result = $link->query($query);

And now works ok. Thanks for everyone's input

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...