Jump to content

Archived

This topic is now archived and is closed to further replies.

bob_the _builder

Array issue

Recommended Posts

Hi,

With the following function:

[code=php:0]function ValidateInput($value) {

$BBCode = array(
"<b>" => "[b]",
"</b>" => "[/b]",
"<u>" => "[u]",
"</u>" => "[/u]",
);
$value = str_replace(array_keys($BBCode), array_values($BBCode), $value);
$value = mysql_real_escape_string(trim(strip_tags($value)));
return $value;
}[/code]


Using a form:

[code=php:0] echo '<center>';

$number_of_fields = 4;

echo '<form enctype="multipart/form-data" action="index.php?action=upload" method="post" name="upload_form">';

  while($counter <= $number_of_fields){
  echo '<input name="photo_filename[]" type="file" class="input-box"><br />';
echo '<textarea name="photo_caption[]" cols="26" rows="3" class="input-box"></textarea><br /><br />';
  $counter++;
}

echo '<input type="submit" name="submit" value="Upload Photos" class="submit-button">';
echo '</form>';

echo '</center>';[/code]


When I submit it to the insert page using:


[code=php:0]$photo_caption = ValidateInput($_POST['photo_caption']);


mysql_query("INSERT INTO gallery_images(subcat_id, photo_filename, photo_caption) VALUES('".$subcat_id."', '0', '".$photo_caption[$counter]."')" );[/code]


Basically for each field it displays 1 letter starting to write Array

A
r
r

Thats the out put from 3 fields that did have a sentance or 2 in them

What would cause this? as it works fine just posting a non array single field.


Thanks

Share this post


Link to post
Share on other sites
If you're passing an array into ValidateInput, which it looks as though you are.  You probably need to carry out your actions on each element of the array, not the whole array.

I'd have thought you're in need of a loop somewhere.  Don't hold me to this, but maybe something like this inside the function.

[code]
<?php
function ValidateInput($value) {
$BBCode = array(
"<b>" => "[b]",
"</b>" => "[/b]",
"<u>" => "[u]",
"</u>" => "[/u]",
);

foreach ($value as $k => $v){
$value[$k] = str_replace(array_keys($BBCode), array_values($BBCode), $v);
$value[$k] = mysql_real_escape_string(trim(strip_tags($v)));
}
return $value;
}
?>[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
Sorry,

The str_replace code should have been left out of the loop.

[code]$value = str_replace(array_keys($BBCode), array_values($BBCode), $value);
foreach (...){
  // rest of loop
)[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
Perfect,

Thanks alot.

Is this the correct way to go about magic quotes for adding slashes?

[code=php:0]if (!get_magic_quotes_gpc()) {
$value[$k] = mysql_real_escape_string($v);
}[/code]


Thanks

Share this post


Link to post
Share on other sites
Hi,

I notice that if I use the function on a single text field, I get an error I guess because its not an array been called into it.

Get the error on line:

[code=php:0]foreach ($value as $k => $v){[/code]


Is there a work around so I can use the same function on all variables that are from an array or just a single variable?


Thanks

Share this post


Link to post
Share on other sites
You could use a simple if/else and this function: http://us3.php.net/manual/en/function.is-array.php to determine if it is an array or not and how you should process it.

Share this post


Link to post
Share on other sites
Hi,

Thanks, I got:

[code=php:0]if (is_array($value)) {

foreach ($value as $k => $v){

if (!get_magic_quotes_gpc()) {
$value[$k] = mysql_real_escape_string($v); }
$value[$k] = trim(strip_tags($v));
}
}else{
if (!get_magic_quotes_gpc()) {
$value = mysql_real_escape_string($value);
}
$value = trim(strip_tags($value));
}[/code]

Im sure I can loose the if statment for get magic quotes so its only used once.

Thanks

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.