inserting special chars in mysql

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
leebo
Forum Commoner
Posts: 44
Joined: Sun Oct 20, 2002 9:49 am

inserting special chars in mysql

Post by leebo »

How do i enter the following text into the database ?

date of meeting = '4/4/2003'

i have tried

$text="date of meeting = '4/4/2003'";

$result=MYSQL_QUERY( "update $table SET text='$text') where user='$user' ");

but it doesn`t add it - any ideas ?

thanks
Random
Forum Commoner
Posts: 30
Joined: Wed Mar 12, 2003 5:38 pm

Post by Random »

are you trying to make it a new table or just add it into the database?

if youre just adding it into the database, it should look like this:

Code: Select all

<?
$sql = "INSERT INTO `table name` (column name, column name) values ("value","value","value")";
?>
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

mysql_escape_string() should do it. also check addslashes() out
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Code: Select all

<?php
$sql = "INSERT INTO `$table` (`text`) VALUES ('" . $addslashes($text) . "' WHERE `user` = '$user')";
mysql_query($sql) or die(mysql_error()."<p>$sql");
?>
Image Image
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

stripslashes

Post by phpScott »

just remeber to do a striplashes() on the value when you pull it out of the db or you will not get the correct output and you will end up with a case of the nasty backslashes.
each backslash will be escaped as well as the orginal character that it was escaping.

bob's place
bob''s place
bob\\''s place

you get the idea and doing a stripslashes will only remove the escaping \ so you would end up with

bob''s place if you stripslashed just the last line.

avoid the headache now and remeber the stripslashes() function

learned from a really bad experience.

phpScott
leebo
Forum Commoner
Posts: 44
Joined: Sun Oct 20, 2002 9:49 am

Post by leebo »

Nope that didn`t work

I can get $text='date of meeting = "4/4/2003" ' ; and it will add the " but not the other way around and i need it with the ' char - I can also get it to work with back slashes but when you try to edit the record in phpadmin it throws up an error.
Post Reply