0

I realize that variations of this question have been asked before. I've looked through many, many answers and haven't found something that fixes this.

I'm building a simple private messaging system and I've been trying to get this insert statement to work all day: (NOTE: the code is separated by lines here for readability only)

$query = $conn->prepare(
"INSERT INTO messages (assigned, to, from, subject, body, created)
 VALUES( '?', '?', '?', '?', '?', '?')" );

I've tried with quotes and without quotes. I've also tried replacing the created variable with the mysql NOW function:

$query = $conn->prepare(
"INSERT INTO messages (assigned, to, from, subject, body)
 VALUES( '?', '?', '?', '?', '?', NOW())" );

Nothing works. I've been getting 2 errors depending on whether I have full reporting on or not. It gives me Uncaught exception 'mysqli_sql_exception' with message 'You have an error in your SQL syntax; which hasn't helped and, because the prepare statement returns false and the next line uses it as an object, something along the lines of boolean cannot be used as an object.

I'm probably making a dumb mistake somewhere and I can't see the forest for the trees, but nonetheless I've spent way too much time on this.

zfrisch
  • 8,474
  • 1
  • 22
  • 34
  • Placeholders shouldn't be quoted. In your second query, you're passing along 5 columns but 6 variables. If you don't use the quotes and use the correct number of column/variables, what is the full error that you get? The "Error in your SQL syntax" should also give you the sql query. – aynber Jul 05 '16 at 17:59
  • For someone else wondering - backticks was necessary to get this to work. You'll find the answer in the link to the duplicate, however it's a long post. – zfrisch Jul 05 '16 at 18:49
  • `To` and `From` are reserved words. I had missed that. – aynber Jul 05 '16 at 19:18

0 Answers0