我有一个TinyMCE编辑器,它的内容需要由AJAX上传。
很明显,由于AJAX发送参数,它们需要通过javascripts ()函数进行转义,这样就不会破坏AJAX参数。这些字段是在mysql_real_escape_string端编辑的,我只需要转义AJAX参数。
不幸的是,当我在编辑器中添加链接和图像,然后提交时,图像和链接的URL如下所示:
http://localhost:8888/%22../img/miscimages/hwo-american.gif/%22在向用户显示内容的页面(产品视图页)上,数据库中的字符串通过urldecode()运行,以消除所有%22和其他转义字符。这个页面上没有javascript,它都是用PHP生成的,因此是urldecode()而不是unescape()。有办法绕过这件事吗?
代码:
AJAX发送
function update(){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = xmlhttp.responseText;
alert(response);
window.location = 'admin-products.php';
}
}
var prodID=document.getElementById("editchoice").value;
var edittitle=escape(document.getElementById("editname").value);
var editcategory=document.getElementById("editcat").value;
var editcontent = escape(tinyMCE.get('editcontent').getContent());
var parameters= "prodID="+prodID+"&title="+edittitle+"&content="+editcontent+"&cat="+editcategory;
xmlhttp.open("POST", "../scr/editProduct.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(parameters);
}PHP数据库插入
$prodID = $_POST['prodID'];
$title = urldecode(mysql_escape_string($_POST['title']));
$content = urldecode(mysql_escape_string($_POST['content']));
$category = $_POST['cat'];
echo $prodID . $title . $content . $category;
mysql_query("UPDATE product SET title='$title', content='$content', category_id='$category' WHERE id='$prodID'") or die("ERROR: ".mysql_error());页面上的PHP显示
/* Get Product from Alias */
$alias = $_GET['name'];
$product_selectProd = mysql_query("SELECT * FROM product WHERE alias='$alias'") or die("ERROR: ". mysql_error());
/* Sort Query Vars */
while($product_arrayProd = mysql_fetch_array($product_selectProd)){
$product_category = $product_arrayProd['category_id'];
$product_title = urldecode($product_arrayProd['title']);
$product_text = urldecode($product_arrayProd['content']);
$product_image = $product_arrayProd['main_image'];
$product_sub_image = $product_arrayProd['sub_image'];
/* Build the Product List */
$productDetail .= "<img src='$product_image' width='350' height='240' class='prod_image_left' /><img src='$category_image' width='350' height='240' class='prod_image_right' />";
$productDetail .= "<p> </p><h1>Fiddes $product_title</h1><hr />";
$productDetail .= "$product_text";
}发布于 2011-03-03 13:42:25
答案似乎在TinyMCE中,我正在对字符串进行mysql_real_escape_string,但是默认情况下TinyMCE已经这样做了,所以当php正在取消转义这些字符时,它只是转义它的一个实例,而不是它的所有实例。
发布于 2011-03-03 10:25:46
来自MDC:https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Functions#escape_and_unescape_Functions
转义和取消转义函数对于非ASCII字符不能正常工作,因此已被取消推荐。在JavaScript 1.5及更高版本中,使用encodeURI、decodeURI、encodeURIComponent和decodeURIComponent。
所以不要使用转义,它被废弃了:)
发布于 2011-03-03 10:40:54
只是在你的数据库插入一张便条。
在其他地方执行此操作,而不是将数据插入数据库的时间。mysql_real_escape_string应该是,这是在插入之前应用于数据的最后一件事。或者你打破了一切(就像你目前所做的那样),,
- escape it the same way as strings, as you already quoting it
- or cast it to prober type, using intval() function for example
- or get rid of all escaping quoting and casting and use prepared statements to bind variables. (it will require major code changes)
别名变量和站点中的所有其他动态查询也是一样的。你应该尽快修好它。
https://stackoverflow.com/questions/5179101
复制相似问题