Skip to content
Advertisement

Php – cannot create new database record because primary key is null (autoincrement)

There are two tables: address and person. person_id (primary key, autoincrement) is a foreign key to address. I am trying to add an address record and person record from the same form but am getting this error.

Uncaught Error: Attempt to assign property “person_id” on null in C:xampphtdocsappcontrollersMain.php:22 Stack trace: #0 [internal function]: appcontrollersMain->insert() #1 C:xampphtdocsappcoreApp.php(52): call_user_func_array(Array, Array) #2 C:xampphtdocsindex.php(4): appcoreApp->__construct() #3 {main} thrown in C:xampphtdocsappcontrollersMain.php on line 22

Person insert method

Address insert

Advertisement

Answer

You have to use mysqli insert_id, after executing the insert command. insert_id will give you the primary key of the insert.

After

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement