addForm.php to editForm.php

5 posts by 3 authors in: Forums > CMS Builder
Last Post: July 10, 2014   (RSS)

By Perchpole - July 7, 2014

Hi, All -

I'm trying to rework the popular addForm.php file so that I can use it to edit existing records. Part of the current code looks like this:

 mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)
      $query = "INSERT INTO `{$TABLE_PREFIX}$tableName` SET
                    createdDate      = NOW(),
                    createdByUserNum = '" .intval( @$CURRENT_USER['num'] ). "',
                    updatedDate      = NOW(),
                    updatedByUserNum = '" .intval( @$CURRENT_USER['num'] ). "',

                    title            = '".mysql_escape( $_REQUEST['title'] )."'";
      mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
      $newRecordNum = mysql_insert_id()

I want to modify this so that when a record already exisits the script will UPDATE it. (NB: I appreciate I would have to pass a page number variable to the script.)

Any pointers?

Perch

By gkornbluth - July 7, 2014

Hi Perch,

Have a look at the code in the member profile update form in the Membership plugin, it should give you the basic code for matching and updating an existing record.

There's also a recipe or two in the CMSB Cookbook http://www.thecmsbcookbook.com about updating record fields.

Look at the one called POPULATING A FORM FROM A MULTI RECORD DATABASE, UPDATING RECORDS, CREATING NEW RECORDS and UPDATING A RECORD ONLY WHERE AN INPUT FIELD MATCHES A RECORD FIELD VALUE

They should be of some help,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - July 8, 2014

Hi Perch, 

Here's some code snippets using some of our latest library functions:

// add record
$colsToValues = array(); // these fields get automatically mysql escaped unless you end fieldname with =
$colsToValues['createdDate=']     = 'NOW()';
$colsToValues['updatedDate=']     = 'NOW()';
$colsToValues['createdByUserNum'] = @$CURRENT_USER['num'];
$colsToValues['updatedByUserNum'] = @$CURRENT_USER['num'];
$colsToValues['title']            = @$_REQUEST['title'];
$hideMissingFieldErrors = true;
$newRecordNum = mysql_insert($tablename, $colsToValues, $hideMissingFieldErrors);

// update record
$recordNum    = 123;  // you can use either or both of $recordNum And $where
$where        = null; // lookup by recordNum only
$colsToValues = array(); // these fields get automatically mysql escaped unless you end fieldname with =
$colsToValues['updatedDate=']     = 'NOW()';
$colsToValues['updatedByUserNum'] = @$CURRENT_USER['num'];
$colsToValues['title']            = @$_REQUEST['title'];
mysql_update($tablename, $recordNum, $where, $colsToValues);

Hope that helps, cheers!

Dave Edis - Senior Developer
interactivetools.com

By Perchpole - July 10, 2014

Hi, Dave

Thanks for this. It would be great if someone would update the Uploadform.php with the new code options. 

Perchpole