We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44437
    • 74 Posts
    Hey all,

    I'm trying to understand how Formit2db works. I'm trying to update a row the database and I'm using the id to determine the row that I would like to update. I was reading the manual and tried out the snippet below but I keep getting an error.

    [[!FormIt? 
        &hooks=`spam,Formit2db` 
        &preHooks=`db2formit`
        &prefix=`modx_`
        &packagename=`service_requests`
        &classname=`service_requests`
        &tablename=`service_requests`
        &paramname=`id`
        &validate=`
            nospam:blank,
            sr_description:required`
        &successMessage=`It works` 
    ]]
    
    <form class="form" action="[[~[[*id]]]]" method="post">
        <div id="tcerrors">[[!+fi.validation_error_message]]</div>
        [[!fi.successMessage]]
        <input name="nospam" type="hidden" />
        <input name="id" type="text" value="[[+fi.id]]"/>
        <input name="sr_description" type="text" value="[[+fi.sr_description]]"/>
        <input type="submit" value="Save"/>
    </form>


    My error is:
    [2015-02-01 22:42:29] (ERROR @ /index.php) Error 23000 executing statement:
    INSERT INTO `modx_service_requests` (`id`, `sr_description`, `sr_status`, `sr_type`, `sr_client`, `sr_user`, `sr_uid`, `published`, `deleted`) VALUES (9, 'test', 'Pending', '', '', '', '', 0, 0)
    Array
    (
        [0] => 23000
        [1] => 1062
        [2] => Duplicate entry '9' for key 'PRIMARY'
    )
    
    [2015-02-01 22:42:29] (ERROR in FormIt2db Hook @ /index.php) Failed to save object of type: service_requests
    


    I can see that it is trying to insert an id that is already there but unless I'm understanding the manual incorrectly I was using the "&paramname" parameter to select which row (based off the id) to update.

    Can anyone please assist?

    This question has been answered by Bruno17. See the first response.

      • 4172
      • 5,888 Posts
      what is your xpdo-schema?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 44437
        • 74 Posts
        This is my xpdo-schema
        <?xml version="1.0" encoding="UTF-8"?>
        <model package="service_requests" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
                <object class="service_requests" table="service_requests" extends="xPDOSimpleObject" >
                <field key="sr_description" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
                <field key="sr_status" dbtype="varchar" precision="255" phptype="string" null="false" default="Pending" />
        		<field key="sr_type" dbtype="varchar" precision="255" phptype="string" null="false" default="" />		
        		<field key="sr_client" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
        		<field key="sr_user" dbtype="varchar" precision="255" phptype="string" null="false" default="" />		
        		<field key="sr_uid" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
                <field key="published" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
                <field key="deleted" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
                </object>
        </model>
          • 4172
          • 5,888 Posts
          if you want to update an existing object, you will need to use the property

          &where=`{"id":"9"}`


          otherwise it would try to create a new object with an allready existing id.

          Try something like that(untested):

          [[!FormIt?
              &hooks=`spam,Formit2db`
              &preHooks=`db2formit`
              &prefix=`modx_`
              &packagename=`service_requests`
              &classname=`service_requests`
              &tablename=`service_requests`
              &paramname=`object_id`
              &where=`{"id":"[[!getObjectId]]"}`
              &validate=`
                  nospam:blank,
                  sr_description:required`
              &successMessage=`It works`
          ]]
           
          <form class="form" action="[[~[[*id]]]]" method="post">
              <div id="tcerrors">[[!+fi.validation_error_message]]</div>
              [[!fi.successMessage]]
              <input name="nospam" type="hidden" />
              <input name="object_id" type="text" value="[[!getObjectId]]"/>
              <input name="sr_description" type="text" value="[[+fi.sr_description]]"/>
              <input type="submit" value="Save"/>
          </form>


          snippet getObjectId:

          return $modx->getOption('object_id',$_REQUEST,0);
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 44437
            • 74 Posts
            Yea, I had already tried &where=`{"id":"9"}` and it did not work. I just tested your code and I got the following error.
             [2015-02-02 09:24:48] (ERROR @ /index.php) Error 42S22 executing statement: 
            Array
            (
                [0] => 42S22
                [1] => 1054
                [2] => Unknown column 'service_requests.object_id' in 'where clause'
            )


            I should mention that I can add a row if I remove the &paramname and &where parameter. Can't figure out how to update a row.
              • 47401
              • 295 Posts
              i tried formit2db but found documentation very limiting, poor samples. I chose FormSave instead becuase it was a much quick installation and very easy to extract its data for 3rd party applications.
                • 44437
                • 74 Posts
                I prefer to use formit2db so that I can update my database fields. I use FormSave as a backup.
                  • 4172
                  • 5,888 Posts
                  No need for FormSave.
                  I'm sure, we will get a solution with formit2db!

                  Can you explain a bit more?
                  is there a list with all existing items with a link to the form-page, where the user can edit the selected item?
                  Or do you want indeed the user adding an id in a textbox to update that item?
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 44437
                    • 74 Posts
                    Hopefully I can explain carefully what I'm doing. I have an Ajax Table that displays the table rows of 'modx_service_requests'. It has 4 columns, namely: "id,description,status,edit/delete" see screenshot below

                    When the user click the "edit" icon, the description cell becomes a text input field and the edit/delete cell becomes a "save" icon. When the user clicks the "save" icon, it posts to another resource with the formit snippet.

                    This is my javascript code
                    function tcsave(){
                    var e = $(this).parent().parent();
                    var t = e.children("td:nth-child(1)");
                    var n = e.children("td:nth-child(2)");
                    var r = e.children("td:nth-child(3)");
                    var i = e.children("td:nth-child(4)");
                                
                    var u = n.children("input[type=text]").val();
                                
                    var f = "id=" + t.html() + "&sr_description=" + u;
                    $.post("portal/edit-service-request", f, function(e) {
                         if (!$(e).find("#tcerrors").length) var t = e;
                         else {alert("Some Error :(");
                         return false
                         }
                    })
                    }


                    And as you know my Formit snippet is:
                    [[!FormIt? 
                        &hooks=`spam,Formit2db` 
                        &preHooks=`db2formit`
                        &prefix=`modx_`
                        &packagename=`service_requests`
                        &classname=`service_requests`
                        &tablename=`service_requests`
                        &paramname=`object_id`
                        &where=`{"id":"[[!getObjectId]]"}`
                        &validate=`
                            nospam:blank,
                            sr_description:required`
                        &successMessage=`It works` 
                    ]]
                    
                    <form class="form" action="[[~[[*id]]]]" method="post">
                        <div id="tcerrors">[[!+fi.validation_error_message]]</div>
                        [[!fi.successMessage]]
                        <input name="nospam" type="hidden" />
                        <input name="object_id" type="text" value="[[!getObjectId]]"/>
                        <input name="sr_description" type="text" value="[[+fi.sr_description]]"/>
                        <input type="submit" value="Save"/>
                    </form>


                    I hope that is clear. I was testing the Formit snippet to see if I can update the table row but to no avail.
                      • 44437
                      • 74 Posts
                      Can you see where I'm going wrong?