Jump to content
Sign in to follow this  
Guru Salem

Get Last Record id from FileMaker-PHP

Recommended Posts

$rec =& $fm->newAddCommand('employee');

$rec->setField('name', 'Jack');

$result = $rec->execute();

With the above code i have successfully inserted the "Jack" record to the filemaker table.

Here i would like to get he last inserted record id..

Is any predefined function available for this to get "last record id".. if you know please help me to resolve this solution.

Many thanks in advance for your valuable reply.

Share this post

Link to post
Share on other sites

Not that I know of. I had to create a work-around that looks like this:

$rec =& $fm->newAddCommand('employee');

$rec->setField('name', 'Jack');

$result = $rec->execute();

$findRec = $fm->newFindCommand('employee');

$findRec->addFindCriterion('name', 'Jack');

$result = $findRec->execute();

if( FileMaker::IsError($result)){

die('no records found<br>');

} else {

$recs = $result->getRecords();

$count = count($recs);

$lastID = $recs[$count-1]->getRecordID();

echo '<br>record id = ' .$lastID . '<br>';


Basically, this code creates the record, then searches for the data you entered into the record. Since this data may be the same as previously entered records, you have to go to the last record in the found set, and get the RecordID of that record.

Caution: I am not sure that this is totally multi-user safe.

Share this post

Link to post
Share on other sites

You right Doughemi.,

Having the same doubt whether it will support for multi-user !

anyway thanks for the great support and valuable reply,

Share this post

Link to post
Share on other sites

The only multi-user issue I can imagine would be if someone else created a record with the same "name" field value at almost the exact same time - which probably isn't very likely in most use-case scenarios.

Using an exact field match search will reduce this from happening as often:

$findRec->addFindCriterion('name', '==Jack');

The only other method I can think of would be using a script to create the record. I'm not quite sure how you would get the result of the script, but I think that question has been asked (and answered?) on these forums.

Share this post

Link to post
Share on other sites

Hmmm - I thought the return values from an insert was the record just created - that's certainly how FX.php handles it. So you can just get the actual -recid from the data returned back from the create...

Share this post

Link to post
Share on other sites

Yes, the result is a normal FilemakerResult, so just use:

$rec =& $fm->newAddCommand('employee');
$rec->setField('name', 'Jack');
$result = $rec->execute();

$rec_id = $result->getLastRecord()->getRecordID();
$created_name = $result->getLastRecord()->getField('name',0); # or any other field, also autofill

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Mark Hussey
      Does anyone know how to have empty fields collapse, both image and text, on my search results page. Especially the pictures but if there is a way for empty text fields to collapse as well that would be awesome. I am new to the PHP API and have everything working except for a solution for this. An example is a tourism site I'm working on where there are selected icons in the FM db - I only want to show the ones that are selected and if they are empty I need them to collapse - otherwise I get this big long list with icons showing up wherever they are in the order of the code. If someone knows the answer please help - Mark
    • By Jerry100
      Does anyone know if FM Studio and Dreamweaver CS4 are compatible and if there is any advantage in purchasing CS4 instead of CS3?
    • By midimid
      Something I'm a bit perplexed about. I have Filemaker 10 Advanced, FMStudio with Dreamweaver CS5 and apache installed locally. We are using fmgateway for hosting.

      Using the above, can I setup things so that I'm developing entirely locally without touching the online fmgateway DB, then move the DB and local php files online when I need to launch?
    • By Steven1337
      The left menu on my website is dynamic and it is based off a recordset from my filemaker database. My left menu is included on every page of my website and I do not want the find of the recordset to occur every time a user browses to a different page. I am looking for a way to store the results in a session so that it only has to look for them once per session. The code that creates my leftbar menu looks like this:

      <?php // FMStudio v1.0 - do not remove comment, needed for DreamWeaver support ?> <?php $categories_find = $Rackspace->newFindCommand('vehiclesearchsmall'); $categories_findCriterions = array('VehicleCategory'=>'=='.$_SESSION['make'],'VehicleId'=>$_SESSION['model'],'year'=>$_SESSION['year'],); foreach($categories_findCriterions as $key=>$value) { $categories_find->AddFindCriterion($key,$value); } fmsSetPage($categories_find,'categories',10000); $categories_find->addSortRule('SubCategory2',1,FILEMAKER_SORT_ASCEND); $categories_result = $categories_find->execute(); if(FileMaker::isError($categories_result)) {$error = 'yes';} else { fmsSetLastPage($categories_result,'categories',10000); $categories_row = current($categories_result->getRecords()); } ?> <?php $lis = array(); foreach ($categories_result->getRecords() as $cer) { $tac = $cer->getField('SubCategory2'); $lis[$tac] = (!isset($lis[$tac])) ? 1 : $lis[$tac] +1 ; } foreach ($lis as $nam => $valu) { echo '<dd class="categories" style="border-bottom:1px dotted #999;"><a href="cilist.php?VehicleCategory='. $_SESSION['make'] .'&SubCategory2=' . $nam . '&VehicleId=' .$_SESSION['model'] . '&year=' . $_SESSION['year'] . '" >' . $nam .'</a> '. '(' . $valu . ')' . '</dd>'; } ?>

      I tried storing the $categories_row in a session variable but it would only give me one result. I am stuck here any help would be greatly appreciated.
    • By NYC Newbie
      I have an ecommerce site in which the inventory is also listed on another metasite (i.e. amazon, ebay, etc..). The metasite maintains its own separate inventory.
      I need to send a xml to the metasite's webservice whenever someone buys an item on my website.
      The purpose of this xml is to delete the item off the metasite immediately.

      I am using FMStudio for FX.php and Filemaker Pro 11.
      The delete xml will be generated upon payment confirmation from the buyer. It is autogenerated as a webpage.
      I also have a php page that sends a 'file_get_contents' to the webservice sucessfully - meaning it works just fine when sending an xml file that resides in the same folder.
      But I don't know how to integrate the two. The two files do not sit in the same folder so the latter is having a hard time finding the former, and any combination of "../foldername.php" doesn't seem to work.
      And i'm not even sure this is the most expedient way to do this so I'm open to any suggestions.

      Here is my autogenerated delete xml page in php. Basically it lists the ids of ordered items that reside in a portal on an FM layout called 'orders' -

      <?php require_once('Connections/invoices.php'); ?>
      <?php $ids_find = clone($invoices);
      $ids_find->layout = 'orders';
      $ids_findCriterions = array('orderID'=>$_REQUEST['orderid'],);
      foreach($ids_findCriterions as $key=>$value) {


      $ids_result = $ids_find->FMFind();

      if(is_a($ids_result,'FX_Error') || $ids_result['errorCode'] != "0") fmsTrapError($ids_result,"error.php");

      $ids_row = current($ids_result['data']);
      $ids_row_key = key($ids_result['data']);

      $ids_orderlineitem_portal_row = 0;
      $ids_orderpaypalvars_portal_row = 0;



      //FMStudioFX v1.0 - do not remove comment, needed for DreamWeaver support ?>
      header('content-type: text/xml; charset=utf-8');
      echo('<?xml version="1.0" encoding="ISO-8859-1"?>');
      echo('<inventoryUpdateRequest version="1.0">');
      echo('<action name="bookupdate">');
      $ids_orderlineitem_portal_rownum = 1;
      foreach($ids_row['order_line_item::bookID'] as $ids_orderlineitem_portal_row=>$ids_orderlineitem_portal){
      echo('<vendorBookID>'.$ids_row['order_line_item::bookID'][$ids_orderlineitem_portal_row].'</vendorBookID> </MetaSite>');
      if($ids_orderlineitem_portal_rownum == 0) break; else $ids_orderlineitem_portal_rownum++;

      The xml document appears in a URL formatted correctly. But I don't care for the buyers to see this page, not even a millisecond of it.

      Here is the redirected 'Books/websitedeleteIndex.php file -

      <?php require_once('post_xml.php');?>
      <?php $xml = file_get_contents('updateInventory.php');
      $url ='https://inventoryupdate.metasite.com';
      $port = 80;

      $response = xml_post($xml, $url, $port);

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>Untitled Document</title>

      I tried <?php $xml = file_get_contents('../updateInventory.php') because it sits one directory level up. Doesn't work.
      But there's got to be an easier way to send this info, yes?

Important Information

By using this site, you agree to our Terms of Use.