亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將鏈接作為 href 標簽插入數據庫

將鏈接作為 href 標簽插入數據庫

PHP
隔江千里 2023-08-11 10:49:11
我在數據庫中插入鏈接時遇到問題。我通過幾個條件生成鏈接,但最終,當我嘗試在數據庫中插入鏈接時出現錯誤。另外,我從數據庫中提取描述,然后將這些鏈接添加到現有描述中,然后更新該值。$parent_path = 'https://sitename.com/game/'.$parentSlug;$parentLink = '<a href="'.$parent_path.'">'.$parentName.'</a>';$child_path = 'https://sitename.com/game/'.$slug;$childLink = '<a href="'.$child_path.'">'.$name.'</a>'; 我在腳本運行期間遇到幾個錯誤未捕獲的 PDOException: SQLSTATE[42000]: 語法錯誤或訪問沖突: 1064 您的 SQL 語法有錯誤;檢查與您的 MariaDB 服務器版本相對應的手冊,了解正確的語法,以使用附近的也是 CBD 教育部分,以幫助您在做出...時做出最佳決定。$sql = "SELECT * FROM `wpdev_postmeta` where post_id = $post_id  and meta_key = 'test'";$q = $conn->query($sql);     while($r = $q -> fetch()){     $description = $r['meta_value']; }$description 可能包含“a href 標簽”,當我回顯它時,它變成可點擊的鏈接,并且無法更新數據庫中的字段。$desc = $description.'<br/>'.$parentLink.$childLink;$stmt = "UPDATE wpdev_postmeta SET  meta_value = '$desc' WHERE meta_key  = 'test' and post_id = $post_id ";      if ($conn->query($stmt)) {      echo "Record updated successfully". '<br/>';} else {      echo "Error updating record: " . '<br/>';}為了將來的參考,我是否需要先從數據庫獲取數據,然后使用新的更新再次發送數據,以防我不想丟失之前插入的內容?我正在谷歌搜索但找不到合適的答案。
查看完整描述

1 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

如果您需要將 HTML 保存到數據庫,您可以使用兩種不同的方式

  1. 使用準備好的語句。

  2. 對所有標簽和其他特殊符號進行編碼,例如通過htmlentitieshtmlspecialchars。

示例htmlentities

<?php

? ? $link = '<a >Check that</a>';

? ? echo(htmlentities($link, ENT_QUOTES|ENT_HTML5));

? ? // output:

? ? /*

? ? &lt;a href&equals;&quot;https&colon;&sol;&sol;stackoverflow&period;com&sol;&quot;&gt;Check that&lt;&sol;a&gt;

? ? */

?>

用于解碼為 HTML 使用html_entity_decode


<?php

? ? $encoded = '&lt;a href&equals;&quot;https&colon;&sol;&sol;stackoverflow&period;com&sol;&quot;&gt;Check that&lt;&sol;a&gt;';

? ? echo(html_entity_decode($encoded, ENT_QUOTES|ENT_HTML5));

? ? // output

? ? /*

? ? <a >Check that</a>

? ? */

?>

示例htmlspecialchars:


<?php

? ? $link = '<a >Check that</a>';

? ? echo(htmlspecialchars($link, ENT_QUOTES|ENT_HTML5));

? ? // output

? ? /*

? ? &lt;a href=&quot;https://stackoverflow.com/&quot;&gt;Check that&lt;/a&gt;

? ? */

?>

對于解碼htmlspecialchars:


<?php

? ? $encoded = '&lt;a href=&quot;https://stackoverflow.com/&quot;&gt;Check that&lt;/a&gt;';

? ? echo(htmlspecialchars_decode($encoded, ENT_QUOTES|ENT_HTML5));

? ? // output

? ? /*

? ? <a >Check that</a>

? ? */

?>


查看完整回答
反對 回復 2023-08-11
  • 1 回答
  • 0 關注
  • 184 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號