Pregunta

Hello I try to update a data in my sql with php.

First I connect to my database

try
{
$bdd = new PDO('mysql:host=localhost;dbname=cruel', 'root', '');
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}

After I take my var from my other page I check if empty

 if (isSet($_POST['radio']) ) 
    {

        $timVerified = ($_POST['radio']) ;
        echo($_POST['radio']);
    }else{
        echo('Aucun changement à été effectué. ');

    }

I make a request to select all my data I need

$requete = $bdd->prepare('SELECT timVerified,usrId, timChoice,prtTimeSheetId,timId,timUserId  FROM projetstaches ,users,timesheets   ');
$requete->execute() or die(print_r($requete->errorInfo()));
$resultat = $requete->fetch();

And here is the problem It's won't update here

$req = $bdd->prepare('UPDATE timesheets (timVerified) SET timVerified = :timVerified SELECT MAX(prtTimeSheetId) FROM projetstaches WHERE usrId = 7 AND timId = prtTimeSheetId AND timUserId = usrID' ); 
 $req->execute(array(
      ':timVerified' => $timVerified
     ));

Could someone help me? ( i use wampserver, notepadd ++ for software)

EDIT For fred the 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 'SELECT MAX(prtTimeSheetId) FROM projetstaches WHERE usrId = 7 AND timId = prtTim' at line 1' in C:\wamp\www\testlp\completecheck.php on line 38

and

( ! ) PDOException: 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 'SELECT MAX(prtTimeSheetId) FROM projetstaches WHERE usrId = 7 AND timId = prtTim' at line 1 in C:\wamp\www\testlp\completecheck.php on line 38

For Michael EDIT

http://pastebin.com/6LBwGtc3

¿Fue útil?

Solución

Since none of the columns from projetstaches are referenced in the UPDATE, and the most recent record per timUserId can be determined by ordering timesheet, the JOIN is unnecessary, and indeed all the other tables are unnecessary. Using ORDER BY and LIMIT, you can target the specific record.

Sort by timCreateDate DESC and by timId for the cases where two share the same date since it is a DATE column rather than a DATETIME.

UPDATE timesheets 
SET timVerified = :timVerified
WHERE timUserId = 7
ORDER BY
  timCreatedDate DESC,
  timId DESC
# Crucially, LIMIT 1 to target one record only (the first in the sort)
LIMIT 1

Reference MySQL UPDATE syntax reference

The query as printed above can be placed directly into your PDO code with the execute() call and its array parameter defined as you already have it.

Note: I would recommend changing the timCreatedDate from a DATE type to DATETIME so that you may target it with more granularity. If you did that, you may not need to use timId DESC in the ORDER BY.

Otros consejos

Change the line updating your MySQL from

$req = $bdd->prepare('UPDATE timesheets (timVerified) SET timVerified = :timVerified SELECT MAX(prtTimeSheetId) FROM projetstaches WHERE usrId = 7 AND timId = prtTimeSheetId AND timUserId = usrID' ); 

to

$req = $bdd->prepare('UPDATE timesheets SET timVerified = :timVerified SELECT MAX(prtTimeSheetId) FROM projetstaches WHERE usrId = 7 AND timId = prtTimeSheetId AND timUserId = usrID' ); 

i.e. remove the (timVerified)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top