updating a database (PHP)

Status
Not open for further replies.

Murdoc

In Runtime
Messages
109
Hello,

So far I've been working on a script that will update a user's databse. I am currently NOT using MYSQL at the moment since I can't get it to work properly yet.

Each user has it's own serperate file which data will be stored in (eg: admin.dat).

So i made a page submitting the variables and the check boxes. I've made a script that will 1) check to see which sections of the forum have been checked off (indicating for an update) and which ones are not. 2) It will read in all the data that have been checked off and store it into a file. Since the information is read only 1 line across I did not put a while loop and the !feof command. I made it so that it will read in all the variables from the database and the form and for the information that is NOT modified, it will just take it and stick it back into the .dat file.

Eg: Murdoc 123

if I modify username Murdoc to admin then it should write to the admin.dat file saying

admin 123.

However, when I used the write command for the file, it only writes what is changed and doesn't include what is not changed as I expected.

Appearing like this:

admin

so when data from another php file loads, it cannot read the 123 which is supposed to be there still.

-------------------------------------------------------
SAMPLE SCRIPT
-------------------------------------------------------
this is what I have so far...

<?php
$user = $_REQUEST['user'];
$updtamt = $_REQUEST['UPamt'];#check if bank account update is on
$cash = $_REQUEST['money'];#requesting updated cash amount if updtamt is on
$ASamt = $_REQUEST['ASamt'];#Check if Add/Subtract amount from account is needed
$asamt = $_REQUEST['asamt'];#requesting amount to be added/subtracted
$UPact = $_REQUEST['UPact'];#Check if account type needs to be updated
$AT = $_REQUEST['AT'];#Requesting New account type

$fp=fopen("$user.dat","r+") or die("Error(102):Database Failure");
$get =trim (fgets($fp,512));
list ($Camt,$AccType) = explode ("\t",$get);#Reading in Current Amount and Accou
nt Type
if ($updtamt == "on" && $ASamt =="" && $UPact =="")
{
$amt = trim (fwrite($fp,"$cash\t$AccType"));#Changing balance
echo "New Amount stored

\n";
echo "Go back to Control Central";

fclose($fp);
}

else if ($updtamt =="" && $ASamt =="on" && $UPact =="")
{
$Namt = $Camt + $asamt;
$Namt2 = trim(fwrite($fp,"$Namt\t$AccType"));#updating balance
echo "Amount Updated

\n";
echo "Go back to Control Central";
fclose($fp);
}
else if ($updtamt =="" && $ASamt =="" && $UPact =="on")
{
$Nat = trim (fwrite($fp,"$Camt\t$AT"));#Updating Account type
echo "Account type updated

\n";
echo"Go back to Control Central";

fclose($fp);
}

?>

with the "r+" in the fopen command, I am able to write both as expected, however it only writes to the next line which I want it to completely overwrite the first line and just repalce the old data with the new data. I dunno why it doens't work with the "w" file open command. = / any ideas?
 
Status
Not open for further replies.
Back
Top Bottom