Jump to content

Archived

This topic is now archived and is closed to further replies.

robcrozier

using ' in mysql!!!

Recommended Posts

hi, im trying to input some text into a mysql text field using php.  Though everytime i use a ' in my text i get an error.  Here is the code that i'm using to insert the data into the table:

[code]
$query = "update stock
set title = '$t', description = '$d', price = '$p'
where id = '$i'";
[/code]

How can i tweak this to allow me to input inverted commas ( ' ) in the description and title fields?

Thanks!

Share this post


Link to post
Share on other sites
They are single quotes.  You have two options.  Use addslashes (http://www.php.net/addslashes) or use mysql_real_escape_string (http://www.php.net/mysql_real_escape_string) on the var that is giving you trouble (not the whole query)

Share this post


Link to post
Share on other sites
thanks mate thats done the trick!!!  ;D

However, i now have one more question:
  - Can you use a similar function to get mysql to accept commas and print them out when used in an INT field? At the min whenever i enter one, all of the data that follows it is not displayed.

thanks again!

Share this post


Link to post
Share on other sites
I also use [code=php:0] str_replace("'", "\'", "There's a tree"); [/code]

Share this post


Link to post
Share on other sites
INT field is an integer 

1493 is an INT
1,493 is technically a string

You would have to format the output to display the comma for you.  Unless of course there is a particular function that I am unaware of to format numbers like that for you. I know there is one for formating decimals....

Share this post


Link to post
Share on other sites
Use the function number_format() to format numbers. http://www.php.net/number_format

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.