How Insert Data Into Mysql When Cannot Use The ' Carracter Apostrophe



How Insert Data Into Mysql When Cannot Use The ' Carracter Apostrophe
How Insert Data Into Mysql When Cannot Use The ' Carracter Apostrophe
Post Description:
Post Tags: how, insert, data, into, mysql, when, cannot, use, the, carracter, apostrophe
This Post Has Been Viewed 2223 Times Since Wed Nov 29, 2006 9:42 am Posted By leni with 11 replies
Next Post »» PHP Tutorials- How to Replace Change Certain Letters Strings in Forms
How Insert Data Into Mysql When Cannot Use The ' Carracter Apostrophe
I want to share this i learn here.

Last week i wrote a php script where it was a form, and once the form was submitted, it would enter the information into my mysql database, but for some reason, whenever the information data contain the apostrophe character (the apostrophe charactor is the one that's next to the enter key on your keyboard. Which is the apostrophe key? its this one: ' )

note: this asumes you are familiar with php, mysql and html.

this is an example of the data to be entered into the database when a user would submit the form with information:


     Code:
This is an example of the apostrophe not being inserted into my datase when the user's information is entered


one thing i learned is that if the information was entered with the backlash character infront of the apostrophe it would work, so my input information would look like, it would work:

If i were to try to enter this data into my database with my form, it wouldn't work. Finally, i figured out why it doesn't work. I figured out a PHP function to allow these specials characters into the database using ther INSERT or UPDATE commands.


     Code:
This is an example of the apostrophe not being inserted into my datase when the user\'s information is entered


Notice i basically changed user's[b] to user\'s[/b]

But i know i couldn't have my visitors have to enter a \ character everytime they use the apostrophe character.

I found a php function that does the trick, its called the: mysql_real_escape_string()

So to understand the mysql_real_escape_string() function, i will show you how it works in a real example:

Lets say i have a form, where i ask the user's name, once the user submits the form, his name would be entered into a database.

the name of the input variable will be called "user_name" so the HTML code would like something like this:

     Code:
<form method="post" action="">
<input type="text" name="user_name" value="wallpaperama's">
<br />
<br />
<input type="submit" name="Submit" value="Submit">
</form>


In this case, the value of user_name is equal to: wallpaperama's

now when its time for the PHP to process the variable user_name and inster into the data, i would use the mysql_real_escape_string() function to make it work, and this is how i would declare it in the php script


     Code:
<?php
$user_name = mysql_real_escape_string ($_POST['user_name']);
?>


Now that the variable $user_name has been declared with the real strings, i can insert it into the database and mysql will take it withoug any problems.

[this post sponsored by:]
post picture

mysql_real_escape_string = Escapes special characters in a string for use in a SQL statement

Description
string mysql_real_escape_string ( string unescaped_string [, resource link_identifier] )

if you want more hel on this function, you can visit the official PHP website at: http://www.php.net/manual/en/function.mysql-real-escape-string.php

Thanks and i hope it helps, i tried everywhere to find this, but no one would give me information because i didn't know how to explain it.

leni


     Quote:

apostrofi, apostrofy, apostrophy, opostrophy, opostrofy, opostrofi, apostrophey, apostrofe, apastrophy, apastrofy, apastrofi, apastrofie, opastrofy, opastrophy, opastrophe, apostophy, apostophe, aostrophe, apostropeh, spostrophe, zpostrophe, qpostrophe, aoostrophe, alostrophe, apistrophe, aplstrophe, appstrophe, apoetrophe, apoatrophe, apodtrophe, apowtrophe, aposrrophe, aposgrophe, aposyrophe, aposteophe, aposttophe, apostgophe, apostfophe, apostriphe, apostrlphe, apostrpphe, apostroohe, apostrolhe, apostropne, apostropte, apostropge, apostropbe

Leave Your Comments     [ dejar commentarios ]
  * Name     [nombre]

  * eMail (will not be published)     [coreo electronico]

* Enter Your Reply or Comments:    [commentarios]


Add Picture To Comments         [incluir foto]
YES NO             upload
Receive Replies on my Comments (An email will be sent to you when someone replies to your comments)

     

Comments and replies About How Insert Data Into Mysql When Cannot Use The ' Carracter Apostrophe




:: 1 :: #299 - Reply By Robert McMackin On Fri Feb 09, 2007 5:46 pm
Thank you very much. I have been looking for this
:: 2 :: #3985 - Reply By Eric On Mon May 21, 2007 2:30 pm
i was running into a similar problem. in php, if you have an html form and the user types in an apostrophe in a field and submit the form, php will add a backslash before the apostrophe (i.e., "al's" becomes "al\'s"). in order to get what the user typed, you need to use stripslashes(), which will remove the backslash that php inserted.
:: 3 :: #19844 - Reply By Sam On Wed Sep 12, 2007 1:24 am
after upgrading from mysql 4.x to 5.x, i spent several hours trying to figure out why i couldn't update or insert any data that included apostrophes. once i found this page, i knew why. now i can go to sleep! thanks a million.
:: 4 :: #20018 - Reply By Dave On Thu Sep 13, 2007 11:36 am
where does the php code you sugested go. i have tried adding it to a few places and nothing worked.
:: 5 :: #30170 - Reply By Brandon On Sun Dec 02, 2007 8:28 am
thank you!!!!!!!!!!!!

i needed this function very badly!!!!
:: 6 :: #40647 - Reply By hostman On Sun Feb 10, 2008 5:30 pm
:: 7 :: #42236 - Reply By tech support On Thu Feb 21, 2008 7:21 am
but its better to use this instead:

trim(stripslashes($_POST['somedata']))
:: 8 :: #52464 - Reply By Adnan Saeed On Fri May 02, 2008 12:55 pm
thank you very much, it working fine, i used it in the following way

$info = mysql_real_escape_string($_post['i mytable set info='$info' where id=1");

i can now store or update text with apostrophes into mysql database.

:: 9 :: #60670 - Reply By Mark Kelsall On Mon Jul 07, 2008 7:43 am
mate thank you very much!!! that made my life so much easier than the way i was trying to make it work! cheers. just need to strip them out now(walks away sifting through internet pages)
:: 10 :: #67064 - Reply By Mattias On Mon Aug 18, 2008 9:26 am
i have a small database for home use and i've had this problem for years with apostrophes that wasn't accepted. finally i've had it solved after reading what you wrote!
:: 11 :: #69112 - Reply By cbo On Tue Sep 02, 2008 9:31 am
i have this exact problem. how can i apply mysql_real_escape_string to this code?

$addition.="fname='$