[SOLVED] Word Array instead of the data is being inserted

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lmayer
Forum Newbie
Posts: 3
Joined: Thu Feb 24, 2005 6:40 am

[SOLVED] Word Array instead of the data is being inserted

Post by lmayer »

Morning,

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

One of the fields is like this:

Code: Select all

<select name="CitrixApps&#1111;]" 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:

Code: Select all

class dbinsert&#123;
var $i;
var $n;
var $fld;
var $val;

function gotcha($arr, $table)&#123;
$this->i = 0;

foreach($arr as $key => $value)&#123;
if($key != 'Submit')&#123;
$this->fld&#1111;$this->i] = $key;
$this->val&#1111;$this->i] = ""$value"";
&#125;
else&#123;
break;
&#125;

$this->i++;

&#125;

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

&#125;


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

&#125;
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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

when you stored $value as "\"$value\"" you sealed it into 'Array', as you asked the engine to store an array as a string.

you should do a type check when $value comes in to see if it is an array (is_array()) and implode it when needed.
lmayer
Forum Newbie
Posts: 3
Joined: Thu Feb 24, 2005 6:40 am

Post by lmayer »

Makes large amounts of sense. So create an if else statement...Thanks for pointing me in the right direction.

Laura
lmayer
Forum Newbie
Posts: 3
Joined: Thu Feb 24, 2005 6:40 am

Post by lmayer »

Got it!

exchange:$this->val[$this->i] = "\"$value\"";

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

Works like a dream.

Laura
Post Reply