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

PHP TypeError错误求助:字符串与整数无法直接相加(附代码)

TypeError:字符串与整数相加错误

错误详情

未捕获异常:
类型:TypeError
信息:不支持的操作数类型:string + int
文件路径:C:\xampp\htdocs\perpustakaan\application\models\Anggota_model.php
行号:46

调用栈

  • 文件:C:\xampp\htdocs\perpustakaan\application\models\Anggota_model.php,行号:8,函数:generateID
  • 文件:C:\xampp\htdocs\perpustakaan\application\controllers\Anggota.php,行号:78,函数:insert
  • 文件:C:\xampp\htdocs\perpustakaan\index.php,行号:315,函数:require_once

问题原因

在generateID()方法中,substr($query, 3)提取的编号后缀为字符串类型,直接与整数1进行加法运算时触发类型错误。同时未处理数据库无数据的边界情况,此时$query为null,会导致substr调用失败。

解决方案

  1. 强制将字符串类型的编号后缀转换为整数,使用(int)或intval()函数
  2. 增加数据库无数据的判断逻辑,无记录时直接从1开始生成ID

修改后的generateID()方法:

public function generateID(){
    $query = $this->db->order_by('ID_ANGGOTA', 'DESC')->limit(1)->get('anggota')->row('ID_ANGGOTA');
    
    // 处理数据库无数据的情况
    if(!$query){
        $lastNo = 0;
    }else{
        // 将字符串转换为整数
        $lastNo = (int) substr($query, 3);
    }
    
    $next = $lastNo + 1;
    $kd = 'AGT';
    return $kd.sprintf('%03d', $next); // 用%03d确保数字补零,比%03s更严谨
}

完整修改后的模型代码

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Anggota_model extends CI_Model {

    public function insert($id_petugas, $foto){
        $data = array(
            'ID_ANGGOTA'    => $this->generateID(),
            'ID_ADMIN'      => $id_petugas,
            'FULL_NAME'     => $this->input->post('nama_lengkap'),
            'TMP_LAHIR'     => $this->input->post('tmp_lahir'),
            'TGL_LAHIR'     => $this->input->post('tgl_lahir'),
            'ALAMAT'        => $this->input->post('alamat'),
            'GENDER'        => $this->input->post('gender'),
            'TELP'          => $this->input->post('telp'),
            'FOTO'          => $foto['file_name'],
            'D_CREATED'     => date('Ymd')
             );

        $this->db->insert('anggota', $data);
        return $this->db->affected_rows() > 0;
    }

    public function update($id){
        $data = array(
            'FULL_NAME'     => $this->input->post('nama_lengkap'),
            'ALAMAT'        => $this->input->post('alamat'),
            'TELP'          => $this->input->post('telp')
        );

        $this->db->where('ID_ANGGOTA', $id)->update('anggota', $data);
        return $this->db->affected_rows() > 0;
    }

    public function generateID(){
        $query = $this->db->order_by('ID_ANGGOTA', 'DESC')->limit(1)->get('anggota')->row('ID_ANGGOTA');
        
        if(!$query){
            $lastNo = 0;
        }else{
            $lastNo = (int) substr($query, 3);
        }
        
        $next = $lastNo + 1;
        $kd = 'AGT';
        return $kd.sprintf('%03d', $next);
    }

    public function getList(){
        return $this->db->order_by('id_anggota','ASC')->get('anggota')->result();
    }

    public function getCount(){
        return $this->db->count_all('anggota');
    }

    public function getDetail($id){
        return $this->db->where('ID_ANGGOTA', $id)->get('anggota')->row();
    }

    public function checkAvailability($id){
        $query = $this->db->where('ID_ANGGOTA', $id)->get('anggota');
        return $query->num_rows() > 0;
    }

    public function delete($id){
        $this->db->where('ID_ANGGOTA', $id)->delete('anggota');
        return $this->db->affected_rows() > 0;
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 10:53:30