Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-08-2012, 06:28 PM   PM User | #1
MaDmiX
Regular Coder

 
Join Date: Feb 2012
Location: Charlotte, NC
Posts: 104
Thanks: 26
Thanked 0 Times in 0 Posts
MaDmiX is an unknown quantity at this point
Using mysql_prep() with an array

Hi All,

I am trying to use the mysql_prep() function on a form processing script as follows:
PHP Code:
$SegmentNotes mysql_prep($_POST['SegmentNotes[]']); 
The data is not being written to my database. If I remove the mysql_prep() function the data writes just fine but since it is a text field, I really need to use the function. If I write as follows:
PHP Code:
$SegmentNotes mysql_prep($_POST['SegmentNotes']); 
I get an error that mysql_prep() was expecting a string. Is there a way (or an alternate function) to use mysql_prep() on an array?

Thanks,

Ken
MaDmiX is offline   Reply With Quote
Old 08-08-2012, 07:10 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
mysql_prep is not a standard function. Examples that I've seen take a single string argument and clean this text for inclusion in a sql statement, not an array.

If you have such a function then I suppose you could do:

PHP Code:
$CleanNotes array_map('mysql_prep'$_POST['SegmentNotes']); 
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
MaDmiX (08-08-2012)
Old 08-08-2012, 08:54 PM   PM User | #3
MaDmiX
Regular Coder

 
Join Date: Feb 2012
Location: Charlotte, NC
Posts: 104
Thanks: 26
Thanked 0 Times in 0 Posts
MaDmiX is an unknown quantity at this point
Hi AndrewGSW,

I had forgotton that I got that function from an online PHP course :-)

Here is the code:
PHP Code:
    function mysql_prep$value ) {
        
$magic_quotes_active get_magic_quotes_gpc();
        
$new_enough_php function_exists"mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
        
if( $new_enough_php ) { // PHP v4.3.0 or higher
            // undo any magic quote effects so mysql_real_escape_string can do the work
            
if( $magic_quotes_active ) { $value stripslashes$value ); }
            
$value mysql_real_escape_string$value );
        } else { 
// before PHP v4.3.0
            // if magic quotes aren't already on then add slashes manually
            
if( !$magic_quotes_active ) { $value addslashes$value ); }
            
// if magic quotes are active, then the slashes already exist
        
}
        return 
$value;
    } 
I will see if I can create a function based on your suggestion that will handle arrays. Thanks for your help!

Kind regards,

Ken
MaDmiX is offline   Reply With Quote
Old 08-08-2012, 09:02 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Why not use 'array_map' which will feed all your POST/SegmentNotes data into the mysql_prep function, returning an array as the result?
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 08-09-2012, 04:18 PM   PM User | #5
MaDmiX
Regular Coder

 
Join Date: Feb 2012
Location: Charlotte, NC
Posts: 104
Thanks: 26
Thanked 0 Times in 0 Posts
MaDmiX is an unknown quantity at this point
Yes that would be the best approach. That's what you had suggested originally, right? I wasn't sure about using that approach because i though that the mysql_prep() function would still want a string and would bomb when fed the $_POST['SegmentNotes'] array. I will give it a try, though.

Last edited by MaDmiX; 08-09-2012 at 04:20 PM..
MaDmiX is offline   Reply With Quote
Old 08-09-2012, 05:35 PM   PM User | #6
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Code:
$CleanNotes = array_map('mysql_prep', $_POST['SegmentNotes']);
Will feed each of the SegmentNotes elements (strings) into the function mysql_prep() one by one, returning all the results into the new array CleanNotes.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
MaDmiX (09-12-2012)
Old 08-11-2012, 12:36 AM   PM User | #7
MaDmiX
Regular Coder

 
Join Date: Feb 2012
Location: Charlotte, NC
Posts: 104
Thanks: 26
Thanked 0 Times in 0 Posts
MaDmiX is an unknown quantity at this point
Quote:
Originally Posted by AndrewGSW View Post
Code:
$CleanNotes = array_map('mysql_prep', $_POST['SegmentNotes']);
Will feed each of the SegmentNotes elements (strings) into the function mysql_prep() one by one, returning all the results into the new array CleanNotes.
I haven't been able to work on this project for a while but array_map() is exactly what I need. I'll post back when I have it working. Thanks for your help.

Kind regards,

Ken
MaDmiX is offline   Reply With Quote
Old 09-12-2012, 04:51 PM   PM User | #8
MaDmiX
Regular Coder

 
Join Date: Feb 2012
Location: Charlotte, NC
Posts: 104
Thanks: 26
Thanked 0 Times in 0 Posts
MaDmiX is an unknown quantity at this point
Quote:
Originally Posted by MaDmiX View Post
I haven't been able to work on this project for a while but array_map() is exactly what I need. I'll post back when I have it working. Thanks for your help.

Kind regards,

Ken
Just got round to doing this lol. It works fine and I just wanted to say thanks.

Ken
MaDmiX is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:37 PM.


Advertisement
Log in to turn off these ads.