Jump to content


Photo

Array issue


  • Please log in to reply
8 replies to this topic

#1 bob_the _builder

bob_the _builder
  • Members
  • PipPipPip
  • Advanced Member
  • 207 posts

Posted 20 September 2006 - 07:53 AM

Hi,

With the following function:

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; 
}


Using a form:

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>';


When I submit it to the insert page using:


$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]."')" );


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

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 20 September 2006 - 08:33 AM

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.

<?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;
}
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 bob_the _builder

bob_the _builder
  • Members
  • PipPipPip
  • Advanced Member
  • 207 posts

Posted 20 September 2006 - 01:33 PM

Hi,

Thanks that makes a diference, but now its not converting the html to the bbcode ..?


Cheers

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 20 September 2006 - 02:41 PM

Sorry,

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

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

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 bob_the _builder

bob_the _builder
  • Members
  • PipPipPip
  • Advanced Member
  • 207 posts

Posted 20 September 2006 - 09:17 PM

Perfect,

Thanks alot.

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

if (!get_magic_quotes_gpc()) {
	$value[$k] = mysql_real_escape_string($v);
}


Thanks

#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 20 September 2006 - 10:06 PM

It looks good to me Bob  ;D

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#7 bob_the _builder

bob_the _builder
  • Members
  • PipPipPip
  • Advanced Member
  • 207 posts

Posted 21 September 2006 - 01:26 AM

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:

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


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

#8 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 21 September 2006 - 01:29 AM

You could use a simple if/else and this function: http://us3.php.net/m...on.is-array.php to determine if it is an array or not and how you should process it.

Info: PHP Manual


#9 bob_the _builder

bob_the _builder
  • Members
  • PipPipPip
  • Advanced Member
  • 207 posts

Posted 21 September 2006 - 05:20 AM

Hi,

Thanks, I got:

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)); 
}

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

Thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users