0

I have a problem with my code, it is PayPal express Integration. I mean in its original state after some tweaking to match my database and so on it works, it fills rows with ex. payerID and so on.

I have in my database 2 tables - first is Products it has 5 Rows, like productID, Price, currency and so on, but I need 1 extra row - let's say we call it Credits. I manually added it to my database, filled Table Products with data.

Then I have another table called Orders- where I added Row called Credits too, than after successful payment+checkout matched Credits to chosen Product it should fill this Row Credits in Orders table. The problem that I have it that Order Table is filled with all data except that last Row -> Credits it's always showing NULL in the database.

This is part of my code:

    public function getAllProducts()
    {
        $db = getDB();
        $stmt = $db->prepare("SELECT * FROM products");
        $stmt->bindParam("pid", $pid, PDO::PARAM_INT) ;
        $stmt->execute();
        $data = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db=null;
        return $data;

    }

    public function getProduct($pid)
    {
        $db = getDB();
        $stmt = $db->prepare("SELECT * FROM products WHERE pid=:pid");
        $stmt->bindParam("pid", $pid, PDO::PARAM_INT) ;
        $stmt->execute();
        $data = $stmt->fetch(PDO::FETCH_OBJ);
        $db=null;
        return $data;

    }

and this where I have a problem

    public function orders()
    {
        $id = $_SESSION['session_id'];
        $db = getDB();
        $stmt = $db->prepare("SELECT P.product, P.price, P.product_img, P.currency, P.credits, O.created, O.oid  FROM orders O, products P WHERE O.id_fk=:id AND P.pid = O.pid_fk ORDER BY O.created DESC");
        $stmt->bindParam("id", $id, PDO::PARAM_INT) ;
        $stmt->execute();
        $data = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db=null;
        return $data;

    }

    public function updateOrder($pid, $payerID, $paymentID, $token, $credits)
    {
        $id = $_SESSION['session_id'];
        if($this->pyamentCheck($paymentID) < 1 && $id > 0){
        $db = getDB();

        $stmt = $db->prepare("INSERT INTO orders (id_fk, pid_fk, payerID, paymentID, token, created, credits) VALUES (:id, :pid, :payerID, :paymentID, :token, :created, :credits)");
        $stmt->bindParam("paymentID", $paymentID, PDO::PARAM_STR) ;
        $stmt->bindParam("payerID", $payerID, PDO::PARAM_STR) ;
        $stmt->bindParam("token", $token, PDO::PARAM_STR) ;
        $stmt->bindParam("pid", $pid, PDO::PARAM_INT) ;
        $stmt->bindParam("id", $id, PDO::PARAM_INT) ;
        $created = time();
        $stmt->bindParam("created", $created, PDO::PARAM_INT) ;
        $stmt->bindParam(":credits", $credits, PDO::PARAM_INT) ;

        $stmt->execute();
        $db=null;
        return true;

        }
        else{
            return false;
        }

    }

I can´t figure it out.

1 Answers1

0

This is quite a frequent question so it deserves an answer, however obvious it may seem.

Once you have a working INSERT query and one of columns turns to be NULL, it means that the source variable contained NULL.

So the problem is neither with PDO, nor prepared statements, nor a database but simply with your source variable, $credits. You have to check your code related to this variable and make sure it does contain the desired value.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); does not show any errors, i tryied change in code name of Table and then it throw exception error, when i put right name of Table, it shows nothing i mean no error. Still in DB last row is NULL – Kamil Wisniewski May 01 '19 at 17:01
  • @KamilWisniewski Maybe give `$credits` a default value in your function definition, `$credits = 0)` or debug where you call the function and what the variable is there. – user3783243 May 01 '19 at 17:28
  • Maybe i didnt wrote all info. I cannot put $Credit = 0 or 100 or 1000, becouse its paypal express checkout. If User choose for ex. Product 1 it has in DB amount of Credits 100, if he choose Product 2 it has 200 and so on. So after he choose product he is redirected to paypal chceckout with session_id and pid-> product id. When he finish payment he should get those Credits with reference to this Session and pid. All Infos are filled into Order tables in DB except this additional one Credits. In Products table i filled Credits row with amounts. It still fill that last with NULL in Order table – Kamil Wisniewski May 01 '19 at 18:31
  • @KamilWisniewski you should work further on your code. This is, actually what all programmers do. There is very little we can help you with, having no your code, no you database, no you paypal account. You have got an answer to the question why Credits column (it is called a column, not a row by the way) is null. Why your $credits variable is null is your job to find. – Your Common Sense May 01 '19 at 18:35