我在我的MySQL表列中存储HTML代码。为了插入记录,我构思了这样的查询:
$InsertQuery = "INSERT INTO listing (ldate, places, company, designation, projectdetails, desiredcandidate, hrname, hrcontact, email) VALUES (DATE_FORMAT('" .$ldate ."','%Y/%m/%d'),'" .$places. "','" .$company. "','" .$designation. "','" .htmlentities($projectdetails). "','" .htmlentities($desiredcandidate). "','" .$hrname. "','" .$hrcontact. "','" .$email. "')";
但是当我使用以下代码输出结果时,它会显示带有HTML标记的文本。它没有在页面上实现标记。
$FetchResultsQuery = "SELECT * FROM listing WHERE recordid=" . $SelectedRowID;
$result = mysql_query($FetchResultsQuery);
$row = mysql_fetch_row($result);
if ($row)
{
$PostedDate = date($row[0],'d.m.Y');
$Places = $row[1];
$Company = $row[2];
$Designation = $row[3];
$ProjectDetails = $row[4];
$DesiredCandidate = $row[5];
$HRName = $row[6];
$HRContact = $row[7];
$Email = $row[8];
}
<td valign="top" style="padding-left:10px; text-align:justify;"><h4> </h4>
<p><strong>Company Name: </strong> <strong><?PHP echo $Company; ?></strong></p>
<p> </p>
<p><strong>Location: </strong> <?PHP echo $Places; ?> </p>
<p> </p>
<p><strong>Posted Date: </strong><span class="style19"> <?PHP echo $PostedDate; ?></span></p>
<p> </p>
<p><strong>Designation:</strong><?PHP echo $Designation; ?></p>
<p> </p>
<p><strong>Project Details :</strong></p><br>
<?PHP echo $ProjectDetails; ?>
<p> </p>
<p><strong>HR Name: </strong> <?PHP echo $HRName; ?> </p>
<p> </p>
<p><strong> HR's Contact details: </strong><?PHP echo $HRContact; ?></p>
<p> </p>
<p><strong>Email: </strong><?PHP echo $Email; ?></p>
<p><span class="style18"><strong><br />
<br />
</strong></span></p></td>
<td></td>
答案 0 :(得分:3)
问题是htmlentities函数将所有尖括号转换为等效的HTML实体。因此,而不是输出'&lt;'你会得到'&amp; lt;'。
在数据库中存储数据时,通常会将其存储为原始格式,即未编码,因为您可能并不总是希望以HTML格式显示数据,例如您可能希望将其导出为CSV。在浏览器中输出数据时,通常会使用 htmlentities 。
如果您还没有这样做 - 从您的INSERT代码段中说出来有点难以理解 - 您可能希望修改SQL以通过mysql_real_escape_string或使用prepared statements来传递参数
答案 1 :(得分:2)
尝试像这样回应:
print html_entity_decode('your text here');