We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Error on inserting a new data

Hello, i have a quite weird error when doing a data insertion. The error is only occured on this section, when i do insert in other functions, it works fine.

Here's the bugged function:

public function checkoutAction(){
        $cartID = $this->request->getQuery("id");

        $mHTrans = new htransaction();
        $sql2 = "INSERT INTO htransaction (cartID) VALUES ('$cartID')";
        $mHTrans->getModelsManager()->executeQuery($sql2);
}

This will result in Notice: Cannot use a scalar value as an array in phalcon/mvc/model/query.zep on line 1823 in (my file) on line 119 Unknown expression error.

Meanwhile, when i change the sql into INSERT INTO htransaction (cartID, financeStatus) VALUES ('$cartID', 'unconfirmed'), the error becomes Fatal error: Wrong parameters for Exception([string $exception [, long $code [, Exception $previous = NULL]]]) in (my file) on line 119

Here's the htransaction Model :

class htransaction extends Model{

    public $hTransID;
    public $cartID;
    public $adminID;
    public $dateAdded;
    public $financeStatus;

    public function initialize(){
        $this->belongsTo("cartID", "cart", "cartID");
        $this->belongsTo("adminID", "admin", "adminID");
    }
}

Thanks in advance :D



58.4k

Refer to https://docs.phalcon.io/en/latest/reference/phql.html#inserting-data or tried

        $sql2 = "INSERT INTO htransaction (cartID) VALUES ('" . $cartID . " ')";


6.6k

Refer to https://docs.phalcon.io/en/latest/reference/phql.html#inserting-data or tried

       $sql2 = "INSERT INTO htransaction (cartID) VALUES ('" . $cartID . " ')";

Nope, it doesn't work. Still giving the same error. I have try using placeholders but to no avail also.

Does this work?

public function checkoutAction(){
        $cartID = $this->request->getQuery("id");

        $mHTrans = new htransaction();
        $sql2 = "INSERT INTO htransaction (cartID) VALUES (?0)";
        $mHTrans->getModelsManager()->executeQuery($sql2, [$cartID]);
}


6.6k

Does this work?

public function checkoutAction(){
       $cartID = $this->request->getQuery("id");

       $mHTrans = new htransaction();
       $sql2 = "INSERT INTO htransaction (cartID) VALUES (?0)";
       $mHTrans->getModelsManager()->executeQuery($sql2, [$cartID]);
}

Sadly also no :x