douxian1895 2019-05-01 16:22
浏览 48
已采纳

INSERT INTO不会填满所有行

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条回答 默认 最新

  • dongqu1783 2019-05-01 16:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!