Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_real_escape_string() just makes an empty string?

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.

like image 360
VIVA LA NWO Avatar asked Jun 09 '10 10:06

VIVA LA NWO


People also ask

What is the use of mysql_real_escape_string () function?

The real_escape_string() / mysqli_real_escape_string() function escapes special characters in a string for use in an SQL query, taking into account the current character set of the connection.

Is mysql_real_escape_string deprecated?

This extension was deprecated in PHP 5.5. 0, and it was removed in PHP 7.0.

Why does mysql_real_escape_string need a connection?

mysql_real_escape_string() and prepared statements need a connection to the database so that they can escape the string using the appropriate character set - otherwise SQL injection attacks are still possible using multi-byte characters.

Does mysql_real_escape_string prevent SQL injection?

mysql_real_escape_string ALONE can prevent nothing. Moreover, this function has nothing to do with injections at all. Whenever you need escaping, you need it despite of "security", but just because it is required by SQL syntax. And where you don't need it, escaping won't help you even a bit.


1 Answers

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.

like image 162
Pekka Avatar answered Oct 16 '22 13:10

Pekka