Morning,

I have this class to enter data from a form dynamically. Its works like a dream except for one little piece. I have about 5 multiple selects on my form. It keeps insert them as the word "Array"

One of the fields is like this:

<select name="CitrixApps[]" multiple>
<option value="PHP" SELECTED>PHP</option>
<option value="Perl">Perl</option>
<option value="JavaScript">JavaScript</option>
<option value="Python">Python</option>
<option value="Ruby">Ruby</option>
</select>

This is the class to insert the records:

class dbinsert{
var $i;
var $n;
var $fld;
var $val;

function gotcha($arr, $table){
$this->i = 0;

foreach($arr as $key => $value){
if($key != 'Submit'){
$this->fld[$this->i] = $key;
$this->val[$this->i] = "\"$value\"";
}
else{
break;
}

$this->i++;

}

$this->insert($table, $this->fld, $this->val);

}

function insert($table, $fld, $val){
$query = "INSERT INTO $table (%s) VALUES (%s)";
$query = sprintf($query, implode(",", $fld), implode(",", $val));
$result = mssql_query($query) or die;
}

}

This is a print from the form submission:

CitrixApps = Perl,JavaScript,Python
query = INSERT INTO tblHolding (CitrixApps,ActionRequested) VALUES ("Array","Add New User")

The word "Array" should be "Perl,JavaScript,Python" Any thoughts how I can make this happen would be greatly appreciated.

Thanks

Laura

    When PHP receives the data from your dropdown menu, it dumps everything it finds into an array -- actually, believe it or not, you asked it to just that when you named the element "CitrixApps[]" (the '[]' instructs PHP to interpret the data as an array). When you ask for that array as a string, PHP simply gives you the string "Array", rather than trying to figure out what you want to do with it.

    In your loop, you should test to see if the value is an array or not and, if it is, do a join:

    if (is_array($value) {
       $this->val[$this->i] = '"' . join(,','$value) . '"';
    }
    else { 
       $this->val[$this->i] = "\"$value\"";
    }
    

    or for shorter code:

    $this->val[$this->i] = '"' . (is_array($value) ? join(',',$value) : $value) . '"';
    

      Thank you thank you thank you!!!

      That was exactly. Have a wonderful weekend.

      Laura

        Write a Reply...