Jump to content
MakeWebGames

I need help with my tuning module


andr3as

Recommended Posts

Please copy and paste your code - a screenshot of it can make it harder to debug.

On line 101, you have a variable `$garage` which does not appear to have been defined within that function or passed in via function parameters or global scoping.
The code implies that it's supposed to send an error message if `$garage['GA_exhaust']` doesn't match the DB result, however, the code actually reads as setting an array key within the `$garage` var but doesn't use it.

On line 112, you have another seemingly undefined variable `$value`.

I've split the logics combining non-existent row with user doesn't own. What happens after trying this? (Note: Written in English. Hungarian is not a strong point of mine)

public function method_exhaust() {
    // Get vehicle and adjoining garage data
    $stmt = $this->db->prepare('SELECT * FROM garage 
        INNER JOIN cars ON CA_id = GA_car
        WHERE GA_id = :car
    ');
    $stmt->bindParam(':car', $this->methodData->car);
    $stmt->execute();
    $car = $stmt->fetchObject();
    
    if(empty($car)) { // row not found
        $this->alerts[] = $this->page->buildElement('error', ['text' => 'Car doesn\'t exist']);
    } elseif ($car->GA_uid != $this->user->id) { // row doesn't belong to user
        $this->alerts[] = $this->page->buildElement('error', ['text' => 'Car isn\'t yours']);
    } elseif ($garage['GA_exhaust'] != $this->garage->info->GA_exhaust) { // selected car part doesn't match garage entry
        $this->alerts[] = $this->page->buildElement('error', ['text' => 'Mismatched parts']);
    } elseif ($value > $this->user->info->US_money) { // Not enough money
        $this->alerts[] = $this->page->buildElement('error', ['text', 'Not enough money. $'.number_format($value).' required']);
    } else { // Make purchase
        // Update garage
        $garage = $this->db->prepare('UPDATE garage SET GA_exhaust = GA_exhaust + 100 WHERE GA_id = :id');
        $garage->bindParam(':id', $car->GA_id);
        $garage->execute();
        // Take cost
        $money = $this->db->prepare('UPDATE userStats SET US_money = US_money - 1000000 WHERE US_id = :id');
        $money->bindParam(':id', $this->user->id);
        $money->execute();
        // Hook it
        $actionHook = new hook('userAction');
        $actionHook->run([
            'user' => $this->user->id,
            'module' => 'tuning.exhaust',
            'id' => $car->CA_id,
            'success' => true,
            'reward' => $value,
        ]);
    }
}

 

Link to comment
Share on other sites

I would also remove your elseifs and only use ifs

If(checkforErrorsLine #1)

Return $this->alerts[] line

 

If(checkforErrorsLine #2)

Return $this->alerts[] line

If(checkforErrorsLine #3)

Return $this->alerts[] line

 

Ect:

Then just do this at end

gics combining non-existent row with user doesn't own. What happens after trying this? (Note: Written in English. Hungarian is not a strong point of mine)

public function method_exhaust() {
    // Get vehicle and adjoining garage data
    $stmt = $this->db->prepare('SELECT * FROM garage 
        INNER JOIN cars ON CA_id = GA_car
        WHERE GA_id = :car
    ');
    $stmt->bindParam(':car', $this->methodData->car);
    $stmt->execute();
    $car = $stmt->fetchObject();
    
    if(empty($car)) { // row not found
        Return $this->alerts[] = $this->page->buildElement('error', ['text' => 'Car doesn\'t exist']);
    } 

if ($car->GA_uid != $this->user->id) { // row doesn't belong to user
        Return $this->alerts[] = $this->page->buildElement('error', ['text' => 'Car isn\'t yours']);
    } 

if ($garage['GA_exhaust'] != $this->garage->info->GA_exhaust) { // selected car part doesn't match garage entry
       Return $this->alerts[] = $this->page->buildElement('error', ['text' => 'Mismatched parts']);
    } 

If ($value > $this->user->info->US_money) { // Not enough money
        Return $this->alerts[] = $this->page->buildElement('error', ['text', 'Not enough money. $'.number_format($value).' required']);
    } 

// Make purchase
        // Update garage
        $garage = $this->db->prepare('UPDATE garage SET GA_exhaust = GA_exhaust + 100 WHERE GA_id = :id');
        $garage->bindParam(':id', $car->GA_id);
        $garage->execute();
        // Take cost
        $money = $this->db->prepare('UPDATE userStats SET US_money = US_money - 1000000 WHERE US_id = :id');
        $money->bindParam(':id', $this->user->id);
        $money->execute();
        // Hook it
        $actionHook = new hook('userAction');
        $actionHook->run([
            'user' => $this->user->id,
            'module' => 'tuning.exhaust',
            'id' => $car->CA_id,
            'success' => true,
            'reward' => $value,
        ]);
    

This is how I manage my error handling, it removes a lot of the extra bracket and leaves my logic more tabs aligned left and less nested IFs

  • Like 1
Link to comment
Share on other sites

Yes when I go home from work I gona post here. 

 So I tried your solution for the tuning mode but no working Coming up one error like this, when I try to buy the exhaust for the car .

Sorry something went very wrong!

The error has been logged and waiting for a developer to review this issue.

If you are the developer you can enable better dubuging by editing config.php and enabling debug.

 

 

10 hours ago, Magictallguy said:

Please copy and paste your code - a screenshot of it can make it harder to debug.

On line 101, you have a variable `$garage` which does not appear to have been defined within that function or passed in via function parameters or global scoping.
The code implies that it's supposed to send an error message if `$garage['GA_exhaust']` doesn't match the DB result, however, the code actually reads as setting an array key within the `$garage` var but doesn't use it.

On line 112, you have another seemingly undefined variable `$value`.

I've split the logics combining non-existent row with user doesn't own. What happens after trying this? (Note: Written in English. Hungarian is not a strong point of mine)

public function method_exhaust() { // Get vehicle and adjoining garage data $stmt = $this->db->prepare('SELECT * FROM garage INNER JOIN cars ON CA_id = GA_car WHERE GA_id = :car '); $stmt->bindParam(':car', $this->methodData->car); $stmt->execute(); $car = $stmt->fetchObject(); if(empty($car)) { // row not found $this->alerts[] = $this->page->buildElement('error', ['text' => 'Car doesn\'t exist']); } elseif ($car->GA_uid != $this->user->id) { // row doesn't belong to user $this->alerts[] = $this->page->buildElement('error', ['text' => 'Car isn\'t yours']); } elseif ($garage['GA_exhaust'] != $this->garage->info->GA_exhaust) { // selected car part doesn't match garage entry $this->alerts[] = $this->page->buildElement('error', ['text' => 'Mismatched parts']); } elseif ($value > $this->user->info->US_money) { // Not enough money $this->alerts[] = $this->page->buildElement('error', ['text', 'Not enough money. $'.number_format($value).' required']); } else { // Make purchase // Update garage $garage = $this->db->prepare('UPDATE garage SET GA_exhaust = GA_exhaust + 100 WHERE GA_id = :id'); $garage->bindParam(':id', $car->GA_id); $garage->execute(); // Take cost $money = $this->db->prepare('UPDATE userStats SET US_money = US_money - 1000000 WHERE US_id = :id'); $money->bindParam(':id', $this->user->id); $money->execute(); // Hook it $actionHook = new hook('userAction'); $actionHook->run([ 'user' => $this->user->id, 'module' => 'tuning.exhaust', 'id' => $car->CA_id, 'success' => true, 'reward' => $value, ]); } }


public function method_exhaust() {
    // Get vehicle and adjoining garage data
    $stmt = $this->db->prepare('SELECT * FROM garage 
        INNER JOIN cars ON CA_id = GA_car
        WHERE GA_id = :car
    ');
    $stmt->bindParam(':car', $this->methodData->car);
    $stmt->execute();
    $car = $stmt->fetchObject();
    
    if(empty($car)) { // row not found
        $this->alerts[] = $this->page->buildElement('error', ['text' => 'Car doesn\'t exist']);
    } elseif ($car->GA_uid != $this->user->id) { // row doesn't belong to user
        $this->alerts[] = $this->page->buildElement('error', ['text' => 'Car isn\'t yours']);
    } elseif ($garage['GA_exhaust'] != $this->garage->info->GA_exhaust) { // selected car part doesn't match garage entry
        $this->alerts[] = $this->page->buildElement('error', ['text' => 'Mismatched parts']);
    } elseif ($value > $this->user->info->US_money) { // Not enough money
        $this->alerts[] = $this->page->buildElement('error', ['text', 'Not enough money. $'.number_format($value).' required']);
    } else { // Make purchase
        // Update garage
        $garage = $this->db->prepare('UPDATE garage SET GA_exhaust = GA_exhaust + 100 WHERE GA_id = :id');
        $garage->bindParam(':id', $car->GA_id);
        $garage->execute();
        // Take cost
        $money = $this->db->prepare('UPDATE userStats SET US_money = US_money - 1000000 WHERE US_id = :id');
        $money->bindParam(':id', $this->user->id);
        $money->execute();
        // Hook it
        $actionHook = new hook('userAction');
        $actionHook->run([
            'user' => $this->user->id,
            'module' => 'tuning.exhaust',
            'id' => $car->CA_id,
            'success' => true,
            'reward' => $value,
        ]);
    }
}

 

 

Sim I try your solution as well but also no working.

9 hours ago, Sim said:

I would also remove your elseifs and only use ifs

If(checkforErrorsLine #1)

Return $this->alerts[] line

 

If(checkforErrorsLine #2)

Return $this->alerts[] line

If(checkforErrorsLine #3)

Return $this->alerts[] line

 

Ect:

Then just do this at end

gics combining non-existent row with user doesn't own. What happens after trying this? (Note: Written in English. Hungarian is not a strong point of mine)

 


public function method_exhaust() {
    // Get vehicle and adjoining garage data
    $stmt = $this->db->prepare('SELECT * FROM garage 
        INNER JOIN cars ON CA_id = GA_car
        WHERE GA_id = :car
    ');
    $stmt->bindParam(':car', $this->methodData->car);
    $stmt->execute();
    $car = $stmt->fetchObject();
    
    if(empty($car)) { // row not found
        Return $this->alerts[] = $this->page->buildElement('error', ['text' => 'Car doesn\'t exist']);
    } 

if ($car->GA_uid != $this->user->id) { // row doesn't belong to user
        Return $this->alerts[] = $this->page->buildElement('error', ['text' => 'Car isn\'t yours']);
    } 

if ($garage['GA_exhaust'] != $this->garage->info->GA_exhaust) { // selected car part doesn't match garage entry
       Return $this->alerts[] = $this->page->buildElement('error', ['text' => 'Mismatched parts']);
    } 

If ($value > $this->user->info->US_money) { // Not enough money
        Return $this->alerts[] = $this->page->buildElement('error', ['text', 'Not enough money. $'.number_format($value).' required']);
    } 

// Make purchase
        // Update garage
        $garage = $this->db->prepare('UPDATE garage SET GA_exhaust = GA_exhaust + 100 WHERE GA_id = :id');
        $garage->bindParam(':id', $car->GA_id);
        $garage->execute();
        // Take cost
        $money = $this->db->prepare('UPDATE userStats SET US_money = US_money - 1000000 WHERE US_id = :id');
        $money->bindParam(':id', $this->user->id);
        $money->execute();
        // Hook it
        $actionHook = new hook('userAction');
        $actionHook->run([
            'user' => $this->user->id,
            'module' => 'tuning.exhaust',
            'id' => $car->CA_id,
            'success' => true,
            'reward' => $value,
        ]);
    

This is how I manage my error handling, it removes a lot of the extra bracket and leaves my logic more tabs aligned left and less nested IFs

 

Sorry something went very wrong!

The error has been logged and waiting for a developer to review this issue.

If you are the developer you can enable better dubuging by editing config.php and enabling debug

With this solution working now for me. I know its not the best solution.Now I just need to add to don't let peoples to buy more than 1 tuning part for each car.

 

public function method_exhaust() {

    $car = $this->db->prepare("SELECT * FROM garage INNER JOIN cars ON (CA_id = GA_car) WHERE GA_id = :car");
    $car->bindParam(':car', $this->methodData->car);
    $car->execute();
    $car = $car->fetchObject();
 
    if (empty($car) || $car->GA_uid != $this->user->id) {
        $this->alerts[] = $this->page->buildElement('error', array("text"=>'Car doesnt exist'));
    }  else if ($this->user->info->US_money <= 0) {
        $this->alerts[] = $this->page->buildElement('error', array("text" => "Not enough money!"));
    } else if ($this->garage->info->GA_exhaust == 100) {
        $this->alerts[] = $this->page->buildElement('error', array("text" => "You all ready have this"));
    } else {
        if ($value < $this->user->info->US_money) {
            $this->alerts[] = $this->page->buildElement('success', array("text"=>'You buyed exhaust tuning.'));
            $this->db->query("UPDATE garage SET GA_exhaust = GA_exhaust + 100 WHERE GA_id = ".$car->GA_id);
            $this->db->query("UPDATE userStats SET US_money = US_money - 1000000 WHERE US_id = ".$this->user->id);
 
            $actionHook = new hook("userAction");

            $action = array(
                "user" => $this->user->id,
                "module" => "tuning.exhaust",
                "id" => $car->CA_id,
                "success" => true,
                "reward" => $value
            );
 
            $actionHook->run($action);
        }
    }
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...