-1

I have a little problem in php/mySql.

Here is the code :

$updateLastchange = "UPDATE lastchange SET student = ? date = ? WHERE tutor= ? ";
$req = $db->prepare($updateLastchange);
if($req->execute(array($_SESSION['toHelp'], $date, $_SESSION['email'])))
{
    //some code that should be executed
}

You also have to know that "lastchange" actually contains the row where tutor = $_SESSION['email'], $_SESSION['toHelp'] is defined and date is just a date("Y-m-d H:i:s") created a bit earlier.

The problem is that the execute returns false, and I have a syntax error :

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'date = '2015-06-10 11:09:53' WHERE tutor= 'tutor''

I don't understand why ...

I use WAMP (I don't know if you need to know that).

Thank you for your help !

Raphallal
  • 122
  • 1
  • 12

1 Answers1

3

You forgot , in your query:

$updateLastchange = "UPDATE lastchange SET student = ?, date = ? WHERE tutor= ? ";

If you want to store a variable in your database, I think it is possible (though I haven't tried it yet):

$variable = '$variableYouWantToStore';

Just use a single tick ('), not double tick("), when doing this. You can refer here.

Community
  • 1
  • 1
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
  • Thank you ! I feel a bit stupid ... Just one question : is it possible to update a table whose name is a variable ? Like "UPDATE ? SET ..." with a execute later ? – Raphallal Jun 10 '15 at 09:39
  • @Raphallal if my answer did help you, you can upvote or mark my answer as the correct one. And I did update my answer regarding the storing of variable name. – Logan Wayne Jun 10 '15 at 09:41
  • I mean, I want my table name to be a variable... And I couldn't set your answer as "accepted" because I had to wait for 7 minutes. – Raphallal Jun 10 '15 at 09:59
  • @Raphallal - yes, you can do a query where the table name is coming from a variable. `"UPDATE ".$tablename." SET column1 = ? WHERE id = ?"` – Logan Wayne Jun 11 '15 at 00:59