user3517652
user3517652

Reputation: 94

Why my update function cannot update record?

I called my update sql every time run the php file and it return true statement but record cannot update perfectly. I want to know that where my code goes wrong? Please help me and I will appreciate it. Thanks in advance.

This is my php code in event-listing.php:

$update_event_list = $event->updateeventlist($type = 1);

This is my sql statement in Event.inc.php :

function updateeventlist($type){
            global $db;

            $stmt = "SELECT * FROM "._CONST_TBL_EVENT." WHERE type = ".$type;

            if($rs = $db->Execute($stmt)){
                while($rsa = $rs->FetchRow())
                {
                    if($rsa['start_date'] < strtotime("now")){
                        $updateEvent = "UPDATE "._CONST_TBL_EVENT." SET type = 2 WHERE id = ".$rsa['id'];
                    }
                }
            }
            return true;
        }

I have tried to echo out the statement and it return true statement that I want.

Upvotes: 0

Views: 59

Answers (3)

Purushottam zende
Purushottam zende

Reputation: 552

Below are some points that i observed in your code:-

  1. You are not executing the update query. You are just making the query as string but not executing.
  2. Even if you none of the records is updated or fetched you still get "true", because there is no condition to specify when to return false if it fails.

        $stmt = "SELECT * FROM "._CONST_TBL_EVENT." WHERE type = ".$type;
    
        if($rs = $db->Execute($stmt))
        {
         if( $rs has atleast one row rows )
          {
            while($rsa = $rs->FetchRow())
            {
                if($rsa['start_date'] < strtotime("now")){
                    $updateEvent = "UPDATE "._CONST_TBL_EVENT." SET type = 2 WHERE id = ".$rsa['id'];
                    $db->Execute($updateEvent); // this line was missing in you code
                }
            }
          }
          else
         {
                return false;
                 // $rsa has empty rows
          }
    
        }
        else // execution of query fails for any reason
        {
           return false;
        }
    

Upvotes: 1

Padmanathan J
Padmanathan J

Reputation: 4620

Query execution missing after your update Query

function updateeventlist($type){
            global $db;

            $stmt = "SELECT * FROM "._CONST_TBL_EVENT." WHERE type = ".$type;

            if($rs = $db->Execute($stmt)){
                while($rsa = $rs->FetchRow())
                {
                    if($rsa['start_date'] < strtotime("now")){
                        $updateEvent = "UPDATE "._CONST_TBL_EVENT." SET type = 2 WHERE id = ".$rsa['id'];
                        $db->Execute($updateEvent);
                    }
                }
            }
            return true;
        }

Upvotes: 1

Krish R
Krish R

Reputation: 22711

You need to add execute function after the update query.

$rs = $db->Execute($updateEvent);

Upvotes: 2

Related Questions