title: INSERT private message system PDO php
body: <p>I am trying to build a little private message system in PHP, i am using PDO as connection to the database. i have got the most to work excpet the most important part.</p>
  
  <p>I cannot get the insert message to write anything to the database so i would love some help. </p>
  
  <p>I will first post my database so you all can see what we are working with here.
  starting with my users</p>
  
  <pre><code>id int(10) UN AI PK 
  username varchar(30) 
  password varchar(100) 
  regdate datetime
  </code></pre>
  
  <p>This is my message table which im trying to write the message to</p>
  
  <pre><code>id int(11) AI PK 
  from_user varchar(45) 
  to_user varchar(45) 
  subject varchar(400) 
  message text 
  date date 
  read tinyint(4) 
  </code></pre>
  
  <p>I have tried a couple of diffrent code to get it to work
  i will now paste the code that according to my error check says that the message  was succesfully sent... </p>
  
  <pre><code>$sql = "
  INSERT INTO private_messages VALUES('',$user, 
  $to_user','$subject',$message','$date','0')
  ";
  
      echo "Youre message was succesfully sent...";
  </code></pre>
  
  <p>But this does not work and i know that this is probably not the right way when using PDO. </p>
  
  <p>I have also tried this</p>
  
  <pre><code>$query =$dbh-&gt;prepare("INSERT INTO private_messages(user, to_user, subject, 
  message,
  date);
  VALUES('',$user, $to_user','$subject',$message','$date','0')
  ");
  
  $query-&gt;bindParam(":user", $user);
  $query-&gt;bindParam(":to_user", $to_user);
  $query-&gt;bindParam(":subject", $subject);
  $query-&gt;bindParam(":message", $message);
  
  if ($query-&gt;execute()) {
      $success = true;
  }
  </code></pre>
  
  <p>but then i get Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 </p>
  
  <p>I will also post the form im using if that can be of any value</p>
  
  <pre><code>echo "
  &lt;form action='compose.inc.php' method='POST'&gt;
  &lt;table&gt;
  &lt;tr&gt;
  &lt;td&gt;To: &lt;/td&gt;
  &lt;td&gt;&lt;input type='text' name='to_user'/&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Subject: &lt;/td&gt;
  &lt;td&gt;&lt;input type='text' name='subject'/&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Message:&lt;/td&gt;
  &lt;td&gt;&lt;textarea name='message' rows='10' cols='50'&gt;&lt;/textarea&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td colspan='2'&gt;&lt;input type='submit' name='submit' value='Send Message'/&gt;
  &lt;/td&gt;
  &lt;/r&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  
    ";
  </code></pre>
  
  <p>When doing this message system ive been following this <a href="https://www.youtube.com/watch?v=BED_yMC7qv0" rel="nofollow noreferrer">https://www.youtube.com/watch?v=BED_yMC7qv0</a> , its using mysqli and i have been trying to decode it to PDO, i think the problem lies therein</p>
  
  <p>So short recap
  My error check says that my message are sent.
  Nothing gets written to my database.
  im trying to use PDO.</p>
  
  <p>If a i cant find a solution i would love to get pointers to tutorial or books or anything that are about message system with PDO.
  Best regards /Robert</p>