Jump to content

SQL Syntax error - single quotes


TeddyKiller

Recommended Posts

My PHP is below. Basically, whenever I use an ' in a message or whatever, it brings up an SQL syntax error. EG:

Notice: You have an error in your SQL syntax; check the manualthat corresponds to your MySQL server version for the right syntax touse near 's me', 'testing it's me', '1270259346')' at line 2 in /home/jeanie/public_html/mail.php on line 400

 

          if(isset($_POST['send'])) {
            if(empty($_POST['message']) || empty($_POST['to'])){
                $error = '<span style=\"color:#E11919;\">You have left empty fields!</span><br />'; 
            }        
            if(!$error){
                $to = $_POST['to'];
                $from = $user->id;
                $subject = stripslashes($_POST['subject']);
                $message = stripslashes($_POST['message']);
                $date = time();

                $query = mysql_query("INSERT INTO `sent_messages` (to_id, from_id, subject, message, date) 
                VALUES ('$to', '$from', '$subject', '$message', '$date')") or trigger_error(mysql_error());
                echo "<span style=\"color:#E11919;\">Your message has been sent</span><br />";
            }
            echo $error;
        }

 

How can I fix this?

Link to comment
https://forums.phpfreaks.com/topic/197434-sql-syntax-error-single-quotes/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.