I'm making a quotes and phrases site. I want to post this quotes and prevent <script>
or similar <html tags>
, I'm currently using the following query to post and push this data to the database
mysql_query("INSERT INTO `phrases`
(`id`, `text`, `date`, `views`, `ip`, `reported`, `strange`, `lang`)
VALUES (NULL, '$quote', '$date', '0', '$ip', '0', '0', 'en')
");
using the following PHP code
<?php
$date = date('Y-m-d H:i:s', time());
$quote = $_POST["quote"];
$ip = $_SERVER['REMOTE_ADDR'];
//The query above
?>
But I get the following problems:
COMPLETED: Timestamp is not showing correctly.
I'm getting some unescaped characters.
For example, I'm trying to post this:
<Ω∑©√ß µ„…–å∫∂ƒ™¶§ ~{}œæ€®†¥ øπ[]
If I post this
as is
, it posts<Ω∑©√ß µ„…–å∫∂ƒ™¶§ ~{}œæ€®†¥ øπ[]
If I use
mysql_real_escape_string($quote)
, it posts<Ω∑©√ß µ„…–å∫∂ƒ™¶§ ~{}œæ€®†¥ øπ[]
And if I use
htmlspecialchars(nl2br(stripslashes($quote)))
, it posts<Ω∑©√ß µ„…–å∫∂ƒ™¶§ ~{}œæ€®†¥ øπ[]
I've been warned that this is vulnerable to SQL injection
How can I prevent this?
As additional information, this is called via AJAX.