Buggy PDO function

I picked up a neath little pdo function on a tutorial from phpacademy that works sometime but sometime it doesn’t. I was wondering if anyone could see a reason why? When I test the function on the same page or on some of my pages with a redirect it works great, but other times it simply doesn’t work. I commented the 'self::delete($name) and tried it again and it works fine and echo as it should on the redirected page but it doesn’t go away after the page refresh. ’

public static function flash($name, $string=''){
		if(self::exists($name)){
			$session = self::get($name);
			self::delete($name);
			return $session;
			}else{
				self::put($name, $string);
				}
				
		}

that doesn’t look like a PDO issue at all. anyways, if you commented out the delete, then the message stays in the session and you have it on each reload.

But the whole point of this “flash” function is to flash a message once and then disappear after the page is updated or refreshed by an event. If I keep the session alive the message echo on everypage where I have a check if the session exist “which I do not want”

For displaying system messages, this is the class that I’m using, part based on a class I found somewhere else:

<?php

class sys_message {
    
    public function __construct($db) {
        $this->sess_id    = session_id();
        $this->db        = $db;
    }
    
    public function add_sys_message($type,$title,$message,$redirect) {
        try {
            $sql="
                INSERT INTO
                    ue_user_system_message
                    (
                          message_type
                        , message_title
                        , message_content
                        , session
                    )
                VALUES
                    (
                          :message_type
                        , :message_title
                        , :message_content
                        , :sess_id
                    )
            ";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':message_type', $type);
            $stmt->bindParam(':message_title', $title);
            $stmt->bindParam(':message_content', $message);
            $stmt->bindParam(':sess_id', $this->sess_id);
            $stmt->execute();

            if (!empty($redirect)) {
                header("Location: $redirect");
                exit;
            }
            return true;
        }
        catch (PDOException $e) {
            echo $e->getMessage();
            error_log('Error adding a system message for the user');
            error_log(' Query with error: '.$sql);
            error_log(' Reason given:'.$e->getMessage()."\n");
            return false;
        }
    }
    
    public function get_user_messages() {
        $all_user_messages=array();
        $user_messages=array();
        try {
            $sql="
                  SELECT
                        message_type
                      , message_title
                      , message_content
                  FROM
                      ue_user_system_message
                  WHERE
                      session = :sess_id
            ";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':sess_id', $this->sess_id);
            $stmt->execute();
            $all_user_messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
            foreach ($all_user_messages AS $message ) {
                
                if ( $message['message_type'] === 'e' ) {
                    $user_messages['error'][] = $message;
                }
                if ( $message['message_type'] === 'w' ) {
                    $user_messages['warning'][] = $message;
                }
                if ( $message['message_type'] === 'i' ) {
                    $user_messages['info'][] = $message;
                }
                if ( $message['message_type'] === 's' ) {
                    $user_messages['success'][] = $message;
                }
                if ( $message['message_type'] === 'h' ) {
                    $user_messages['help'][] = $message;
                }
            }
            $this->delete_user_messages();
            return $user_messages;
        }
        catch (PDOException $e) {
            echo $e->getMessage();
            error_log('Error getting the system messages for the user');
            error_log(' Query with error: '.$sql);
            error_log(' Reason given:'.$e->getMessage()."\n");
        }
        return;
    }
    
    public function delete_user_messages() {
        try {
            $sql="
                DELETE FROM
                      ue_user_system_message
                  WHERE
                      session = :sess_id
              ";
              $stmt = $this->db->prepare($sql);
              $stmt->bindParam(':sess_id', $this->sess_id);
              $stmt->execute();
        }
        catch (PDOException $e) {
            echo $e->getMessage();
            error_log('Error deleteing the system messages for the user');
            error_log(' Query with error: '.$sql);
            error_log(' Reason given:'.$e->getMessage()."\n");
        }
        return;
    }
    
