[Help] Need to work with PHP at practice

malemees

Member
Joined
Jun 22, 2009
Messages
107
Reaction score
22
Got one problem, I get following error on website:
Fatal error: Uncaught exception 'ErrorException' with message 'Get Error' in E:\Servers\XAMPP\htdocs\ebus\Router.php:35 Stack trace: #0 E:\Servers\XAMPP\htdocs\ebus\Router.php(10): Router->handleGet(Array) #1 E:\Servers\XAMPP\htdocs\ebus\index.php(26): Router->handle() #2 {main} thrown in E:\Servers\XAMPP\htdocs\ebus\Router.php on line 35

Router.php:

PHP:
<?php
class Router {

	function __construct() {
	}

	public function handle() {
		$r = array();
		if ($_SERVER['REQUEST_METHOD'] == 'GET') {
			$this->handleGet($r);
			echo json_encode($r);
		} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$this->handlePost($r);
			echo json_encode($r);
		}
	}

	public static function get($obj, $key) {
		if (isset($obj[$key])) {
			return mysql_escape_string($obj[$key]);
		}
		throw new ErrorException();
	}

	public function handleGet(&$r) {
		
		if(isset($_GET['component'])){
			$this->handleComponent($r);
			return;	} 
		elseif(isset($_GET['templates'])){
			$template = new TemplateLoader();
			$template->handle($r);
			return;}
		else{
			throw new ErrorException("Get Error");
		}
		return;
	}
	
	public function handleComponent(&$r) {
		$r['req'] = array();
		$component = self::get($_GET, 'component');
		$r['req']['component'] = $component;
		$r['req']['target'] = self::get($_GET, 'target');
		
		if ($component == 'main') {
			//http://localhost/example/?component=main
			$r["images"] = Main::$data -> get3Images();
			$r["updates"] = array();
			$r["comments"] = array();
			
			$r["template"] = 'main';
			return;
		}
		if ($component == 'depots') {
			//http://localhost/example/?component=depots&location=1
			$location = self::get($_GET, 'location');
			$r['req']['location'] = $location;
			$r["location"] = $location;
			$r["depots"] = array( array("id" => "1", "name" => "park1", ));
			return;
		}
		if ($component == 'depot_buses') {
			//http://localhost/example/?component=depot_buses&depot=1
			$depot = self::get($_GET, 'depot');
			$r['req']['depot'] = $depot;
			
			$r["depot"] = $depot;
			$r["buses"] = array("buses" => array());
			return;
		}
		if ($component == 'search') {
			$r["search"] = array();
			return;
		}
		if ($component == 'bus') {
			$id = self::get($_GET, 'id');
			$r['req']['id'] = $id;
			
			$bus = Main::$data->getBus($id);
			$r['bus'] = $bus;
			
			$images = Main::$data->getBusImages($id);
			$r['images'] = $images;
			$r['template'] = 'bus';
			return;
		}
		if ($component == 'gallery') {
			//http://localhost/example/?component=gallery
			$r['gallery'] = Main::$data->getGallery();
			return;
		}
		if ($component == 'comments') {
			//http://localhost/example/?component=comments
			$r["comments"] = array( array('id' => 1, 'bus' => 1, 'text' => 'text1'), array('id' => 2, 'bus' => 3, 'text' => 'text2'), array('id' => 3, 'bus' => 4, 'text' => 'text3'));
			return;
		}

		if ($component == 'locations') {
			//http://localhost/example/?component=locations
			$r["locations"] = Main::$data -> getLocations();
			$r["template"] = 'locations';
			return;
		}

		throw new ErrorException("component error");
	}

	public function handlePost(&$r) {
		$component = self::get($_POST, 'component');
		$r['req']['component'] = $component;
		$r['req']['target'] = self::get($_POST, 'target');
		if ($component == 'bus') {
			$fields = array(
			"id",
			"location",
			"service_type",
			"depot",
			"model",
			"state",
			"plate_nr",
			"built",
			"page_views",
			);
			
			$bus = array();
			foreach ($fields as $field) {
				$bus[$field] = self::get($_POST, $field);
			}
			$r['req']['bus'] = $bus;
			$id = mysql_escape_string($r['req']['bus']['id']);
			$count = Main::$data->getSingleCell("select count(1) from bus where id = '$id'");
			
			if($count > 1){
				throw new ErrorException("post error");
			}
			
			if($count == 1){
				Main::$data->updateBus($bus);
			}
			
			if($count == 0){
				$id = Main::$data->updateBus($bus,TRUE);
				$r['location'] = "component=bus&target=content&id=$id";
			}
			
			$bus = Main::$data->getBus($id);
			$r['bus'] = $bus;
			
			$images = Main::$data->getBusImages($id);
			$r['images'] = $images;
			
			$r['template'] = 'bus';
			
			return;
		}
		throw new ErrorException("post error");
	}

}
?>

Can you please explain me whats wrong and where I can learn about PHP
 
is this your code or is it a framework?

you have 2 problems:
1. you haven't caught the ErrorException
PHP: set_error_handler - Manual
2. your problem is here:
PHP:
    public function handleGet(&$r) { 
         
        if(isset($_GET['component'])){ 
            $this->handleComponent($r); 
            return;    }  
        elseif(isset($_GET['templates'])){ 
            $template = new TemplateLoader(); 
            $template->handle($r); 
            return;} 
        else{ 
            throw new ErrorException("Get Error"); 
        } 
        return; 
    }
 
