rj.rijo

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

Discussion created by rj.rijo on Mar 14, 2016
Latest reply on Mar 15, 2016 by 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>

Outcomes