Updating a .dbf
file using PHP involves reading the current records, displaying them in an HTML form, allowing users to modify them, and then saving the changes back to the .dbf
file.
To facilitate this, your “Update” button should be part of a form that submits the updated record data to a PHP script that processes the update.
Below is an example of how you could structure your index.php
to include a form for each record and a separate PHP script that handles the update. Please note that this example is simplified and does not include any error checking or security measures like CSRF protection, which you should implement in a production environment.
-
index.php
(with update forms for each row):
<html>
<head>
<style>
table {
border-style: solid;
border-width: 2px;
border-color: pink;
}
</style>
</head>
<body bgcolor="#EEFDEF">
<?php
// open in read-only mode
$db = dbase_open('test.dbf', 0);
if ($db) {
$record_numbers = dbase_numrecords($db);
echo "TOTAL USERS => $record_numbers<br>";
echo "<table border='1'>
<tr>
<th>USER ID</th>
<th>NAME</th>
<th>PASS</th>
<th>TRIES</th>
<th>ACTIVE</th>
<th>UPDATE</th>
</tr>";
for ($i = 1; $i <= $record_numbers; $i++) {
$row = dbase_get_record_with_names($db, $i);
echo "<form action='update_dbf.php' method='post'>";
echo "<tr>";
echo "<td>" . $row['USER_ID'] . "</td>";
echo "<td>" . $row['NAME'] . "</td>";
echo "<td><input type='text' name='pass' value='" . trim($row["PASS"]) . "' ></td>";
echo "<td><input type='text' name='tries' value='" . trim($row["TRIES"]) . "' ></td>";
echo "<td><input type='text' name='active' value='" . trim($row["ACTIVE"]) . "' ></td>";
echo "<td><input type='hidden' name='record_number' value='$i'>";
echo "<input type='submit' value='Update'></td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
dbase_close($db);
}
?>
</body>
</html>
-
update_dbf.php
(the script that processes the update):
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$record_number = $_POST['record_number'];
$pass = $_POST['pass'];
$tries = $_POST['tries'];
$active = $_POST['active'];
$db = dbase_open('test.dbf', 2); // open in read-write mode
if ($db) {
// Replace with the actual field names and indices from your DBF file
$row = dbase_get_record_with_names($db, $record_number);
$row['PASS'] = $pass;
$row['TRIES'] = $tries;
$row['ACTIVE'] = $active;
// Remove the 'deleted' entry from the row
unset($row['deleted']);
// Update the record
if (dbase_replace_record($db, array_values($row), $record_number)) {
echo "Record updated successfully.";
} else {
echo "Failed to update record.";
}
dbase_close($db);
} else {
echo "Error opening the DBF file.";
}
}
?>
In this example, update_dbf.php
will be called when the “Update” button is clicked. The record number along with the new values for PASS
, TRIES
, and ACTIVE
fields are sent via POST to the server. The update_dbf.php
script then opens the .dbf
file in read-write mode, updates the specific record, and saves the changes.
Make sure that the dbase
extension is enabled in your PHP installation, and that you have write permissions to the .dbf
file on the server.
Good luck