Jump to content

Inserting html tags into MySQL


denoteone

Recommended Posts

yes, but you will want to do it like this:

 

$mylink = "<a href='mysite.com?title=thanks'>click here</a>";
$sql = "INSERT INTO table_name ('link') VALUES ('".mysql_real_escape_string($mylink)."')";
mysql_query($sql) or die(mysql_error());

 

$result = mysql_query("SELECT * FROM table_name");
$row = mysql_fetch_array($result);
echo $row['link'];

mysql_real_escape_string() preps a string to be inserted into a MySQL DB. let's take your example for instance...the following:

$mylink = "<a href='mysite.com?title=thanks'>click here</a>";
$sql = "INSERT INTO table_name ('link') VALUES ('$mylink')";
echo $sql;

will produce:

INSERT INTO table_name ('link') VALUES ('<a href='mysite.com?title=thanks'>click here</a>')

but that is no good, cus you can't have single quotes inside single quotes. mysql_real_escape_string() adds slashes in front of any needed characters. the slashes don't stay with the string in the DB, they are just to escape it for the query. when you select the data back out, it won't have the extra slashes in it

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.