gpt4 book ai didi

php - 从 MySQL 结果中转义 PHP 中的引号 (PDO)

转载 作者:行者123 更新时间:2023-11-29 21:01:17 24 4
gpt4 key购买 nike

要显示的字符串来自 MySQL 表。我使用 PDO 查询。该字符串包含双引号:

Older spelling (from French). The modernized "petty larceny" is now preferred.

这是字典条目的定义,如果可用的话将显示:

$search_results .= (!empty($english_definition)? "<a class=\"definition\" href=\"#\" data-toggle=\"popover\" rel=\"popover\"
data-content=\"".$english_definition."\">".$english."*</a>" : $english);

由于引号的原因,定义被切到“现代化”一词之后。

我尝试使用addslashes(),但结果是显示单个斜杠,后面没有任何内容。

我还尝试在表字段内添加斜杠,例如

Older spelling (from French). The modernized \"petty larceny\" is now preferred.

如果 PHP 代码中没有 stripslashes(),则仅显示第一个反斜杠,后面不显示任何内容。

当我添加 stripslashes() 时,“modernized”一词后面不显示任何内容。

所以,这就是我陷入困境的地方。

附加代码:这就是我插入新术语和定义的方式。我为可能存在引号的输入添加了 htmlspecialchars() :

        if(isset($_POST['submit'])) {

$english = htmlspecialchars($_POST['english']);
$english_abbr = $_POST['english_abbr'];
$variant = $_POST['variant'];
$bulgarian = htmlspecialchars($_POST['bulgarian']);
$bulgarian_abbr = $_POST['bulgarian_abbr'];
$theme_id = $_POST['theme_id'];
$english_definition = htmlspecialchars($_POST['english_definition']);
$bulgarian_definition = htmlspecialchars($_POST['bulgarian_definition']);

// Check if an entry already exists
$exists = $db->prepare("SELECT * FROM ".DICTIONARY_TABLE." WHERE english = :english AND theme_id = :theme_id ");
$exists->execute(array(':english' => $english, ':theme_id' => $theme_id));
$count = $exists->rowCount();
if($count > 0) {
echo "<h3 style=\"color:navy; background:transparent;\">&#8658; An entry in the same theme already exists.</h3>";
}
else {
$insert = $db->prepare("INSERT INTO ".DICTIONARY_TABLE."
(english, english_abbr, variant, bulgarian, bulgarian_abbr, theme_id)
VALUES
(:english, :english_abbr, :variant, :bulgarian, :bulgarian_abbr, :theme_id)");
$insert->execute(array(':english' => $english,
':english_abbr' => $english_abbr,
':variant' => $variant,
':bulgarian' => $bulgarian,
':bulgarian_abbr' => $bulgarian_abbr,
':theme_id' => $theme_id));

if($insert) {
echo "<h4 style=\"color:green; background:transparent;\">&#8658; Term \"$english\" inserted successfully.</h4>";

if(!empty($english_definition) || !empty($bulgarian_definition)) {
$insert_id = $db->lastInsertId();
$insert_def = $db->prepare(
"INSERT INTO ".DICTIONARY_DEFINITIONS."
(term_id, english_definition, bulgarian_definition)
VALUES
(:term_id, :english_definition, :bulgarian_definition)");
$insert_def->execute(array(
':term_id' => $insert_id,
':english_definition' => $english_definition,
':bulgarian_definition' => $bulgarian_definition));

if($insert_def) {
echo "<h4 style=\"color:green; background:transparent;\">&#8658; Definition(s) inserted successfully.</h4>";
}
else {
echo "<h4 style=\"color:red; background:transparent;\">&#8658; There was a problem inserting the definition(s)!</h4>";
}
}

unset($_POST); $_POST = array();
}
else {
echo "<h4 style=\"color:red; background:transparent;\">&#8658; There was a problem executing the query: </h4>";
}
}
include("insert_form.php");
}
else {
include("insert_form.php");
}

最佳答案

将其保存到数据库时使用 htmlspecialchars() 函数,并在想要再次回显时使用 htmlspecialchars_decode() 函数。

Link to htmlspecialchars() function

Link to htmlspecialchars_decode() function

关于php - 从 MySQL 结果中转义 PHP 中的引号 (PDO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37206020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com