Buy Now
Sign in

Create a Form to insert/saveand update data in your database

marcq , December 01 2014
Answered
m
marcq

Hi everyone,

BNDragon helped me last week to create a form which enable a registred user to insert, save and update data. My project was to create some checklists which each registred user can use, but with the limitation that a user can only create one record per form.

You will find attached to this topic a PDF file called CHRONOFORM FORM which explain precisely how BNDragon solved the issues I had.

You will also find a backup of this form, which you can use. But don't forget to adapt it and to create your database.

I hope it helps !

Cheers,

Marc

Thanks to BNDragon !

Attachments
CHRONOFORM FORM.pdf
(751.59 KiB)
1243 Downloads/Views
admin

Thank you! :)

Max
If your main question got answered then please mark the answer using the button!

G
Gjertsen

Hi, and thank you for such a great and detailed guide :) I have a question though from trying to adapt it to fit my own form. Not sure if I should start a new thread or post here, but here it goes.

I'm at the "custom code action" (on found) under the DB Read /On Load. The last few lines of php code seem to deal with reading data from the checkbox entries, but since I'm no php programmer I'm really struggling to adapt this to my case where I have two text boxes instead of checkboxes that I would want to read/save data from.

admin

Hi Gjertsen,

What are the lines of code you need help with ?

Regards,
Max

Max
If your main question got answered then please mark the answer using the button!

G
Gjertsen

Hi, and thanks for the quick reply :)

				
$checkbox1=$line['checkbox group name'];
$pieces = explode(",", $checkbox1);
$form->data["checkbox group name"] = $pieces;

$checkbox2=$line[‘checkbox group name'];
$pieces = explode(",", $checkbox2);
$form->data["checkbox group name"] = $pieces;


The lines above are the last lines of the php code snippet on page 17 in the PDF guide. They seem to handle the checkbox entries in the DB and have some "checkbox group name" entries which makes me think these lines of code need to be reworked altogether for two text box fields to be read/written to DB insted of checkboxes (which in the example from the pdf is also part of a checkbox group). My apologies that I'dont really know what I'm talking about when it comes to php :?

admin

Hi Gjertsen,

No, those lines just split the checkboxes values since they are normally a list of values, so if you don't have checkboxes in your form then this is not helpful!

Regards,
Max

Max
If your main question got answered then please mark the answer using the button!

G
Gjertsen

I'm sorry to ask again, but I can't seem to get this working. Am I wrong thinking that the custom code (which the above code was a part of) under On load/DB Read/On found handles the displaying of the found DB entries in the website form? If thats the case then my question is how can I insert my two textbox values in the website form instead of the checkbox ones in the example? Simply changing the checkbox1 and checkbox2 values don't seem to work. Since the only mention in the code of the saved form values are in the code above, I assumed they had to do with the inserting of the read DB values into the website form.

Best regards

admin

Your problem is in the "save" part or the update part ?

the save part should be straight forward, a "DB Save" will handle this.

The update part should be the same, but you will have to set the primary key value in the "data array" before the "db save", you can read the data using a "db read" action.

You should not have to worry about any fields values in this process.

Regards,
Max

Max
If your main question got answered then please mark the answer using the button!

G
Gjertsen

Ok, so I've installed the backup form that goes with the PDF guide and I got it to work with the checkboxes, just to try it as it is before trying to make it fit my own purpose.

To the example form I've then added a "text13" textbox to the designer and made a new table which includes this field as well. The form updates the DB just fine, but the strange ting is that the saved value to text13 remains visible in the textbox when changing users and even with no users logged in. The checkboxes changes values as they should.

The other strange thing is that the form only seem to save a record for the first user that submits the form. Any other users logging in doesn't get its own DB entry from submitting the form. Is there anything I've missed, and mabye even a connection between the two?

G
Gjertsen

I think I may have solved the first problem myself by adding the following code at the end of both custom code entries and now the saved values loads only when logged in for the textbox as well.

				
$typetb=$line['text13'];
$form->data["text13"] = $typetb;

Still no luck saving more than 1 record to the table though.

G
Gjertsen

I'm at the point now where I've done some reading and understand that the update/save to DB for different users have to do with the primary key setting when creating the table (like you correctly mentioned in your last post), but I can't figure out which column that should be set as primary key. By default when creating a table a INT "id" column with extra set to "auto_increment" is suggested. I've tried removing the "auto_increment" setting, since I believe that the "id" column should contain the user id, and not an auto incremented number, but this doesn't seem to help. I've also tried setting "user_id" as primary key.

admin

The table's primary key should be id and it should be set to auto increment, there is already another column created by default named "user_id", and that one will hold the user's id (if a user is logged in), this also happens automatically if you have the latest v5 update.

Please try creating a new fresh form with a new table and test this, each submission should then store a new record.

Let me know!

Max
If your main question got answered then please mark the answer using the button!

G
Gjertsen

Ok, thank you for the clarification on the table settings. I've now tried setting up a new form with a table from scratch, using the basic setup from the example files to produce the functionality that I'm after. Still not able to create a new record for other than the first user that submits the form. That user updates the corresponding record when new data is submitted (which is correct), but data from any other than the first user that submits doesn't register anywhere.

I tried removing the update condition code below from the DB Save under On submit. When I do that there's created a new record each time someone submits the form, but this way already submitted data from registered users aren't updated, just a new record created for each submission. Could there be something with this update condition code that prevents a new record from being created for new users?

				
<?php
return array("user_id" => $form->data["userID"]);
?>


However, since there's a DB read with a DB Save under On not_found, I would think that this is the place a new record is created in the case of no pre-existing registered data to the DB table. The only difference between this DB Save and the DB Save under On submit, is the update condition posted above, which is only under the On submit, DB Save.

I've also tried setting Multi read under DB Read to yes, but not sure if that's neccessary to read (and check) more than one record from the table. It doesn't seem to be set to yes in the example/tutorial.

admin

Well, if you have the code above in the "update conditions" then the logged in user's record will always be updated, so, if you are submitting the form without being logged in then you will have a record with user_id = 0, and that record will be always updated.

Please try to login using 2 different users and submit the form each time, do you get 1 record or 2 records ?

Regards,
Max

Max
If your main question got answered then please mark the answer using the button!