Page 1 of 1

inserting data into DB problem

Posted: Thu Sep 08, 2005 7:39 pm
by Think Pink
hello, i found a bit of code, something like bbcode and modified it a little to what i need.
the problem is that I have no ideea (and i tried a few) howw to assign the function to a variable and insert it into db.

here is a piece.

Code: Select all

function replaceMessage($post) {
   $post    = str_replace ("[u]", "<u>", "$post");
   $post    = str_replace ("[/u]", "</u>", "$post");
   $post    = str_replace ("[i]", "<i>", "$post");
   $post    = str_replace ("[/i]", "</i>", "$post");
   $post    = str_replace ("[b]", "<b>", "$post");
   $post    = str_replace ("[/b]", "</b>", "$post");
   $post    = str_replace ("[size=1]", "<font size='1'>", "$post");
   $post    = str_replace ("[size=2]", "<font size='2'>", "$post");
   $post    = str_replace ("[size=3]", "<font size='3'>", "$post");
   $post    = str_replace ("[size=5]", "<font size='5'>", "$post");
   $post    = str_replace ("[size=7]", "<font size='7'>", "$post");
   $post    = str_replace ("[/size]", "</font>", "$post");
   $post    = str_replace ("[color=maroon]", "<font color='maroon'>", "$post");
   $post    = str_replace ("[color=red]", "<font color='red'>", "$post");
   $post    = str_replace ("[color=navy]", "<font color='navy'>", "$post");
   $post    = str_replace ("[color=blue]", "<font color='blue'>", "$post");
   $post    = str_replace ("[color=teal]", "<font color='teal'>", "$post");
   $post    = str_replace ("[color=green]", "<font color='green'>", "$post");
   $post    = str_replace ("[color=olive]", "<font color='olive'>", "$post");
   $post    = str_replace ("[color=white]", "<font color='white'>", "$post");
   $post    = str_replace ("[color=silver]", "<font color='silver'>", "$post");
   $post    = str_replace ("[color=gray]", "<font color='gray'>", "$post");
   $post    = str_replace ("[color=black]", "<font color='black'>", "$post");
   $post    = str_replace ("[/color]", "</font>", "$post");

   return $post;
}
echo replaceMessage($post);
}
this displayes ok,but how do i enter the data into db?

Code: Select all

$sql = mysql_query("INSERT INTO table(field) VALUES('".$what_comes_here?."')");
thx

Posted: Thu Sep 08, 2005 10:01 pm
by anjanesh

Code: Select all

$sql = mysql_query("INSERT INTO table(field) VALUES('".replaceMessage($post)."')");

Posted: Thu Sep 08, 2005 11:33 pm
by feyd
pssst, remember to escape the string returned so MySQL can work with it! ;)

inserting data into DB problem

Posted: Fri Sep 09, 2005 3:41 am
by Think Pink
well, i tried that and other few forms of it, and nothing happendes.
some more ideeas?

Posted: Fri Sep 09, 2005 4:28 am
by raghavan20

Code: Select all

function replaceMessage($post) { 
   $post    = str_replace ("[u]", "<u>", "$post"); 
   $post    = str_replace ("[/u]", "</u>", "$post"); 
   $post    = str_replace ("[i]", "<i>", "$post"); 
   $post    = str_replace ("[/i]", "</i>", "$post"); 
   $post    = str_replace ("[b]", "<b>", "$post"); 
   $post    = str_replace ("[/b]", "</b>", "$post"); 
   $post    = str_replace ("[size=1]", "<font size='1'>", "$post"); 
   $post    = str_replace ("[size=2]", "<font size='2'>", "$post"); 
   $post    = str_replace ("[size=3]", "<font size='3'>", "$post"); 
   $post    = str_replace ("[size=5]", "<font size='5'>", "$post"); 
   $post    = str_replace ("[size=7]", "<font size='7'>", "$post"); 
   $post    = str_replace ("[/size]", "</font>", "$post"); 
   $post    = str_replace ("[color=maroon]", "<font color='maroon'>", "$post"); 
   $post    = str_replace ("[color=red]", "<font color='red'>", "$post"); 
   $post    = str_replace ("[color=navy]", "<font color='navy'>", "$post"); 
   $post    = str_replace ("[color=blue]", "<font color='blue'>", "$post"); 
   $post    = str_replace ("[color=teal]", "<font color='teal'>", "$post"); 
   $post    = str_replace ("[color=green]", "<font color='green'>", "$post"); 
   $post    = str_replace ("[color=olive]", "<font color='olive'>", "$post"); 
   $post    = str_replace ("[color=white]", "<font color='white'>", "$post"); 
   $post    = str_replace ("[color=silver]", "<font color='silver'>", "$post"); 
   $post    = str_replace ("[color=gray]", "<font color='gray'>", "$post"); 
   $post    = str_replace ("[color=black]", "<font color='black'>", "$post"); 
   $post    = str_replace ("[/color]", "</font>", "$post"); 

   return $post; 
} 
echo replaceMessage($post); //<------------------------------------what is this line??
} //<------------------------------------------------------------------------what is this another line??

first show us the string in the $post. echo it and show the returned value.
i dont know whether you escaped the whole $post as feyd said. use mysql_real_escape_string() to escape.
also if possible, echo the insert query and post it here.

Posted: Fri Sep 09, 2005 4:43 am
by Think Pink

Code: Select all

echo replaceMessage($post); //<------------------------------------what is this line??----i printed the results

Code: Select all

} //<------------------------------------------------------------------------what is this another line?? ---- i typed it by mistake here

i used get_magic_quotes_gpc() and addslashes to format the data.

here is the text i inerted in the form
TestTest [ b ]test[ / b] [ i ]test[ / i] without spaces between []

here is the results printed with

Code: Select all

echo replaceMessage($post);
TestTest test test


here is the insert query

Code: Select all

$sql = mysql_query("INSERT INTO table(field) VALUES('".replaceMessage($post)."')");

Posted: Fri Sep 09, 2005 4:53 am
by raghavan20

here is the insert query
PHP:
$sql = mysql_query("INSERT INTO table(field) VALUES('".replaceMessage($post)."')");
run this query and echo this query on to your browser where you will get the values of the $post in the INSERT command.

I dont see you use mysql_escape_string.

Code: Select all

mysql_query("INSERT INTO table(field) VALUES('".mysql_real_escape_string(stripslashes(replaceMessage($post)))."')");

SOLVED inserting data into DB problem

Posted: Fri Sep 09, 2005 5:02 am
by Think Pink
now is working

I did mysql_real_escape_string() and is working great.

Anyhow, thx a lot guys. I learned something new today.

PS : do you think i should insert the message in the db as it comes from the form, and when to display it to use replaceMessage($post)?

Posted: Fri Sep 09, 2005 8:26 am
by feyd
it all comes down to a few decisions:
  • If you make changes to the tags, do you want previous posts to be affected by the change? Yes, run on display; No, perform it before insertion
  • Do you want the site to run at maximum speed, versus flexibility? Yes, perform it before insertion; No, run on display