You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter集成PayPal时获取item_idr字段提示未定义索引错误

问题:CodeIgniter新增字段item_idr时出现Undefined index错误

我在CodeIgniter项目里新增了字段item_idr,打算把它的值存入数据库的itemid列,但按照现有字段的处理方式操作后,出现了Undefined index: item_idr的错误。

错误详情

  • 严重程度:通知
  • 消息:未定义索引:item_idr
  • 文件名:controllers/Paypal.php
  • 行号:20
  • 回溯信息:

    文件:C:\laragon\www\web\application\controllers\Paypal.php
    行:20
    函数:_error_handler
    文件:C:\laragon\www\web\index.php
    行:315
    函数:require_once

相关代码

模型代码

function __construct() {
    $this->proTable   = 'web_store';
    $this->transTable = 'purchases_users';
    $this->GameRewards = 'users_items';
}

/*
 * Fetch products data from the database
 * @param id returns a single record if specified, otherwise all records
 */
public function getRows($id = ''){
    $this->db->select('*');
    $this->db->from($this->proTable);
    $this->db->where('status', '1');
    if($id){
        $this->db->where('id', $id);
        $query = $this->db->get();
        $result = $query->row_array();
    }else{
        $this->db->order_by('Title', 'asc');
        $query = $this->db->get();
        $result = $query->result_array();
    }
    
    // return fetched data
    return !empty($result)?$result:false;
}

/*
 * Insert data in the database
 * @param data array
 */
public function insertTransaction($data){
    $insert = $this->db->insert($this->transTable,$data);
    return $insert?true:false;
}

/*
 * Insert data in the database
 * @param data array
 */
public function insertGameRewards($data){
    $insert = $this->db->insert($this->GameRewards,$data);
    return $insert?true:false;
}

PayPal控制器代码

function  __construct(){
    parent::__construct();
    $this->load->library('paypal_lib');
    $this->load->model('product');
}
 
function success(){
    // Get user session (is it logged in the webpage)
    $Session = $this->session->userdata("Player_Logged");
    
    if($Session) {
    // Get the transaction data
    $paypalInfo = $this->input->post();
    $data['item_name']= $paypalInfo['item_name'];
    $data['item_number']= $paypalInfo['item_number'];
    $data['item_idr']= $paypalInfo['item_idr'];
    $data['txn_id'] = $paypalInfo["txn_id"];
    $data['payment_amt'] = $paypalInfo["payment_gross"];
    $data['currency_code'] = $paypalInfo["mc_currency"];
    $data['status'] = $paypalInfo["payment_status"];
    
    // Get user ID from 'users' table.
    $IGN = $this->session->userdata['Player_Logged']['id'];


    // Pass the transaction data to view
    $this->load->view('paypal/success', $data);
            // Insert the transaction data in the database
                // This "custom" is old method by documentation, updated by me :P ('user_id' is 'UserID' [I changed column name in database])
            //$datatobase['user_id']    = $paypalInfo["custom"];
            $datatobase['UserID']   = $IGN;
            $datatobase['PackageID']    = $paypalInfo["item_number"];
            // ne barai taka e
            $datatobase['itemid']   = $paypalInfo["item_idr"];
            $datatobase['txn_id']   = $paypalInfo["txn_id"];
            $datatobase['payment_gross']    = $paypalInfo["mc_gross"];
            $datatobase['currency_code']    = $paypalInfo["mc_currency"];
            $datatobase['payer_email']  = $paypalInfo["payer_email"];
            $datatobase['payment_status'] = $paypalInfo["payment_status"];

            // Insert the gamerewards data in the database
            $rewardstobase['UserID']    = $IGN;
            $rewardstobase['ItemID']    = $paypalInfo["item_idr"];
            $rewardstobase['EnhID']     = "1957";
            $rewardstobase['Equipped']  = "0";
            $rewardstobase['Quantity']  = "1";
            $rewardstobase['Bank']      = "0";
            
            // Insert the transaction data in the database
            $this->product->insertTransaction($datatobase);
            // Insert the gamerewards data in the database
            $this->product->insertGameRewards($rewardstobase);
    } else {
            echo '<div class="alert alert-warning alert-dismissible fade show" role="alert"><strong>Error</strong> Error :( ! You need to log in to your account first.
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>';
            $this->output->set_header('refresh:5; url=http://192.168.1.14/web/Home');
    }
}
 
