Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    190
    Thanks
    50
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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

  • Users who have thanked AndrewGSW for this post:

    MaDmiX (08-08-2012)

  • #3
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    190
    Thanks
    50
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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

  • #5
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    190
    Thanks
    50
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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

  • Users who have thanked AndrewGSW for this post:

    MaDmiX (09-12-2012)

  • #7
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    190
    Thanks
    50
    Thanked 0 Times in 0 Posts
    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

  • #8
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    190
    Thanks
    50
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •