This topic is locked

How to insert a default value into a field as an after record added ac

12/9/2009 4:38:47 PM
PHPRunner General questions
A
Alanmelon author

Hi,
I have a database set up using phprunner and would like to add a default numerical value to one of my fields in a table when a new record is added. This field is not included in the Add Page list of fields, and so automatically gets a blank entry when a new record is created, but I would like it to have a value of zero. Could anyone provide me with the code to do this please?
Many thanks.

Pfeiffer 12/9/2009

Hello,
my first idea...

define in your database table that the fields value is 0 if you put no value into it during add.

For example in mysql you define the field value as NOT NULL, and mysql will automatically put 0 into the field.
my second idea..

in after record event you have to get the last insert_id from table.

This depends on your database .. in mysql it is the command mysql_insert_id()..

Put this value in $last_id = ....<last_id> from your database for this table
With this value you can make an update command like

$sql = "Update <table> set <myfield>=0 where <primary_key> = $last_id"

db_exec ($sql)
Best regards

Uwe Pfeiffer