|
dbase_replace_record
Replaces a record in a database
(PHP 4, PHP 5)
Parameters
ExamplesExample 483. Updating a record in the database<?php
See Also
Code Examples / Notes » dbase_replace_recordwysocki
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); ?> hassan
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! ;) ptorres
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! 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> <INPUT TYPE='Submit' VALUE='Modify'> </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. |