function cancel(){
    // Load payment failed view
    $this->load->view('paypal/cancel');
 }
 
function ipn(){
    // Paypal posts the transaction data
    $paypalInfo = $this->input->post();
    
    if(!empty($paypalInfo)){
        // Validate and get the ipn response
        $ipnCheck = $this->paypal_lib->validate_ipn($paypalInfo);

        // Check whether the transaction is valid
        if($ipnCheck){
            // Insert the transaction data in the database
            $data['user_id']    = $paypalInfo["custom"];
            $data['product_id'] = $paypalInfo["item_number"];
            $data['txn_id'] = $paypalInfo["txn_id"];
            $data['payment_gross']  = $paypalInfo["mc_gross"];
            $data['currency_code']  = $paypalInfo["mc_currency"];
            $data['payer_email']    = $paypalInfo["payer_email"];
            $data['payment_status'] = $paypalInfo["payment_status"];

            $this->product->insertTransaction($data);
        }
    }
}

产品控制器代码

function __construct() {
    $this->proTable   = 'web_store';
    $this->transTable = 'purchases_users';
    $this->GameRewards = 'users_items';
}

/*
 * Fetch products data from the database
 * @param id returns a single record if specified, otherwise all records
 */
public function getRows($id = ''){
    $this->db->select('*');
    $this->db->from($this->proTable);
    $this->db->where('status', '1');
    if($id){
        $this->db->where('id', $id);
        $query = $this->db->get();
        $result = $query->row_array();
    }else{
        $this->db->order_by('Title', 'asc');
        $query = $this->db->get();
        $result = $query->result_array();
    }
    
    // return fetched data
    return !empty($result)?$result:false;
}

/*
 * Insert data in the database
 * @param data array
 */
public function insertTransaction($data){
    $insert = $this->db->insert($this->transTable,$data);
    return $insert?true:false;
}

/*
 * Insert data in the database
 * @param data array
 */
public function insertGameRewards($data){
    $insert = $this->db->insert($this->GameRewards,$data);
    return $insert?true:false;
}

解决方案

这个错误的核心原因是PayPal没有回传item_idr这个参数,你的代码直接从POST数据里取这个索引,但它根本不存在。

1. 确保前端支付表单传递了item_idr

在用户点击支付的表单里,必须添加隐藏字段把item_idr传给PayPal,比如:

<input type="hidden" name="item_idr" value="<?php echo $product['item_id']; ?>">

替换$product['item_id']为你产品表中实际存储物品ID的字段名。

2. 给代码加索引存在性检查

即使表单传了参数,PayPal也可能因为回调机制不包含该字段,所以要先判断索引是否存在,避免报错:
修改PayPal控制器success方法里的取值代码:

// 替换原来直接赋值的行
$data['item_idr'] = isset($paypalInfo['item_idr']) ? $paypalInfo['item_idr'] : '';
// 数据库插入部分也要修改
$datatobase['itemid'] = isset($paypalInfo['item_idr']) ? $paypalInfo['item_idr'] : null;
$rewardstobase['ItemID'] = isset($paypalInfo['item_idr']) ? $paypalInfo['item_idr'] : null;

如果item_idr是必填项,可以在这里加判断,比如为空时终止插入并提示错误。

3. 更可靠的方案:通过item_number查数据库获取物品ID

不要依赖PayPal回传自定义字段,而是用item_number(产品ID)从数据库的产品表中查询对应的物品ID:

// 在success方法中,先获取产品信息
$product = $this->product->getRows($paypalInfo['item_number']);
// 从产品数据中取出物品ID
$item_id = $product['item_id']; // 替换为实际字段名

// 然后赋值给数据库插入数组
$datatobase['itemid'] = $item_id;
$rewardstobase['ItemID'] = $item_id;
$data['item_idr'] = $item_id;

这种方式不需要依赖PayPal传递自定义字段,稳定性更高。


内容的提问来源于stack exchange,提问作者traplord

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 21:31:08