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

?>


add a note add a note User Contributed Notes
hassan at datakillarna dot se
11-Feb-2006 08: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
12-Feb-2005 05: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
17-Jan-2002 09: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.