inserting data into DB problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

inserting data into DB problem

Post 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
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Code: Select all

$sql = mysql_query("INSERT INTO table(field) VALUES('".replaceMessage($post)."')");
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

pssst, remember to escape the string returned so MySQL can work with it! ;)
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

inserting data into DB problem

Post by Think Pink »

well, i tried that and other few forms of it, and nothing happendes.
some more ideeas?
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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.
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

Post 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)."')");
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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)))."')");
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

SOLVED inserting data into DB problem

Post 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)?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
Post Reply