Phi*_*hil 5 php regex mysql sql jquery
我有一个页面,它对 api 进行 jquery 调用以接收多个维基百科 url。然后,我从 url 中提取文章名称(即science从http://en.wikipedia.org/science等中获取),为每个文章添加单引号 ('),将它们串在一起,最后将它们发送到 php 页面,这会进行 mysql 调用select * from MyTable where title in('name1','name2','name3','name4')。当文章名称中已经有单引号(即“希克定律”)时,就会出现问题,因为它破坏了where in单引号。这是我正在使用的代码:
$.getJSON('http://ajax.googleapis.com/ajax/services/search/web?q=keyword site:en.wikipedia.org&rsz=8&v=1.0&callback=?',
function (r) {
var urls1="";
$.each(r.responseData.results, function(i, item) {
var thisurl = (i==0) ? "'" + item.url.substring(item.url.lastIndexOf('/') + 1) + "'" : ",'" + item.url.substring(item.url.lastIndexOf('/') + 1) + "'";
urls1 += thisurl.replace(/_/g,'%20');
});});
$('#quotes').html($('<div>').load('pr.php?s='+urls1 +' #quotes', function() {}
Run Code Online (Sandbox Code Playgroud)
我将单引号添加到文章名称中,因此字符串应该已准备好用于 mysql where in。
回顾一下,步骤如下:
urls1字符串中,同时用空格替换下划线urls1到pr.php页面。"SELECT * FROM MyTable WHERE title in".$_GET['s']我尝试这样做mysql_real_escape_string($_GET['s']),但没有成功。
我现在试图转义文章名称中的任何单引号,这样就where in不会中断,但它不起作用。我尝试将上面的内容更改为
var thisurl=(i==0) ? "'"+item.url.substring(item.url.lastIndexOf('/') + 1).replace(/'/g, "\'")+"'":",'"+item.url.substring(item.url.lastIndexOf('/') + 1).replace(/'/g, "\'")+"'";
Run Code Online (Sandbox Code Playgroud)
但这没有用。有任何想法吗?
蒂亚!
因为它的 HTML 内容,您应该能够用 替换 的'实例"。然后,它将在浏览器中正确显示,但在对执行相同替换所需的内容执行搜索时,您必须保持清醒(例如,“希克定律”作为搜索词将在子句声明中变为“希克定律” WHERE) )。
PHP 可能内置了用于转义 HTML 的功能,因此我建议您在自己处理此问题之前先查找一下。
| 归档时间: |
|
| 查看次数: |
566 次 |
| 最近记录: |