PHP: dbase_replace_record - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DBM dbase_pack

Last updated: Sat, 27 Jan 2007

view this page in

# dbase_replace_record

(PHP 3 >= 3.0.11, PHP 4, PHP 5)

dbase_replace_record -- Replaces a record in a database

## Описание

bool dbase_replace_record ( int dbase_identifier, array record, int record_number )

Replaces the given record in the database with the given data.

## Список параметров

`dbase_identifier`

  The database link identifier, returned by dbase_open() or dbase_create().

`record`

  An indexed array of data. The number of items must be equal to the number of fields in the database, otherwise dbase_add_record() will fail.

  > Замечание: If you're using dbase_get_record() return value for this parameter, remember to reset the key named `deleted`.

`record_number`

  An integer which spans from 1 to the number of records in the database (as returned by dbase_numrecords()).

## Возвращаемые значения

Возвращает `TRUE` в случае успешного завершения или `FALSE` в случае возникновения ошибки.

## Примеры

Пример 1. Updating a record in the database

`<?php
// open in read-write mode
$db = dbase_open('/tmp/test.dbf', 2);
if ($db) {
// gets the old row
$row = dbase_get_record_with_names($db, 1);
// remove the 'deleted' entry
unset($row['deleted']);
// Update the date field with the current timestamp
$row['date'] = date('Ymd');
// Replace the record
dbase_replace_record($db, $row, 1);
dbase_close($db);
}
?>`

## Смотрите также

dbase_add_record()
dbase_delete_record()

add a note User Contributed Notes
dbase_replace_record
hassan at datakillarna dot se
10-Feb-2006 04:30
`If you get "unexpected error", try to change the assoc array, $row, to an indexed array with array_values().
Example:
$row = array_values($row);
dbase_replace_record($db, $row, 1);
The dbase_replace-function cannot handle an assoc array.
Hope this will save someone a headache! ;)`
wysocki at wildworld dot net
11-Feb-2005 01:15
`The dbase add and replace functions do NOT like to use the associative array.
<?
//This gives "unspecified error" on replace and add:
$row = dbase_get_record_with_names($db, 1);
unset($row['deleted']);
dbase_replace_record($db, $row, 1);
dbase_add_record($db, $row);
//To further prove the point,
//The first add works, the second one fails:
$testrow1=array('one','2','three');
dbase_add_record($db,$testrow1);
$testrow2=array('FIELDA' => 'xxx','FIELDB' => '9','FIELDC' => 'yyyyy');
dbase_add_record($db,$testrow2);
?>`
ptorres at ensenada dot net
16-Jan-2002 05:45
`how replace a record ?
This example delete a record and
add a record with a new field value.
// createtable.php
<?
$dbname = "players.dbf";
$def = array
(
array("record", "N",8,0),
array("account", "C",10),
array("password", "C",5),
array("name", "C",30)
);
if (!dbase_create($dbname, $def))
{
print "<strong>Error! <BR> Try: chmod 707 tabledirectory</strong>";
}
else
{
print "<strong>The table ".$dbname." was created !</strong>";
}
?>
// addrecord.php
// just a test, you can add more features to send the data by a html form and validate the account
<?
$db=dbase_open("players.dbf",);
$nextrecord = 1;
for ($a=1; $a <= $nr; $a++)
{
$rec = dbase_get_record($db, $a);
$nextrecord = $rec[0]+1;
}
$def = array ($nextrecord,'hsanchez', 'hugol', 'hugo sanchez');
dbase_add_record($db, $def);
dbase_close($db);
?>
// modify.htm
<FORM ACTION='modify.php' METHOD='post'>
<TABLE BORDER='9'>
<TR>
<TD>Account</TD><TD>Password</TD><TD>Name</TD>
</TR>
<TR>
<TD><INPUT TYPE='TEXT' SIZE='10' MAXLENGTH='10' NAME='f_account></TD><TD><INPUT TYPE='PASSWORD' SIZE='5' MAXLENGTH='5' NAME='f_password'></TD><TD><INPUT TYPE='TEXT' SIZE='30' MAXLENGTH='30' NAME='f_name'></TD>
</TR>
</TABLE>
<BR>
<INPUT TYPE='Submit' VALUE='Modify'><BR>
</FORM>
</CENTER>
</BODY>
</HTML>
// modify.php
<?
$exist = 0;
$db=dbase_open("players.dbf",0);
$nr = dbase_numrecords($db);
if ($nr==0)
{
print "There's no players !";
}
else
{
for ($a=1; $a <= $nr; $a++)
{
$rec = dbase_get_record($db, $a);
if ( !strcasecmp (trim($rec[1]), trim($f_account)) and !strcasecmp (trim($rec[2]), trim($f_password)))
{
$exist = 1;
$nrm = $a;
break;
}
}
}
dbase_close($db);
if ($exist == 1) {
if ($f_name and $f_account and $f_password)
{
$db=dbase_open("players.dbf",2);
dbase_delete_record($db,$nrm);
dbase_pack($db);
dbase_close($db);
$db=dbase_open("players.dbf",2);
$def = array($nrm,trim($f_account),$f_password,trim($f_name));
dbase_add_record($db, $def);
print "The name was updated !";
}
else
{
print "<strong>Error, enter alll data!</strong>";
}
}
if ($exist == 0)
{
print "<strong>Error, the data is not valid !</strong>";
}
?>
That's all !
I know that there is a dbase-replace-record function but i could use it.`
add a note
DBM dbase_pack

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST