Skip to content
Advertisement

Leads to HTTP ERROR 500 PHP and SQL and i can´t solve it

I m trying to show on a page {"status":1} but instead it gives an error 500. All my code it seems to be working on database and on the plataform.

It gives this errors on error log:

  1. [error] 42094#0: *233568 FastCGI sent in stderr: “PHP message: PHP Notice: Undefined index: id in /var/www/…/…/…/…/classes/pageuser.class.php on line 519
  2. PHP message: PHP Fatal error: Call to a member function execute() on boolean in /var/www/…/…/…/…/classes/loan.class.php on line 537″ while reading response header from upstream, client: 162…, server: interno.neeec.pt, request: “GET /loans/tickets/pay HTTP/1.1”, upstream: “fastcgi://unix:/var/lib/php5-fpm/web20.sock:”, host: “interno.neeec.pt”

On pageuser.class.php:

 case "loans/tickets/pay":
            $this->template = "no_render";
            $response = null;
            if (Loan::payTicket($_POST["id"], $user->getID())) {
                $response = ["status" => 1];
            } else {
                $response = ["status" => 0, "err" => "Erro ao atualizar base de dados"];
            }
            die(json_encode($response));
            return;

On loan.class.php:

    public static function payTicket($ticket_id, $user_id)
{
    $ticket_value = Loan::getTicketValue($ticket_id);
    $database = new Database();
    $connection = $database->getConnection();
    $stmt = $connection->prepare("UPDATE loan_tickets SET `paid`=1,date_of_payment=CURRENT_TIMESTAMP WHERE id=?");
    $stmt->bind_param('i', $ticket_id);
    if ($stmt->execute()) {
        $stmt = $connection->prepare("UPDATE `c3interno`.`payment_methods` SET `expected_value` = `expected_value` + $ticket_value WHERE `id` = 6");
        if($stmt->execute())
        {
            $stmt = $connection->prepare("INSERT INTO `c3interno`.`payment_methods_logs` (`payment_method`,`type`,`value`,`user_id`,`date`,`reason`) VALUES (6,5,$ticket_value,$user_id,CURRENT_TIMESTAMP,'Delay')");
            if($stmt->execute())
            {
                $stmt->close();
                return true;
            }
            $stmt->close();
            return false;
        }
        $stmt->close();
        return false;
    }
    $stmt->close();
    return false;
}

Advertisement

Answer

You just need to do some basic error checking. The first warning and probably the root cause is that the $_POST variable “id” was not sent. I have no idea why, you would need to investigate. If this is an optional variable, something like if (isset($_POST["id"])) should be used to see if it exists before trying to use it.

The second error occurs because $stmt is false due to prepare() returning a failure state. See about PDO return values. You need to check for errors with something like if ($stmt === false).

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement