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 Perchpole - July 8, 2014

Hi, Jerry -

Good idea re the membership plugin. I'll take a look.

Also thanks for reminding me about the Cookbook. Lots of good stuff in there!

:0)

Perch

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