PHP mysql update limit?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • impin
    New Member
    • Jul 2010
    • 127

    PHP mysql update limit?

    Table: cquestions
    cqid, cqtext, showdate.

    I want to update 'showdate' field to tomorrows date..

    database:
    Code:
    	cqid	cqtext	showdate
    	200	q1	2013-05-22
    	201	q2	0000-00-00
    	202	q3	0000-00-00
    the code will display q1. i want to update the 2nd row only to
    Code:
    201	q2	2013-05-23
    so, tomorrow q2 will be displayed and the third
    row will be updated as
    Code:
    202	q3	2013-05-24

    Code:
    <?php
    $today=date("Y/m/d");
    $tomorrow= date("Y-m-d", strtotime("tomorrow"));
    echo "<form method='post' id='submit' action='checkresult.php'>";
    $sql="SELECT * FROM cquestions where showdate= '$today' limit 1";
    $result=mysql_query($sql);
    while ($row = mysql_fetch_array($result)) {
    	$cqid=mysql_result($result,"cqid");
    $update1="update cquestions set showdate='$tomorrow' where showdate='0000-00-00' and cqid!='$cqid' order by cqid limit 1";
    mysql_query($update1);
    
    echo "<p>" . $row['cqtext'] . "</p>";
    $sql2="SELECT * FROM canswers where cqid=".$row['cqid'];
    $result2=mysql_query($sql2);
    while($row2=mysql_fetch_assoc($result2)){
    echo "<input type='radio' name='".$row['cqid']."' value='".$row2['cqans']."' />".$row2['aatext']; }
    /*echo "<input type='hidden' name='email' value='email' />";*/
    }
    echo"<input type='submit' id='submit' name='submit' value='Submit Answers' />";
    echo "</form>";
    ?>
    this code update the next row, but the problem is its executed every time page loads and update the next row... i want to execute the update query only once for the day. how to do it?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Store the last time it was updated and if that was today, don't run the update.

    Comment

    • Luuk
      Recognized Expert Top Contributor
      • Mar 2012
      • 1043

      #3
      or, only run the update if
      "SELECT * FROM cquestions where showdate= '$today'"
      did not return a result
      (which means there is no record for today...)

      Comment

      • impin
        New Member
        • Jul 2010
        • 127

        #4
        i have stored the last_update and checked with the current date. i want to run the update query only once.

        updated code:

        Code:
        $last_update = date("Y/m/d");
        if($last_update==$today)
        {
        $update1="update cquestions set showdate='$tomorrow' where showdate='0000-00-00' and cqid!='$cqid' order by cqid limit 1";
        mysql_query($update1);
        $update2="update qupdate set last_update='$tomorrow'";
        mysql_query($update2);
        $sql3="SELECT * FROM qupdate";
        $result=mysql_query($sql3);
        $last_update=mysql_result($result,"last_update");	
        echo "$last_update";
        }
        last_update was stored in the table ($update2="upda te qupdate set last_update='$t omorrow'";). first its executing correctly but next time last_update was set to today's date. (($last_update = date("Y/m/d");)) its not set to the last updated date. so the if condition executing all the time. how to fix it?
        i want to run the update query only once...

        Comment

        • impin
          New Member
          • Jul 2010
          • 127

          #5
          fixed it. but if i change the limit to 2, it doesn't working. only one row is displaying. first two rows should be displayed...

          Code:
          $sql="SELECT * FROM cquestions where showdate= '$today' limit 1";
          Code:
          $today=date("Y-m-d");
          $sql4="SELECT * FROM qupdate";
          $result=mysql_query($sql4);
          $last_update=mysql_result($result,"last_update");	
          $tomorrow= date("Y-m-d", strtotime("tomorrow"));
          echo "$last_update";
          echo "<form method='post' id='submit' action='checkresult.php'>";
          $sql="SELECT * FROM cquestions where showdate= '$today' limit 1";
          $result=mysql_query($sql);
          while ($row = mysql_fetch_array($result)) {
          $cqid=mysql_result($result,"cqid");	
          if($last_update==$today)
          {
          $update1="update cquestions set showdate='$tomorrow' where showdate='0000-00-00' and cqid!='$cqid' order by cqid limit 1";
          mysql_query($update1);
          $update2="update qupdate set last_update='$tomorrow'";
          mysql_query($update2);
          $sql3="SELECT * FROM qupdate";
          $result=mysql_query($sql3);
          $last_update=mysql_result($result,"last_update");	
          echo "$last_update";
          }

          Comment

          • Luuk
            Recognized Expert Top Contributor
            • Mar 2012
            • 1043

            #6
            It fout want to store the last update date in 'qupdate',
            Than you should 1st check what the currenlty stored date is ("select ... from qupdate "), and compare that date with today

            If the dates are not equal, than you should update 'qupdate' with the date for today.

            After above exercise, you should be able to get the (3) questions for today....

            Comment

            Working...