I wan't to create a field for customer numbers (1, 2, ...), read only and auto increment when adding a new record.
I can't use the primary key for it.
I' ve tried a trigger in mysql but this gives a problem when i want to save my form.
Any idea how i can solve my problem?
I know nothing about php or java.
thankx.
Welcome to the nuBuilder Forums!
Register and log in to access exclusive forums and content available only to registered users.
Register and log in to access exclusive forums and content available only to registered users.
auto increment field
Re: auto increment field
johan,
I wonder why you want to have an auto-incrementing primary key.
I'd like you to read this..
http://nubuilder.blogspot.com/2010_09_01_archive.html
BUT
if you really want to you can have an incrementing field by populating a field from another table with an incrementing table by doing this.
-table 1 is a sample of a customer table
-table 2 is where the number comes from
-then put this code into "After Save" on the "Edit Code" tab of the Edit Form.
OTHERWISE
you will find that nuBuilder WILL support auto-increment Primary Key fields in our next update.
Steven
I wonder why you want to have an auto-incrementing primary key.
I'd like you to read this..
http://nubuilder.blogspot.com/2010_09_01_archive.html
BUT
if you really want to you can have an incrementing field by populating a field from another table with an incrementing table by doing this.
-table 1 is a sample of a customer table
-table 2 is where the number comes from
Code: Select all
CREATE TABLE `ac`.`testa` (
`testa_id` VARCHAR( 15 ) NOT NULL ,
`ta_customer_no` INT NOT NULL ,
`ta_name` VARCHAR( 200 ) NOT NULL ,
`ta_phone` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `testa_id` ) ,
INDEX ( `ta_customer_no` )
);
CREATE TABLE `ac`.`testa_counter` (
`testa_counter_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`tc_text` CHAR( 1 ) NOT NULL
)
-then put this code into "After Save" on the "Edit Code" tab of the Edit Form.
Code: Select all
if('#clone#' == '1' or '#recordID#' == '-1'){ //-- this is a new record
nuRunQuery("INSERT INTO testa_counter (tc_text) VALUES ('1')");
$s = "UPDATE testa SET ta_customer_no = '" . mysql_insert_id() . "' ";
$s .= "WHERE testa_id = '#newID#'";
nuRunQuery($s);
}
you will find that nuBuilder WILL support auto-increment Primary Key fields in our next update.
Steven
-
- Posts: 13
- Joined: Fri Jun 06, 2014 9:44 am
Re: auto increment field
I have read all of the posts about auto_increment. I like the idea of not using them except as used in the financial example. I decided I would like to set the next sequence number in the custom code area of my form. Everything works except assigning the resulting value to the form field. I have tried all of the examples I could find. Some just disabled the code I wrote completely. Others just don't work for me. I just want to set the form field to the number I have already obtained. The field in the table is a unsigned integer. Maybe this has something to do with it, but I have spent a lot of time on something that should be very straight forward. nuDisplayError helped get me through because I knew something was really wrong when no message showed up. I only have one last statement to put in code, but everything I have tried either does nothing or it disables all the code in the Before Saving section.
Thanks for any help.
Bill Bailey
Thanks for any help.
Bill Bailey
-
- Posts: 503
- Joined: Thu May 24, 2012 2:08 am
- Location: Milan, Italy
- Contact:
Re: auto increment field
Bill,
code posted above by Steven works fine: I suggest to follow this example.
Pay attention: this works in nuBuilder version 2; if you are running nuBuilderPro you should edit something in the code.
Max
code posted above by Steven works fine: I suggest to follow this example.
Pay attention: this works in nuBuilder version 2; if you are running nuBuilderPro you should edit something in the code.
Max
-
- Posts: 13
- Joined: Fri Jun 06, 2014 9:44 am
Re: auto increment field
Thanks, Max. I am using nuBuilderPro. Is there more risk with Pro than V2? I am not familiar enough with either version to pick one. I picked the most recent one, thinking the other was around for legacy reasons.
Thanks for your thoughts.
Bill
Thanks for your thoughts.
Bill
-
- Posts: 503
- Joined: Thu May 24, 2012 2:08 am
- Location: Milan, Italy
- Contact:
Re: auto increment field
Bill, for new projects, nuBuilderPro is the last and suggested release.
The code above should works also in nuBuilderPro, but there are some differences between two versions to keep in mind:
Hope this helps,
Max
The code above should works also in nuBuilderPro, but there are some differences between two versions to keep in mind:
- primary keys of all tables in nuBuilderPro are (or should be) varchar(25) instead of varchar(15);
- different hash variable names (example: "#RECORD_ID#" instead of "#recordID#", see here all options)
Hope this helps,
Max