4 Replies Latest reply on Mar 15, 2016 11:09 PM by rj.rijo

    Unable to edit record details using php. Showing record no longer exist.?

    rj.rijo

      Unable to edit record details using php. it showing record no longer exist. Im referning a book called Filemaker API for PHP. And im trying my handson the php file that is given with this book. IM on Lesson no 7 from core folder, whenever i click on the save button it showing record no longer exist.

      this is my code:

      EditRecord.php

       

      <html>

      <head>

      <title>Edit Record -&gt; Detail</title>

      </head>

       

       

      <body>

       

       

      <!--

      The purpose of this sample file is to allow the user to click a link to show the detail page.

      The point here is only to learn how to pass the RecordID from a search page to a detail page and display the results.

      The EditRecord.php page passes the RecordsID (as "id") to the EditRecordDetail.php page.

      -->

       

       

      <?php

      include ("../../Conn/dbaccess.php");

      ?>

       

       

      <form action="EditRecord.php" method="get">

      First Name: <input name="FirstName" type="text" />

      <br />

      <input name="submit" type="submit" />

      </form>

       

       

       

       

      <?php

       

       

      if(empty($_GET['FirstName']))

        {

          $FirstName = "";

          $request = $fm->newFindAllCommand('Demo php');

        }

        else

        {

          $FirstName = $_GET['FirstName'];

          $request = $fm->newFindCommand('Demo php');

          $request->addFindCriterion('FirstName', $FirstName);

        }

       

       

      $result = $request->execute();

       

       

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

        echo "<p>Error: " . $result->getMessage() . "</p>";

        exit;

      }

       

      $records = $result->getRecords();

       

       

      echo '<table border="1">

        <tr>

              <th>First Name</th>

                  <th>Last Name</th>

                  <th>Number Number</th>

                  <th>Date</th>

                  <th>Time Stamp</th>

                  <th>Calculation</th>

        <th>Record ID</th>

        <th>Edit Record</th>

              </tr>';

       

       

      foreach ($records as $record)

      {

        echo '<tr>';

          echo '<td>' . $record->getField('FirstName') . '</td>';

          echo '<td>' . $record->getField('LastName') . '</td>';

          echo '<td>' . $record->getField('Number') . '</td>';

          echo '<td>' . $record->getField('Date') . '</td>';

          echo '<td>' . $record->getField('TimeStamp') . '</td>';

          echo '<td>' . $record->getField('Calculation') . '</td>';

        echo '<td>' . $record->getField('RecordID') . '</td>';

       

        # Create the edit link to the EditRecordDetail.php page.

        # The link will create the URL http://localhost/EditRecordDetail.php?id=#

        # Note the name of the RecordID field will be "id" and will be the internal FileMaker RecordID, not an ID field that you created.

        # The first part creates the anchor and the id variable: <a href="EditRecordDetail.php?id='

        # The second part gets the recordID from FileMaker: $record->getRecordId()

        # Syntax: $record->getRecordId() - use as is

        # Finally, close out the text link and table row.

        # That's it... really! Next, you'll open the SearchResultsDetail.php page to display the record information based on the RecordID

       

        echo '<td><a href="EditRecordDetail.php?id=' . $record->getRecordId() . '">edit</a></td>';

          echo '</tr>';

      }

       

       

      echo '</table>';

       

       

      ?>

       

       

       

       

      </body>

      </html>

       

       

      EditRecordDetails.php

       

      <!--

      The purpose of this sample file is to allow the user to click a link from the EditRecord.php page

      to show the detail information here on the EditRecordDetail.php page.

      The point here is only to learn how to get the RecordID from the search page to display the record.

      Then allow the user to edit the record and save it back to the database.

      As always, any time a user is updating information in your database the data should be validated before submission.

      Note: the EditRecord.php page passes the RecordID (as "id") to the EditRecordDetail.php page.

      -->

       

       

      <?php

      include ("../../Conn/dbaccess.php");

      ?>

       

       

      <?php

      //Set message variable for IIS server. Any variables left empty and not defined on IIS will throw a 500 error.

      $message = "";

       

       

      # Check the POST name to see if the user clicked the Save button.

      if (isset($_POST['save']) and $_POST['save'] == 'Save') {

        # Tell FileMaker to edit the record.

        # Before the data is sent back to the database you can validate it here. No examples of validation are shown in the example.

        # Please see the demo for InsertRecordValidation.php

        # This is just a straight forward insert demo.

        $edit = $fm->newEditCommand('Demo php', $_POST['id']);

        # Set the fields with the values from the $_POST superglobal

        $edit->setField('FirstName', $_POST['FirstName']);

        $edit->setField('LastName', $_POST['LastName']);

        $edit->setField('Number', $_POST['Number']);

        //Date and TimeStamp are disabled in the POST, no need to update the values

        //$edit->setField('Date', $_POST['Date']);

        //$edit->setField('TimeStamp', $_POST['TimeStamp']);

        # Execute the newEditCommand

        $edit->execute();

       

       

        # Update the message variable to tell the user the record has been saved.

        $message = '<p>Your changes have been saved</p>';

      }

       

       

      # Make sure there is a record that corresponds to the RecordID. If not, stop the script.

      if (empty($_GET['id'])) {

          die('The record no longer exists.');

      }

       

       

      # Find the record using getRecordByID using the id passed in the URL

      $record = $fm->getRecordById('Demo php', $_GET['id']);

       

       

      # Create variables for all the fields for output in the html

      $FirstName = $record->getField('FirstName');

      $LastName = $record->getField('LastName');

      $Number = $record->getField('Number');

      $Date = $record->getField('Date');

      $TimeStamp = $record->getField('TimeStamp');

       

       

      ?>

       

       

      <html>

        <head>

        <title>Edit Record -> Detail</title>

        </head>

        <body>

       

       

      <!--

      Bu using a form the user can submit the save request to the database multiple times using the hidden field

      for the id value in the URL.

      The trick to pre-loading the values from the database is to echo out the variables stored above as the initial form value.

      Lastly, display the $message variable and a link back to the list page.

      -->

        <form action="EditRecordDetail.php?<?php echo $_GET['id']; ?>" method="post">

          <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />

          <table border="1">

            <tr>

              <th>First Name</th>

              <td><input type="text" name="FirstName" value="<?php echo $FirstName; ?>" /></td>

            </tr>

            <tr>

              <th>Last Name</th>

              <td><input type="text" name="LastName" value="<?php echo $LastName; ?>" /></td>

            </tr>

            <tr>

              <th>Number</th>

              <td><input type="text" name="Number" value="<?php echo $Number; ?>" /></td>

            </tr>

            <tr>

              <th>Date</th>

              <td><?php echo $Date; ?>"</td>

            </tr>

            <tr>

              <th>Time Stamp</th>

              <td><?php echo $TimeStamp; ?>"</td>

            </tr>

          </table>

        <p>

          <input name="save" type="submit" value="Save" />

        <p>

        <?php echo $message . '<a href="EditRecord.php">Click here to return to the list page</a> </p>'; ?>

        </form>

        </body>

      </html>

        • 1. Re: Unable to edit record details using php. Showing record no longer exist.?
          schwjm

          At a very brief glance, it looks like you're mixing $_GET and $_POST. GET and POST are distinct ways of transmitting request information. One of your forms uses method=GET and another uses method=POST. You only check "empty($_GET['id'])."

           

          To fix your issue it probably is just a matter of making sure you're using the correct super global. If you're receiving data from a POST form, you'll generally use $_POST only. If you're receiving it from a GET form, use $_GET only.

           

          Another trick you can use is to use $_REQUEST which combines $_GET and $_POST into one, if you will end up using a mix of form methods.

           

          Without getting into it too much, there are different reasons to use or not use GET/POST. I also don't prefer using $_REQUEST too much if $_GET or $_POST suffice. As long as you can follow what you write then you'll be fine. Hope this helps.

           

          P.S. I just noticed your form action was "EditRecordDetail.php?<?php echo $_GET['id']; ?>". You might also fix the issue by changing to "EditRecordDetail.php?id=<?php echo $_GET['id']; ?>" -- the method I was talking about above would involve passing the id value a different way, but it looks like you have it mostly under control

          • 2. Re: Unable to edit record details using php. Showing record no longer exist.?
            rj.rijo

            thanks sir, i have changed "EditRecordDetail.php?<?php echo $_GET['id']; ?>" to "EditRecordDetail.php?id=<?php echo $_GET['id']; ?>", its working but now when i click on save button it says that "your record has been saved" but in actual data is not saved to the database. i even tried switching between GET and POST.

            • 3. Re: Unable to edit record details using php. Showing record no longer exist.?
              schwjm

              The edit command probably failed for some reason. You will want and need to do error checking after most/all FileMaker calls, just like you would in a normal FileMaker script.

              The result of all commands is returned as either true, a FileMaker_Error object, or a FileMaker_ResultSet object. Try something like the following, the simplest way to do error checking (not necessarily the best):

              //instead of this: $edit->execute();
              $result = $edit->execute();
              if ($fm->isError($result))
              {
                   print "Error:" . $result->getMessage();
              }
              else
              {
                   print "Your changes have been saved.";
              }
              

              edited to use the proper call, I might have mixed things up before.