This is the correct way
PHP:
<?php 
class Router { 

    function __construct() { 
    } 

    public function handle() {
        try {
            $r = array(); 
            if ($_SERVER['REQUEST_METHOD'] == 'GET') { 
                $this->handleGet($r); 
                echo json_encode($r); 
            } else if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
                $this->handlePost($r); 
                echo json_encode($r); 
            }
        }
        catch (ErrorException $e) {
            echo $e->getMessage();
        }
    } 

    public static function get($obj, $key) { 
        if (isset($obj[$key])) { 
            return mysql_escape_string($obj[$key]); 
        } 
        throw new ErrorException(); 
    } 

    public function handleGet(&$r) { 
         
        if(isset($_GET['component'])){ 
            $this->handleComponent($r); 
            return;    }  
        elseif(isset($_GET['templates'])){ 
            $template = new TemplateLoader(); 
            $template->handle($r); 
            return;} 
        else{ 
            throw new ErrorException("Get Error"); 
        } 
        return; 
    } 
     
    public function handleComponent(&$r) { 
        $r['req'] = array(); 
        $component = self::get($_GET, 'component'); 
        $r['req']['component'] = $component; 
        $r['req']['target'] = self::get($_GET, 'target'); 
         
        if ($component == 'main') { 
            //http://localhost/example/?component=main 
            $r["images"] = Main::$data -> get3Images(); 
            $r["updates"] = array(); 
            $r["comments"] = array(); 
             
            $r["template"] = 'main'; 
            return; 
        } 
        if ($component == 'depots') { 
            //http://localhost/example/?component=depots&location=1 
            $location = self::get($_GET, 'location'); 
            $r['req']['location'] = $location; 
            $r["location"] = $location; 
            $r["depots"] = array( array("id" => "1", "name" => "park1", )); 
            return; 
        } 
        if ($component == 'depot_buses') { 
            //http://localhost/example/?component=depot_buses&depot=1 
            $depot = self::get($_GET, 'depot'); 
            $r['req']['depot'] = $depot; 
             
            $r["depot"] = $depot; 
            $r["buses"] = array("buses" => array()); 
            return; 
        } 
        if ($component == 'search') { 
            $r["search"] = array(); 
            return; 
        } 
        if ($component == 'bus') { 
            $id = self::get($_GET, 'id'); 
            $r['req']['id'] = $id; 
             
            $bus = Main::$data->getBus($id); 
            $r['bus'] = $bus; 
             
            $images = Main::$data->getBusImages($id); 
            $r['images'] = $images; 
            $r['template'] = 'bus'; 
            return; 
        } 
        if ($component == 'gallery') { 
            //http://localhost/example/?component=gallery 
            $r['gallery'] = Main::$data->getGallery(); 
            return; 
        } 
        if ($component == 'comments') { 
            //http://localhost/example/?component=comments 
            $r["comments"] = array( array('id' => 1, 'bus' => 1, 'text' => 'text1'), array('id' => 2, 'bus' => 3, 'text' => 'text2'), array('id' => 3, 'bus' => 4, 'text' => 'text3')); 
            return; 
        } 

        if ($component == 'locations') { 
            //http://localhost/example/?component=locations 
            $r["locations"] = Main::$data -> getLocations(); 
            $r["template"] = 'locations'; 
            return; 
        } 

        throw new ErrorException("component error"); 
    } 

    public function handlePost(&$r) { 
        $component = self::get($_POST, 'component'); 
        $r['req']['component'] = $component; 
        $r['req']['target'] = self::get($_POST, 'target'); 
        if ($component == 'bus') { 
            $fields = array( 
            "id", 
            "location", 
            "service_type", 
            "depot", 
            "model", 
            "state", 
            "plate_nr", 
            "built", 
            "page_views", 
            ); 
             
            $bus = array(); 
            foreach ($fields as $field) { 
                $bus[$field] = self::get($_POST, $field); 
            } 
            $r['req']['bus'] = $bus; 
            $id = mysql_escape_string($r['req']['bus']['id']); 
            $count = Main::$data->getSingleCell("select count(1) from bus where id = '$id'"); 
             
            if($count > 1){ 
                throw new ErrorException("post error"); 
            } 
             
            if($count == 1){ 
                Main::$data->updateBus($bus); 
            } 
             
            if($count == 0){ 
                $id = Main::$data->updateBus($bus,TRUE); 
                $r['location'] = "component=bus&target=content&id=$id"; 
            } 
             
            $bus = Main::$data->getBus($id); 
            $r['bus'] = $bus; 
             
            $images = Main::$data->getBusImages($id); 
            $r['images'] = $images; 
             
            $r['template'] = 'bus'; 
             
            return; 
        } 
        throw new ErrorException("post error"); 
    } 

} 
?>
 
  • Like
Reactions: malemees
Thank you very much! I ain't the autor of this code.
Can you tell me what this php file do ?
 
Last edited:
i found 1 more problem that can give you trouble:

change: mysql_escape_string to mysql_real_escape_string
 
  • Like
Reactions: malemees
This php class is handlig Post and Get requests to your server, loading templates and other things. It is hard to explain what exactly is this class doing because its a small part from some kind of framework.
 
  • Like
Reactions: malemees