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

PHP创建关联数据库记录失败:自增主键为空及Attempt to assign property 'person_id' on null错误排查

Let's walk through fixing your problems step by step — the error and the underlying issues preventing you from creating linked Person and Address records:

1. Root Cause of the "Assign Property on Null" Error

The error on line 22 happens because you're trying to set $address->person_id before creating an instance of the Address model. The $address variable is null at that point, so PHP throws an error when you try to access its properties.

Fix: Instantiate the Address Model

Add a line to create an Address object before you try to set its properties:

// After inserting the Person...
$address = new \app\models\Address(); // This line was missing!
$address->person_id = $person->person_id;
// ... rest of the address property assignments

2. Critical Issue: Not Retrieving the Auto-Generated person_id

Your Person model's insert() method has a problem: since person_id is an auto-incrementing primary key, you shouldn't include it in your INSERT statement. Worse, you aren't fetching the auto-generated ID after inserting the Person, so $person->person_id would be empty even if you fixed the first error.

Fix: Update the Person Model's insert() Method

Modify the method to omit the auto-increment field and fetch the generated ID:

public function insert(){
    // Remove person_id from the INSERT statement (database handles auto-increment)
    $SQL = 'INSERT INTO Person(first_name, last_name, `notes`) VALUES (:first_name, :last_name, :notes)';
    $STMT = self::$_connection->prepare($SQL);
    $STMT->execute(['first_name'=>$this->first_name, 'last_name'=>$this->last_name,'notes'=>$this->notes]);
    
    // Fetch the auto-generated person_id and assign it to the current object
    $this->person_id = self::$_connection->lastInsertId();
}

3. Updated Working Controller Code

Here's the full corrected insert() method in your Main controller, including the fixes above and a small improvement to ensure the header redirect works properly:

public function insert(){
    if(isset($_POST['action'])){
        $person = new \app\models\Person();
        $person->first_name = $_POST['first_name'];
        $person->last_name = $_POST['last_name'];
        $person->notes = $_POST['notes'];
        $person->insert();

        // Instantiate Address model (fixed null error)
        $address = new \app\models\Address();
        $address->person_id = $person->person_id; // Now uses the valid auto-generated ID
        $address->description = $_POST['description'];
        $address->street_address = $_POST['street_address'];
        $address->city = $_POST['city'];
        $address->province = $_POST['province'];
        $address->zip_code = $_POST['zip_code'];
        $address->country_code = $_POST['country_code'];
        $address->insert();

        header('location:/Main/index');
        exit; // Add exit to stop execution after redirect (prevents unexpected behavior)
    }else{
        $this->view('Main/insert');
    }
}

Final Notes

  • Your Address model's insert() method is already correct — once $address->person_id holds a valid value, it will insert the linked record properly.
  • Always remember to instantiate model objects before accessing their properties, and fetch auto-increment IDs after inserting records when you need to link related data.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 16:37:48