Custom Contact Form Block not able to save to database
Permalink
I've wrote a custom contact form block to take data from a form and then save it to the database + send a copy of the form entry via email as the included form block wasnt very versatile or easy to style/add custom elements, the logic and process is working fine for the first save but not for any subsequent ones.
I'm thinking this is happening as the block is in a global slot and the block ID is always the same. As the database saves with the key field needing to unique it fails after the first entry.
Any suggestions on how i could fix this?
Can i save through a db.xml without a bID, i havnt had much success with what i've tried.
Error Message on subsequent sending of form:
Block Controller (pulled a bits not related to error and replaced with comments)
Block db.xml
I'm thinking this is happening as the block is in a global slot and the block ID is always the same. As the database saves with the key field needing to unique it fails after the first entry.
Any suggestions on how i could fix this?
Can i save through a db.xml without a bID, i havnt had much success with what i've tried.
Error Message on subsequent sending of form:
An unexpected error occurred. An exception occurred while executing 'INSERT INTO btFormSubs ( bID, field1, field2, field3, field4, field5) VALUES(?, ?, ?, ?, ?, ?)' with params ["164", "xxxxx", "xxxxx", "xxxxx", "xxxxx", "xxxxx"]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '164' for key 'PRIMARY'
Block Controller (pulled a bits not related to error and replaced with comments)
public function action_save_form($bID = false) { if ($this->bID != $bID) { return false; } $field1= $_REQUEST['field1']; $field2= $_REQUEST['field2']; $field3= $_REQUEST['field3']; $field4= $_REQUEST['field4']; $field5= $_REQUEST['field5']; // add to database $db = \Database::connection(); $addToDB = $db->Execute( 'INSERT INTO btFormSubs ( bID, field1, field2, field3, field4, field5) VALUES(?, ?, ?, ?, ?, ?)', array( $this->bID,
Viewing 15 lines of 36 lines. View entire code block.
Block db.xml
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.concrete5.org/doctrine-xml/0.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.concrete5.org/doctrine-xml/0.5http://concrete5.github.io/doctrine-xml/doctrine-xml-0.5.xsd"&... <table name="btFormSubs"> <field name="bID" type="integer"> <key /> </field> <field name="field1" type="text"> </field> <field name="field2" type="text"> </field> <field name="field3" type="text"> </field> <field name="field4" type="text">
Viewing 15 lines of 20 lines. View entire code block.
Sorry for the slow response the forums locked my account and stopped me from posting for some reason (spam catch maybe - i posted 2 questions within an hour)
Anyway thanks MrKDilkington that's unrelated just an edit mistake while posting here will fix.
Does anyone have any other suggestions on how to save my data to the database more than once? Its definately the issue with the blockID being the same which is preventing the save.
Thanks
Anyway thanks MrKDilkington that's unrelated just an edit mistake while posting here will fix.
Does anyone have any other suggestions on how to save my data to the database more than once? Its definately the issue with the blockID being the same which is preventing the save.
Thanks
no one able to offer any insight :(
I think you need another table. You need 1 for the block record (btFormSubs) which stores the block record and the information for the edit screen. Then you need another table (btFormSubResponses?) which will hold the information that was submitted to the form.
This may be unrelated, but your table name is "btContactFormSubs" and your action_save_form() method is trying to insert into a table called "btFormSubs".