Jump to content

[SOLVED] addslashes()


Recommended Posts

Adding to the above post, addslashes() is used to escape special characters in any string which is going to be used in a database query. Escaping means adding slashes to those special characters, like quotes, double quotes, backslashes etc. To remove those slashes in a value taken from a database, u use stripslashes(). Actually using mysql_real_escape_string() is advised.

Link to comment
Share on other sites

Ok, so to be clear, the following is how i should do things from now on:

 

$what_will_be_inserted = addslashes($_POST['asdf']);

 

Then i can insert. And then to get it out again:

 

$query = mysql(whatever);

$what_will_be_gotten = mysql_real_escape_string($query);

 

 

And this makes things safer?

 

 

Link to comment
Share on other sites

Consider this query:

 

$query = "SELECT * FROM users WHERE username = ''. $_POST['username'] .''";

 

The user could send "' OR '1'='1" as their username, which would make the query:

 

$query = "SELECT * FROM users WHERE username = '' OR '1'='1' ";

 

Which would always come back as true (not false) if queried. The user could of course do something even more malicious :o. What mysql_real_escape_string does is add slashes before the quotes, so they are taken literally, not as parts of the query.

 

$query = "SELECT * FROM users WHERE username = '\' OR \'1\'=\'1\''";

 

In this query where the input is escaped the username would have to be "' OR '1'='1" in order for the query to come back as true (not false).

Link to comment
Share on other sites

Heres a bit of a more specific question. Why doesn't this work?

 

$sql_recentnews = stripslashes(mysql_query("SELECT * FROM news ORDER BY timestamp DESC LIMIT 0 , " . $rpp));
while($topic=mysql_fetch_array($sql_recentnews)){
echo "<li class='recent_news_li'><span class='recent_news_topic'><a href='news.php?page=viewtopic&id=" . $topic['id'] . "'>" . $topic['title'] . "</a></span>";
}

 

I get this error:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/asdfdsa/public_html/testing123/index.php on line 69

Link to comment
Share on other sites

You need to use stripslashes on the actual output, not the query....

 

$sql_recentnews = mysql_query("SELECT * FROM news ORDER BY timestamp DESC LIMIT 0 , " . $rpp);

while($topic=mysql_fetch_array($sql_recentnews)){

echo "<li class='recent_news_li'><span class='recent_news_topic'><a href='news.php?page=viewtopic&id=" . $topic['id'] . "'>" . stripslashes($topic['title']) . "</a></span>";

}

 

Look at what I did.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.