We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29479
    • 4 Posts
    I am trying to write a cron to update custom table.
    The table has coupon code, status, start, end fields.
    The status can be unused, used and expired.
    Now there is a time limit to use the coupon. If the coupon is not used within 7 days, I want to set the status to expired.
    I am trying to update the rows where status is unused and end is less than today date, and set status as expired.
    When I run the script, the status for all rows is set as expired, regardless of date and status.

    Here is the code

    <?php
    $format="Y-m-d";
    $date=date($format);
    $old_status = "unused";
    //echo("$old_status");
    $new_status = "expired";
    //echo("$new_status");
    $table = $modx->getFullTableName( 'coupon' );
    $fields = array('status'	=> $new_status );
    $result = $modx->db->update( $fields, $table,  'status = "' . $old_status . '"' AND 'end < "' . $date . '"' );
    if(!$result)
    {
    echo mysql_error ();
    } 
    else{
    echo 'ok done';
    }
    ?>


    Where is the problem?
      • 29479
      • 4 Posts
      ok, seems i am one of those who make a post and then finds the problem within minutes.
      If it helps someone, the where statement must have single quotes only at the start and end, not inbetween.
      So this is wrong
      'status = "' . $old_status . '"' AND 'end < "' . $date . '"'

      and the correct way is
      'status = "' . $old_status . '" AND end < "' . $date . '"'