Results 1 to 2 of 2

Thread: need help w/date compare

  1. #1
    Join Date
    May 2014
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help w/date compare

    Hi, I just want to update a database
    table column (datepaid-type is DATE(YYYY-MM-DD) to the current date
    IF the duedate >= the current date, payrec = 'P', status = 'Y'and datepaid is ' '. I appreciate that my coding is dismal, sorry. Any help?

    PHP Code:
    [B]<?php   
        $hostname 
    "localhost";
        
    $database "homedb";
        
    $username "root";
        
    $password "cookie";
     try
     {
    $dbconn = new PDO("mysql:host=$hostname;dbname=$database"$username

    $password);
            
    $dbconn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
            
    $dbconn->exec('SET NAMES "utf8"');
     }
     catch (
    PDOException $e)
     {
            
    $error "Unable to connect to the database server.";
            
    // Display error to user
            
    exit();
     }
    $acctno="accto"$payrec="payrec"$status="status";
    $duedate="duedate"$datepaid="datepaid";
    if(
    $duedate == 'CURDATE')
    try
     {
            
    $query $dbconn->prepare("
                UPDATE testbl SET 
    $datepaid = CURDATE()
                WHERE 
                    payrec = 'P' AND status = 'Y' AND datepaid IS NULL
            "
    );
    $query->execute(array(
                
    ":acctno" => $_POST['acctno'],
                
    ":payrec" => $_POST['payrec'],
                
    ":status" => $_POST['status'],
                
    ":duedate" => $_POST["duedate"],
                
    ":datepaid" => $_POST["datepaid"]
            ));
     }
    catch(
    PDOException $e)
     {
            
    $error "Error retrieving user: ".$e->getMessage();
            
    // Display error to user
            
    exit();
     }
    ?>[/B]

  2. #2
    Join Date
    May 2014
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi, I have checked the validity of the table and in
    all 3 records "recur" ='Y', "payrec" ='p', values are in "duedate",
    "datepaid" (type is DATE) and "periodic". I'm looping thru the data; var_dump() displays NULL NULL NULL, It seems that my DATEDIFF is flawed.
    I've spent considerable time viewing forums, manuals, code types. How about some advice?
    Code:
    <?php
    error_reporting(E_ALL ^ E_NOTICE);
    // error_reporting(0);
    $servername = "localhost"; $username = "root";
    $password = "cookie"; $dbname = "test";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error)
       { die("Connection failed: " . $conn->connect_error); }
    // ==================================================
    $sql = "SELECT recur, periodic, pd, payrec, duedate, datepaid,
    DATEDIFF(CURDATE(),duedate) AS dayslate
    FROM testbl WHERE recur = 'Y' && payrec = 'P'";
    $result = $conn->query($sql);
    if ($result->num_rows > 0)
     {
        // output data of each row
        while($row = $result->fetch_assoc()) // ****3 records *****
     {
        // ***************************************************
        var_dump($dayslate); // NULL NULL NULL 
       // ***************************************************
        if ($dayslate > 0)
     { 
        if($dayslate > 120)
        {$pastdue = "PAST DUE";}
    
        if($periodic == 1)
               { $duedate = date('Y-m-d', strtotime('+4 week')) ."\n"; }
        if($periodic == 6)
               { $duedate = date('Y-m-d', strtotime('+25 week')) ."\n"; }
    $pd = 'P'; $daylate = 0;
    // ==================================================
    $sql = "UPDATE testbl SET
            pd = '$pd',
       duedate = '$duedate',                      
     $datepaid = 'NOW()',
      dayslate = '$dayslate'
     WHERE dayslate = 0";
    if ($conn->query($sql) === TRUE)
        { echo "Record updated successfully"; } 
        else
        { echo "Error updating record: " . $conn->error; }
    
    $conn->close(); 
     }
      }
     }
     // header( "refresh:3;url='http://localhost/invoice/autolist.php'");
    ?>

Similar Threads

  1. Compare 2 Arrays
    By locbtran in forum JavaScript
    Replies: 1
    Last Post: 12-16-2011, 05:12 AM
  2. Replies: 0
    Last Post: 12-01-2010, 08:08 AM
  3. Replies: 4
    Last Post: 07-04-2010, 10:51 PM
  4. Compare Variable Sets: all in set 1 against all in set 2?
    By JAB Creations in forum JavaScript
    Replies: 1
    Last Post: 06-13-2008, 07:03 PM
  5. Compare input fields
    By mdconnor in forum JavaScript
    Replies: 2
    Last Post: 01-05-2007, 04:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •