[英]my data won't update php - sql
Hello I try to update a data in my sql with php. 您好,我尝试使用php更新SQL中的数据。
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 从其他页面上获取var后,我检查是否为空
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) (我使用wampserver,软件使用notepadd ++)
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; 致命错误:消息“ SQLSTATE [42000]”未捕获的异常“ PDOException”:语法错误或访问冲突:1064 SQL语法有错误; 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 在C:\\ wamp \\ www \\ testlp \\ completecheck中,在'SELECT MAX(prtTimeSheetId)FROM projetstaches WHERE usrId = 7 AND timId = prtTim'在第1行'附近检查与您的MySQL服务器版本对应的手册以使用正确的语法。第38行的php
and 和
( ! ) PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; (!)PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法错误; 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 在C:\\ wamp \\ www \\ testlp \\ completecheck.php的第1行中,检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'SELECT MAX(prtTimeSheetId)FROM projetstaches WHERE usrId = 7 AND timId = prtTim'附近使用在第38行
For Michael EDIT 对于迈克尔· 编辑
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. 由于UPDATE
没有引用projetstaches
任何列,并且可以通过订购timesheet
来确定每个timUserId
最新记录,因此JOIN
是不必要的,而实际上所有其他表都是不必要的。 Using ORDER BY
and LIMIT
, you can target the specific record. 使用ORDER BY
和LIMIT
,可以定位特定记录。
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
. 对于两个共享相同日期的情况,请按timCreateDate DESC
和timId
排序,因为它们是DATE
列而不是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 参考MySQL UPDATE
语法参考
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. 上面打印的查询可以通过execute()
调用及其定义的数组参数直接放入您的PDO
代码中。
Note: I would recommend changing the timCreatedDate
from a DATE
type to DATETIME
so that you may target it with more granularity. 注意:我建议将timCreatedDate
从DATE
类型更改为DATETIME
以便您可以更精确地定位它。 If you did that, you may not need to use timId DESC
in the ORDER BY
. 如果这样做,则可能不需要在ORDER BY
使用timId DESC
。
Change the line updating your MySQL from 更改从以下行更新MySQL
$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' );
ie remove the (timVerified) 即删除(timVerified)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.