Jump to content

Array issue


bob_the _builder

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
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.