【问题标题】:sql update statement including & and line breaksql更新语句,包括&和换行符
【发布时间】:2013-02-06 02:21:24
【问题描述】:

我正在使用这个更新语句

$sql = "update questions set response = ?, `check` = ? where questionID = ? && starID = ?";
$qc = $pdo_conn->prepare($sql);
$qc->execute(array($_GET['response'], $_GET['check'], $_GET['questionID'], $_SESSION['starid']));

但是当响应值中包含& 时,例如pop and r&b,它最终在数据库中为pop and r

如果有换行符,它会删除所有空格,例如:

Bob
Jim

在数据库中以BobJim 结尾

数据库中的响应类型为varchar(10000)

这是获取 $_GET['response'] 值的 javascript 代码

var html = '';
$(document).ready(function(){
    $(".save_btn").live('click', function() {

        $('.response').each(function(){
            //alert($(this).attr('id'));
            //alert($(this).val());
            if ($(this).val() == '') {
                html = $.ajax({
                    url: "response.php?questionID=" + $(this).attr('id') + "&response=" + $(this).val() + "&check=0",
                    async: false
                }).responseText;
            }   
            if ($(this).val() !== '') {
                html = $.ajax({
                    url: "response.php?questionID=" + $(this).attr('id') + "&response=" + $(this).val($POST['response']) + "&check=1",
                    async: false
                }).responseText;
            }   

        }); 
        alert(html);
        location.reload();  
    });
})

if 的这一部分是本例中的重要部分:

       if ($(this).val() !== '') {
            html = $.ajax({
                url: "response.php?questionID=" + $(this).attr('id') + "&response=" + $(this).val($POST['response']) + "&check=1",
                async: false
            }).responseText;
        }

关于如何解决这个问题的任何想法?

【问题讨论】:

  • 你可能应该看看你的 $_GET 变量的实际值,这可能是他们搞砸的地方。
  • 对于流行和 r&b,echo $_GET['response']; = 流行音乐和 r。尝试 post 而不是 get 并得到一个空值
  • 因此,无论通过 GET 请求发送这些变量的任何内容都没有正确编码它们,因此应首先修复。
  • 好的,我将编辑我的帖子以包含那段代码

标签: php sql line-breaks ampersand update-statement


【解决方案1】:

您应该对 URL 的值进行编码:

escape($(this).val())

& 在 URL 中具有特殊含义,用作参数之间的分隔符。

【讨论】:

  • 像这样: if ($(this).val() !== '') { html = $.ajax({ url: "response.php?questionID=" + $(this) .attr('id') + "&response=" + escape($(this).val($POST['response'])) + "&check=1", async: false }).responseText;这使得 echo $_POST['response'] 不再出现}
  • @jenstar 不应该是escape($(this).val())吗?你确定你没有混合 PHP 和 Javascript 吗? $(this).val($POST['response']) 真的很奇怪。
  • 我刚刚取出了 $_POST['response'] 部分,所以它只是 escape($(this).val()) 现在流行和 r&b,使用 echo $_GET['response'] ; = 流行和 r&b。谢谢它现在可以工作了:-)
猜你喜欢
  • 2019-08-24
  • 2012-03-20
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2016-01-18
  • 1970-01-01
  • 2021-12-19
相关资源
最近更新 更多