how insert data into mysql when cannot use the ' carracter apostrophe

Mobile
feeds
Welcome Login | Register

how insert data into mysql when cannot use the ' carracter apostrophe
Reply Your Comments:
Click this button if you are interested in replying to this topic and leave your comments
Sent To Friend
CLick this button if you want to send this page to a friend.
Subsribe To Rss Feeds
Subscribe to RSS
CLick this button if you want to subscribe to this RSS Feed. You can use your browsers feeds burners if you have mozilla or internet explorer 7 or higher and keep up with updates.
  Forums Index
      » PHP Forums
        » » how insert data into mysql when cannot use the ' carracter apostrophe
how insert data into mysql when cannot use the ' carracter apostrophe
Post Description: how insert data into mysql when cannot use the carracter apostrophe PHP
Post Tags:
This Post Has Been Viewed 3957 Times Since Wed Nov 29, 2006 9:42 am Author leni with 14 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
Advertise On This Page




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:

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:
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 to user\'s

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.


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:
<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

<?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.

<?php
$sql = "INSERT INTO users SET username = '".$user_name."' ";
?>



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: ,u> 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






Leave Your Comments

Share:   Subscribe - Keep Up With This Topic
You can use this HTML code to put it on your website to show your friends this wallpaper. Use this code on your profile like myspace, friendster, Facebook or others. Just Copy and Paste it in your HTML on your websites
URL:
To embed this topic, just copy the code from the "Embed" box. Once you've copied the code, just paste it into your website or blog to embed it.
Embed:
BBCODE is use on forums. You can put this code on all your BBCODE enabled forums like PhpBB, vBulletin® and others. Just Copy and Paste this code on your Posts and Replies on your forums
Fourms BBCODE:
Links Related to : how insert data into mysql when cannot use the ' carracter apostrophe
Add Link:

Comments and replies About how insert data into mysql when cannot use the ' carracter apostrophe
:: 1 :: Reply #299 Reply By Robert McMackin On Fri Feb 09, 2007 5:46 pm
Robert McMackin:
Thank you very much. I have been looking for this
:: 2 :: Reply #3985 Reply By Eric On Mon May 21, 2007 2:30 pm
Eric:
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 :: Reply #19844 Reply By Sam On Wed Sep 12, 2007 1:24 am
Sam:
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 :: Reply #20018 Reply By Dave On Thu Sep 13, 2007 11:36 am
Dave:
where does the php code you sugested go. i have tried adding it to a few places and nothing worked.
:: 5 :: Reply #30170 Reply By Brandon On Sun Dec 02, 2007 8:28 am
Brandon:
thank you!!!!!!!!!!!!

i needed this function very badly!!!!
:: 6 :: Reply #40647 Reply By hostman On Sun Feb 10, 2008 5:30 pm
hostman:
you can check out this post its good information:

http://www.wallpaperama.com/forums/another-way-to-use-mysql-real-escape-string-instead-trimstripslashes-t5712.html

this will help you with apostrofi here are some stuff to look for: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
:: 7 :: Reply #42236 Reply By tech support On Thu Feb 21, 2008 7:21 am
tech support:
but its better to use this instead:

trim(stripslashes($_POST['somedata']))
:: 8 :: Reply #52464 Reply By Adnan Saeed On Fri May 02, 2008 12:55 pm
Adnan Saeed:
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 :: Reply #60670 Reply By Mark Kelsall On Mon Jul 07, 2008 7:43 am
Mark Kelsall:
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 :: Reply #67064 Reply By Mattias On Mon Aug 18, 2008 9:26 am
Mattias:
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 :: Reply #69112 Reply By cbo On Tue Sep 02, 2008 9:31 am
cbo:
i have this exact problem. how can i apply mysql_real_escape_string to this code?

$addition.="fname='$
:: 12 :: Reply #98715 Reply By Siva On Mon Mar 30, 2009 2:14 am
Siva:
dear leni,
i am a begineer for php with mysql.i got fedup the prblm in apostrophes.your posting is very hel for me.i want know this function use for other special character like ambersand ~.i appricate your work.thanks for your help.
kindly regards
siva
:: 13 :: Reply #100589 Reply By Kent On Fri Apr 17, 2009 5:16 am
Kent:
thanks for doing this. it saved me too.
:: 14 :: Reply #104680 Reply By Adam On Fri May 29, 2009 4:24 am
Adam:
this is exactly what i was looking for and solved my problem perfectly. thank you!!