php - mysql_real_escape_string(htmlspecialchars) 函数在编辑值时不起作用

标签 php mysql

我在我的网站中使用意大利语言作为主要语言,因此当我编辑详细信息时,一些语言字符会自动转换为特殊字符。有谁知道如何解决这个问题...!

这是我用来编辑数据库值和转换特殊字符的代码

$title7      = mysql_real_escape_string(htmlspecialchars($_POST['title7']));
$description7    = mysql_real_escape_string(htmlspecialchars($_POST['description7']));

这是我用来编辑数据库值的完整代码

// connect to the database
 include('db.php');

 if (isset($_POST['submit']))
 { 
 // confirm that the 'id' value is a valid integer before getting the form data
 if (is_numeric($_POST['id']))
 {
 // get form data, making sure it is valid
 $id = $_POST['id'];
 $language       = mysql_real_escape_string($_POST['txtLanguage']);
 $pkg_name       = mysql_real_escape_string($_POST['pkg_name']); 
 $category       = mysql_real_escape_string($_POST['category']);
 $title          = mysql_real_escape_string($_POST['title']);
 $description1   = mysql_real_escape_string($_POST['description1']);
 $title2         = mysql_real_escape_string($_POST['title2']);
 $description2   = mysql_real_escape_string($_POST['description2']);

 if ($pkg_name == '' || $category == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in Package name field!';

 //error, display form
 renderForm($id,$language,$pkg_name,$category,$title,$description1,$title2,$description2);
 }
 else
 {   

 // save the data to the database
 mysql_query("UPDATE saved_packages SET 
 language       ='$language',
 pkg_name       ='$pkg_name', 
 category       ='$category', 
 title          ='$title',
 description1   ='$description1', 
 title2         ='$title2', 
 description2   ='$description2', 
 WHERE id='$id'"); 

 // once saved, redirect back to the view page
 header("Location: adm_view_package.php"); 
 }
 }
 else
 {
 // if the 'id' isn't valid, display an error
 echo 'Error!';
 }

这是结果 enter image description here

最佳答案

将数据保存到数据库时不要使用htmlspecialchars

你应该只做:

$title7      = mysql_real_escape_string($_POST['title7']);
$description7    = mysql_real_escape_string($_POST['description7']);

当渲染从数据库获取的数据时,使用它:

echo htmlspecialchars($title7);
echo htmlspecialchars($description7);

关于php - mysql_real_escape_string(htmlspecialchars) 函数在编辑值时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22218025/

相关文章:

mysql - 获取哪些区域拥有三名以上合格员工

php - 使用 PDO 插入带有特殊字符的值

php - 明显的语法错误

mysql - 查询构建自动化

mysql - 像 mysql 一样设置 Big Query 变量

php - 如何比 `object` 更好地访问 `$collection[0]->object` 数据

php - 用PHP下载MySql多表

php - 获取多维数组的最后一个索引

php - MVC,填充我的演示模型的最佳实践是什么?

php - 收到电子邮件时如何运行代码