Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 SOLVED: INSERT mysql statement with apostrophe issues.
Index -> Programming, PHP -> PHP Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
thegadgetman




PostPosted: Wed Dec 15, 2010 2:00 pm   Post subject: SOLVED: INSERT mysql statement with apostrophe issues.

Hey thanks for reading and or helping.

My question is I am trying to add some information to a database that has an apostrophe in the name. From what I found out is you need to use \' to properly have it inserted but it is not inserting it to the database. Any file name without special characters works.

Here is my code...

code:

// Add each new media into the $mediaTableNew database
for($index=0; $index < count($mediaCheck); $index++)
        {
           print ($mediaCheck[$index]);
           mysql_query("INSERT INTO $mediaTableNew($mediaName, $mediaDate) values(".str_replace("'", "\'", $mediaCheck[$index]).", CURDATE())");
        }


SOLVED: for those who care this is the correct way to do it.

code:

        // Add each new media into the $mediaTableNew database
        for($index=0; $index < count($mediaCheck); $index++)
        {
                $mediaCheck[$index] = str_replace("'","\'", $mediaCheck[$index]);
                print ($mediaCheck[$index].'<br />');
                mysql_query("INSERT INTO $mediaTableNew($mediaName, $mediaDate) values('$mediaCheck[$index]', CURDATE())");
        }
Sponsor
Sponsor
Sponsor
sponsor
jeffgreco13




PostPosted: Tue Mar 22, 2011 2:43 pm   Post subject: Re: SOLVED: INSERT mysql statement with apostrophe issues.

im sorry this is a really old post guys but its been a while since ive been active.
anyone searching the forums for an answer to this will get this solution and there's a much better way to accomplish this:

php:
for($index=0; $index < count($mediaCheck); $index++)
        {
                //NOT NEEDED$mediaCheck[$index] = str_replace("'","\'", $mediaCheck[$index]);
                print ($mediaCheck[$index].'<br />');
                mysql_query("INSERT INTO $mediaTableNew($mediaName, $mediaDate) values('[b]mysql_real_escape_string($mediaCheck[$index])[/b]', CURDATE())");
        }


The reason why this is superior is because unless you can GUARANTEE the variable's contents, ie) doesn't come from an html form in any way, then this function actually prepends the escaping '\' to multiple unfriendly characters.
http://php.net/manual/en/function.mysql-real-escape-string.php
thegadgetman




PostPosted: Fri Jul 22, 2011 10:09 pm   Post subject: Re: SOLVED: INSERT mysql statement with apostrophe issues.

@jeffgreco13 Thanks! I will use this way instead from what I read it does make more sense.
Display posts from previous:   
   Index -> Programming, PHP -> PHP Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: