Jump to content


Photo

using ' in mysql!!!


  • Please log in to reply
5 replies to this topic

#1 robcrozier

robcrozier
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 23 August 2006 - 02:26 PM

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:

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

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

Thanks!

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 23 August 2006 - 02:39 PM

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/m...l_escape_string) on the var that is giving you trouble (not the whole query)

#3 robcrozier

robcrozier
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 23 August 2006 - 03:00 PM

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!

#4 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 23 August 2006 - 03:02 PM

I also use
str_replace("'", "\'", "There's a tree");


#5 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 23 August 2006 - 03:03 PM

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

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 August 2006 - 03:59 PM

Use the function number_format() to format numbers. http://www.php.net/number_format

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users