bob_the _builder Posted September 20, 2006 Share Posted September 20, 2006 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 ArrayArrThats the out put from 3 fields that did have a sentance or 2 in themWhat would cause this? as it works fine just posting a non array single field.Thanks Quote Link to comment https://forums.phpfreaks.com/topic/21375-array-issue/ Share on other sites More sharing options...
HuggieBear Posted September 20, 2006 Share Posted September 20, 2006 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]<?phpfunction 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]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/21375-array-issue/#findComment-95158 Share on other sites More sharing options...
bob_the _builder Posted September 20, 2006 Author Share Posted September 20, 2006 Hi,Thanks that makes a diference, but now its not converting the html to the bbcode ..?Cheers Quote Link to comment https://forums.phpfreaks.com/topic/21375-array-issue/#findComment-95296 Share on other sites More sharing options...
HuggieBear Posted September 20, 2006 Share Posted September 20, 2006 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]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/21375-array-issue/#findComment-95366 Share on other sites More sharing options...
bob_the _builder Posted September 20, 2006 Author Share Posted September 20, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/21375-array-issue/#findComment-95681 Share on other sites More sharing options...
HuggieBear Posted September 20, 2006 Share Posted September 20, 2006 It looks good to me Bob ;DRegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/21375-array-issue/#findComment-95707 Share on other sites More sharing options...
bob_the _builder Posted September 21, 2006 Author Share Posted September 21, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/21375-array-issue/#findComment-95804 Share on other sites More sharing options...
ober Posted September 21, 2006 Share Posted September 21, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/21375-array-issue/#findComment-95808 Share on other sites More sharing options...
bob_the _builder Posted September 21, 2006 Author Share Posted September 21, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/21375-array-issue/#findComment-95864 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.