    public  function display_user_messages() {
        $user_messages = $this->get_user_messages();
        
        if (!empty($user_messages['info'])) {
            $ib = "<div class='sysmessage_notification'>";
            $ib     .=    '<img src="http://localhost/universal_empires/public/icons/emblem-important.png" style="float:left" alt="" width="18" height="18"/>';

            foreach ( $user_messages['info'] AS $message ) {
                
                $ib    .= "<p><b>{$message['message_title']}</b> {$message['message_content']}</p>";                
            }
            $ib     .=    "</div>\n";                      
        } else {
            $ib='';
        }

        if (!empty($user_messages['warning'])) {
            $wb = "<div class='sysmessage_notification'>";
            $wb     .=    '<img src="http://localhost/universal_empires/public/icons/dialog-warning.png" style="float:left" alt="" width="18" height="18"/>';

            foreach ( $user_messages['warning'] AS $message ) {
                $wb    .= "<p><b>{$message['message_title']}</b> {$message['message_content']}</p>";                
            }
            $wb     .=    "</div>\n";                      
        } else {
            $wb='';
        }
        
        if (!empty($user_messages['success'])) {
            $sb = "<div class='sysmessage_actionsucess'>";
            $sb     .=    '<img src="http://localhost/universal_empires/public/icons/dialog-information.png" style="float:left" alt="" width="18" height="18"/>';

            foreach ( $user_messages['success'] AS $message ) {
                $sb    .= "<p><b>{$message['message_title']}</b> {$message['message_content']}</p>";                
            }
            $sb     .=    "</div>\n";                      
        } else {
            $sb='';
        }
        
        if (!empty($user_messages['error'])) {
            $eb = "<div class='sysmessage_error'>";
            $eb     .=    '<img src="http://localhost/universal_empires/public/icons/dialog-warning.png" style="float:left" alt="" width="18" height="18"/>';

            foreach ( $user_messages['error'] AS $message ) {
                $eb    .= "<p><b>{$message['message_title']}</b> {$message['message_content']}</p>";                
            }
            $eb     .=    "</div>\n";                      
        } else {
            $eb='';
        }
        
        if (!empty($user_messages['help'])) {
            $hb = "<div class='sysmessage_help'>";
            $hb     .=    '<img src="http://localhost/universal_empires/public/icons/help-browser.png" style="float:left" alt="" width="18" height="18"/>';

            foreach ( $user_messages['help'] AS $message ) {
                $hb    .= "<p><b>{$message['message_title']}</b> {$message['message_content']}</p>";                
            }
            $hb     .=    "</div>\n";                      
        } else {
            $hb='';
        }
        $sys_message_bars    = $ib.$wb.$sb.$eb.$hb;
        
        return $sys_message_bars;
    }
}
?>

The CSS that I use for it is:

/* System Messages */
#sys_messages {
    float: left;
    width: 100%;
    /*margin-top: 100px;*/
    /*padding-top: 10px;*/
}

.sysmessage_error {
    text-align: left;
    color: #ff2828;
    padding: 3px 3px 3px 22px;
    background-color: #330000;
    border: 1px solid #CC0000;
    margin-top: 5px;
    /*font-size: 12px;*/
    margin-bottom: 5px;
}

.sysmessage_error b {
    text-align: left;
    color: #ff2828;

    background-color: #330000;
    margin-top: 5px;
    /*font-size: 12px;*/
    margin-bottom: 5px;
}

.sysmessage_notification {
    text-align: left;
    color: #FF7F39;
    padding: 3px 3px 3px 22px;
    background-color: #341302; 
    border: 1px solid #CC6600;
    margin-top: 5px;
/*    font-size: 12px;*/
    margin-bottom: 5px;
}

.sysmessage_notification b {
    text-align: left;
    color: #FF7F39;
    background-color: #341302; 
    margin-top: 5px;
/*    font-size: 12px;*/
    margin-bottom: 5px;
}

.sysmessage_actionsucess {
    text-align: left;
    color: #00da00;
    padding: 3px 3px 3px 22px;
    background-color: #003000;
    border: 1px solid #00da00;
    /*font-size: 12px;    */
    margin-top: 5px;
    margin-bottom: 5px;
}

.sysmessage_actionsucess b {
    text-align: left;
    color: #00da00;
    background-color: #003000;
    /*font-size: 12px;    */
    margin-top: 5px;
    margin-bottom: 5px;
}

.sysmessage_help {
    text-align: left;
    padding: 3px 3px 3px 22px;
    background-color: #000040;
    border: 1px solid #30a7d7;
    /*font color: #000040;*/
    margin-top: 5px;
    margin-bottom: 5px;
    /*font-size: 12px;    */
    color: #30a7d7;
}

The contrasts probably need tweaking to improve accessibility, the icons are just ones that I found somewhere I’d have to make new icons for when the site goes live

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.