mysql_real_escape_string() 只是生成一个空字符串?

发布于 2024-09-04 21:06:28 字数 1648 浏览 12 评论 0原文

我正在使用 jQuery AJAX 请求一个名为 like.php 的页面,该页面连接到我的数据库并插入一行。这是 like.php 代码:

<?php

// Some config stuff
define(DB_HOST, 'localhost');
define(DB_USER, 'root');
define(DB_PASS, '');
define(DB_NAME, 'quicklike');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('ERROR: ' . mysql_error());
$sel = mysql_select_db(DB_NAME, $link) or die('ERROR: ' . mysql_error());

$likeMsg = mysql_real_escape_string(trim($_POST['likeMsg']));
$timeStamp = time();

if(empty($likeMsg))
    die('ERROR: Message is empty');

$sql = "INSERT INTO `likes` (like_message, timestamp)
        VALUES ('$likeMsg', $timeStamp)";

$result = mysql_query($sql, $link) or die('ERROR: ' . mysql_error());

echo mysql_insert_id();

mysql_close($link);

?>

有问题的行是 $likeMsg = mysql_real_escape_string(trim($_POST['likeMsg']));。它似乎只是返回一个空字符串,在我的数据库中,在 like_message 列下,我看到的都是空白条目。如果我删除 mysql_real_escape_string() ,它工作正常。

这是我的 jQuery 代码(如果有帮助的话)。

$('#like').bind('keydown', function(e) {
    if(e.keyCode == 13) {
        var likeMessage = $('#changer p').html();

        if(likeMessage) {
            $.ajax({
                cache: false,
                url: 'like.php',
                type: 'POST',
                data: { likeMsg: likeMessage },
                success: function(data) {
                    $('#like').unbind();
                    writeLikeButton(data);
                }
            });
        } else {
            $('#button_container').html('');
        }
    }
});

所有这些 jQuery 代码都工作正常,我自己独立测试过。

非常感谢任何帮助,谢谢。

I am using a jQuery AJAX request to a page called like.php that connects to my database and inserts a row. This is the like.php code:

<?php

// Some config stuff
define(DB_HOST, 'localhost');
define(DB_USER, 'root');
define(DB_PASS, '');
define(DB_NAME, 'quicklike');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('ERROR: ' . mysql_error());
$sel = mysql_select_db(DB_NAME, $link) or die('ERROR: ' . mysql_error());

$likeMsg = mysql_real_escape_string(trim($_POST['likeMsg']));
$timeStamp = time();

if(empty($likeMsg))
    die('ERROR: Message is empty');

$sql = "INSERT INTO `likes` (like_message, timestamp)
        VALUES ('$likeMsg', $timeStamp)";

$result = mysql_query($sql, $link) or die('ERROR: ' . mysql_error());

echo mysql_insert_id();

mysql_close($link);

?>

The problematic line is $likeMsg = mysql_real_escape_string(trim($_POST['likeMsg']));. It seems to just return an empty string, and in my database under the like_message column all I see is blank entries. If I remove mysql_real_escape_string() though, it works fine.

Here's my jQuery code if it helps.

$('#like').bind('keydown', function(e) {
    if(e.keyCode == 13) {
        var likeMessage = $('#changer p').html();

        if(likeMessage) {
            $.ajax({
                cache: false,
                url: 'like.php',
                type: 'POST',
                data: { likeMsg: likeMessage },
                success: function(data) {
                    $('#like').unbind();
                    writeLikeButton(data);
                }
            });
        } else {
            $('#button_container').html('');
        }
    }
});

All this jQuery code works fine, I've tested it myself independently.

Any help is greatly appreciated, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

葵雨 2024-09-11 21:06:29

您是否 1000% 确定 $_POST["likeMsg"] 实际上包含某些内容?

至于mysql_real_escape_string()返回空值,手册说只有可能发生这种情况的一种情况:

注意:使用mysql_real_escape_string()之前需要先连接MySQL,否则会产生E_WARNING级别的错误,并返回FALSE。如果未定义 link_identifier,则使用最后一个 MySQL 连接。

但这里的情况似乎并非如此,因为您确实有一个打开的连接。奇怪的。

Are you 1000% sure that $_POST["likeMsg"] actually contains something?

As for mysql_real_escape_string() returning an empty value, the manual says there is only one situation where that can happen:

Note: A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level E_WARNING is generated, and FALSE is returned. If link_identifier isn't defined, the last MySQL connection is used.

this doesn't seem to be the case here though, as you do have a connection open. Strange.

猫九 2024-09-11 21:06:29

由于其他答案没有明确说明具体要做什么,这是我的:

当你这样做时,

$db_connection = new mysqli($SERVER, $USERNAME, $PASSWORD, $DATABASE);

你需要像这样转义:

$newEscapedString = $db_connection->real_escape_string($unescapedString);

注意:因为人们对此表示反对(WTF!?),这里是官方 php 手册的官方页面完全说明了我发布的内容:real_escape_string @ PHP 手册

As the other answers don't make clear what exactly to do, here's my:

When you do

$db_connection = new mysqli($SERVER, $USERNAME, $PASSWORD, $DATABASE);

you need to escape like this:

$newEscapedString = $db_connection->real_escape_string($unescapedString);

NOTE: Because people are downvoting this (WTF!?), here's the official page of the official php manual that says EXACTLY what i have posted: real_escape_string @ PHP Manual.

又爬满兰若 2024-09-11 21:06:29

对于现在可能再次发现此问题的人,我刚刚从 PHP5 迁移到 PHP7 时遇到了这个问题。我正在从

string mysql_real_escape_string(string $unescaped, [resource $link = NULL])

更改为:

string mysqli_real_escape_string(mysqli $link, string $escapestr)

所以,在换句话说,数据库 $link 不再是可选的,并且移动到第一个参数位置。如果省略,它会返回一个空字符串,显然不会出现错误。

For people who might be finding this again now, I just ran into this problem as I'm migrating from PHP5 to PHP7. I'm changing from

string mysql_real_escape_string(string $unescaped, [resource $link = NULL])

to:

string mysqli_real_escape_string(mysqli $link, string $escapestr)

So, in other words, the database $link is no longer optional and moves to the first argument position. If left out, it returns an empty string, without an error, apparently.

余厌 2024-09-11 21:06:29

执行 $_POST['likeMsg'] 的 var_dump 和 $likeMsg 的 var_dump。这为您提供了有关问题所在的信息。

Do a var_dump of $_POST['likeMsg'], and a var_dump of $likeMsg. That gives you information on what goes wrong.

放我走吧 2024-09-11 21:06:29

如果您尚未连接到数据库,mysql_real_escape_string() 将返回空白响应...

mysql_real_escape_string() will return blank response if you have not made connection to database ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文