Update data .dbf using php

Hello everyone. please help me to write php script for button function “update” to update or edit data in .dbf file

The files and script to show dbf using php as below:
Index.php
test.dbf

<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);
    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>";

if ($db) {
  	$record_numbers = dbase_numrecords($db);
  	echo "TOTAL USERS => $record_numbers<br>";
  	for ($i = 1; $i <= $record_numbers; $i++) {
	$row = dbase_get_record_with_names($db, $i);
   	 echo "<tr>";
   	 echo "<td>" . $row['USER_ID'] . "</td>";
   	 echo "<td>" . $row['NAME'] . "</td>";
   	 echo '<td><input type="text" value="' . $row["PASS"] . '" ></td>';
   	 echo '<td><input type="text" value="' . $row["TRIES"] . '" ></td>';
   	 echo '<td><input type="text" value="' . $row["ACTIVE"] . '" ></td>';
   	 echo '<td><a href=" ">Update</a></td>';
            	echo "</tr>";
  	}echo "</table>";
   }dbase_close($db);
?>
</body>
</html>

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.

  1. 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>
  1. 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

1 Like

Waaaaahhhhh…THANK YOU… :grinning_face_with_smiling_eyes:
This working perfectly…

HI Katsry.

need to help how to put update_dbf.php code to same page in index.php.
because i need to use only one page…tq very much for help.

<html>
<head>
    <style>
        table {
            border-style: solid;
            border-width: 2px;
            border-color: pink;
        }
    </style>
</head>
<body bgcolor="#EEFDEF">
<?php
// Function to update the database record
function updateRecord($record_number, $pass, $tries, $active) {
    $db = dbase_open('test.dbf', 2); // open in read-write mode
    if ($db) {
        $row = dbase_get_record_with_names($db, $record_number);
        $row['PASS'] = $pass;
        $row['TRIES'] = $tries;
        $row['ACTIVE'] = $active;
        unset($row['deleted']);

        if (dbase_replace_record($db, array_values($row), $record_number)) {
            echo "Record updated successfully.<br>";
        } else {
            echo "Failed to update record.<br>";
        }
        dbase_close($db);
    } else {
        echo "Error opening the DBF file.<br>";
    }
}

// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    updateRecord($_POST['record_number'], $_POST['pass'], $_POST['tries'], $_POST['active']);
}

// Now display the table
$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='index.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>

1 Like

HI Katsry.

Thank you very much for helping me.
The script code working perfectly… :wink: :wink: :hugs: :hugs: :hugs: :+1: :+1:

Your Welcome :smile:

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service