Results 1 to 7 of 7

Thread: Posting ' in forms

  1. #1
    Join Date
    Feb 2005
    Posts
    153
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Posting ' in forms

    Im having trouble posting ' (commers) within my php forms that send data to a database.
    Can anyone help with this?

  2. #2
    Join Date
    Dec 2005
    Posts
    193
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    prefix the apostrophe (') with a backslash, like so: \'
    Schofieldandwhite.com: RFH reseller

  3. #3
    Join Date
    Feb 2004
    Posts
    4,901
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Quote Originally Posted by GarethMoore1979
    Im having trouble posting ' (commers) within my php forms that send data to a database.
    Can anyone help with this?
    This is known as SQL injection, and it VERY bad.

    For example on a login screen, if you do not protect against SQL injection...

    Username: anything
    Password: anything' OR 1=1--

    Code:
    PHP Code:
    $sql "SELECT * FROM tblLogin WHERE username = '" $_POST["username"] . "' AND password = '" $_POST["password"] . "'; 
    The SQL query is turned into:

    SELECT * FROM tblLogin WHERE username = 'anything' AND password = 'anything' OR 1=1--'

    The -- is standard SQL for "ignore rest of query", therefore the last ' is ignored, and as you can see the resulting query will always bring back a result and therefore you have a hole in your login system.

    A similar sort of thing applies to UPDATE queries, like you are having problems with.

    More info:
    http://en.wikibooks.org/wiki/Program...:SQL_Injection
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  4. #4
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 24 Times in 24 Posts

    Default

    Quote Originally Posted by GarethMoore1979
    Im having trouble posting ' (commers) within my php forms that send data to a database.
    Can anyone help with this?
    you need to make sure any user input data is appropriately escaped, so that it cannot break your sql.
    Assuming you are using MySQL, the easiest way in php is just to call mysql_real_escape_string() on any user input you are passing to the db.
    check the PHP manual for more information on it.

  5. #5
    Join Date
    Feb 2005
    Posts
    153
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    Code:
    //-----INSERT THE TOPIC INTO DATABASE-----
    		$Query1 = "INSERT INTO forum_topics (FORUM_ID, T_SUBJECT, T_MESSAGE, T_AUTHOR, T_LAST_POST, T_DATE, T_IP, T_LAST_POST_AUTHOR)
    		VALUES ('$ForumID','$subject','$postedValue','{$_COOKIE['MEMBER_ID']}','$timestamp','$timestamp','$ip','{$_COOKIE['MEMBER_ID']}')";
    		$result = mysql_query ($Query1);
    		$NewTopicID = mysql_insert_id();
    this is my php code that will insert the contents of my form into the database.
    So what do I do to this so that it will escape anyone typing a comma in?

  6. #6
    Join Date
    Feb 2005
    Posts
    153
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    actually ive done it!
    cheers guys!

  7. #7
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 24 Times in 24 Posts

    Default

    Quote Originally Posted by GarethMoore1979
    actually ive done it!
    cheers guys!
    Nice one! I might as well post this anyway... I have just taken the quote_smart function from the php manual, so assuming $ForumID, Member_id and timestamp are integer:
    PHP Code:
    function quote_smart($value)
    {
       
    // Stripslashes
       
    if (get_magic_quotes_gpc()) {
           
    $value stripslashes($value);
       }
       
    // Quote if not integer
       
    if (!is_numeric($value)) {
           
    $value "'" mysql_real_escape_string($value) . "'";
       }
       return 
    $value;
    }

    //-----INSERT THE TOPIC INTO DATABASE-----
            
    $Query1 sprintf("
                INSERT
                    INTO forum_topics
                       (FORUM_ID,        T_SUBJECT,    T_MESSAGE,    T_AUTHOR,
                        T_LAST_POST,    T_DATE,        T_IP,        T_LAST_POST_AUTHOR)
                    VALUES
                        (%d,%s,%s,%d,%d,%d,%s,%d);"
    ,
                        
    $ForumID,//FORUM_ID
                        
    quote_smart($subject),//T_SUBJECT
                        
    quote_smart($postedValue),//T_MESSAGE
                        
    $_COOKIE['MEMBER_ID'],//T_AUTHOR
                        
    $timestamp,//T_LAST_POST
                        
    $timestamp,//T_DATE
                        
    quote_smart($ip),//T_IP
                        
    $_COOKIE['MEMBER_ID']//T_LAST_POST_AUTHOR
                        
    );
                                
            
    $result mysql_query ($Query1);
            
    $NewTopicID mysql_insert_id(); 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. vb.net v2 Windows Forms ListView
    By EdD in forum ASP.NET
    Replies: 0
    Last Post: 6th December 2005, 07:50 PM

Bookmarks

Posting Permissions

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