Page 1 of 1

inserting special chars in mysql

Posted: Mon Apr 07, 2003 4:17 pm
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

Posted: Mon Apr 07, 2003 5:01 pm
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")";
?>

Posted: Mon Apr 07, 2003 5:31 pm
by daven
mysql_escape_string() should do it. also check addslashes() out

Posted: Mon Apr 07, 2003 6:04 pm
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");
?>

stripslashes

Posted: Mon Apr 07, 2003 7:41 pm
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

Posted: Tue Apr 08, 2003 12:55 am
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.