简体   繁体   English

如何使此UPDATE仅在while循环中起作用?

[英]How do I make this UPDATE only work within this while loop?

I've been working on this for a couple days, and my brain has finally gone numb. 我已经为此工作了几天,我的大脑终于变得麻木了。 My UPDATE is just updating every StartDate in the database, but I want it to only act on the event that is in the while loop this UPDATE is contained in. I think the code is probably fairly obvious, so here it is: 我的UPDATE只是更新数据库中的每个StartDate,但我希望它仅对包含此UPDATE的while循环中的事件进行操作。我认为代码可能很明显,所以这里是:

$query = "SELECT * FROM events WHERE ParentEventID='$tempParentEventID' AND GoogleID IS NULL";
$result = mysql_query($query);
            while($row = mysql_fetch_array($result, MYSQL_ASSOC)){

                $tempDaysFromEvent = $row['DaysFromEvent'];
                $tempStartDate = $row['StartDate'];

                //IF STARTDATE IS DIFFERENT FROM HOW IT USED TO BE, UPDATE IT.
                list($year, $month, $day) = explode("-", $tempStartDate);
                $tempStartDate      =   $tempEndDate    =    date("Y-m-d", mktime (0,0,0,$month,$day+$tempDaysFromEvent,$year));


                mysql_query("UPDATE".$eventDatabase." SET
                StartDate = '$tempStartDate'");


            }

I hope the above is clear enough. 我希望以上内容足够清楚。 I am trying to change the StartDate in each $row as it goes through the loop. 我试图通过循环更改每个$ row中的StartDate。

如何添加where子句。

"UPDATE table SET field = '$sValue' WHERE eventID = ".$aRow['eventId']);

查询应该是

 "UPDATE".$eventDatabase." SET  StartDate = '$tempStartDate' WHERE EventId =". $row['evenID'];

simply add a WHERE -clause to your update-statement: 只需在您的更新语句中添加WHERE -clause:

UPDATE table SET StartDate = ... WHERE [whatever*]

*you havn't posted you table definitions, but it sounds like you should use your event-id here. *您尚未发布表定义,但听起来您应该在此处使用事件ID。

您需要在UPDATE-Statement中使用WHERE- WHERE来标识要更新的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM