Reputation: 1
I'm writing a php script for my form-mail script to post all emails sent to a pipe delimited csv file, I then display the csv file via a php array. I need to allow the user to delete an entry by passing the id/invoice number of the listing which is the last or 22nd field in the line from that listing to a delete function as shown below, but it's deleting everything in the csv file, not just that line. Here's my code. I compare the id passed to the invoicenumber if it matches it should delete that line and put the rest back in, but it's deleting all entries.
I made your corrections as follows but it's still wiping out all entries:
if ($_POST['action'] == "Delete")
{
$id = $_GET['id'];
$fptemp = fopen('data-temp.csv', "a+");
if (($handle = fopen($dataFile, "r")) !== FALSE)
{
while (($data= fgetcsv($handle)) !== FALSE)
{
if ($id != $data[22])
{
fputcsv($data);
}
}
fclose($handle);
fclose($fptemp);
unlink($dataFile);
rename('data-temp.csv',$dataFile);
}
}
so I have the following entries in my csv file
1346785585|Martha|Stewart|[email protected]|555-444-2222|578 dude rd|dude|TN|57660|usa|Do it Yourself Cooking Book||389|12|||360|12|Credit Card|www.something.com||98.143.4.228|6c67b98
1346785588|Karen|b|[email protected]|555-444-2222|578 dude rd|dude|TN|57660|usa|Do it Yourself WebSite||389|12|||360|12|Credit Card|www.something.com||98.143.4.228|464b4ec
I want to find invoice number which is the very last field in the line, so let's say I want to find "464b4ec" and delete the entire entry consisting of
1346785588|Karen|b|[email protected]|555-444-2222|578 dude rd|dude|TN|57660|usa|Do it Yourself WebSite||389|12|||360|12|Credit Card|www.something.com||98.143.4.228|464b4ec
I want to leave all other entries alone, how would I do that?
Upvotes: 0
Views: 377
Reputation: 781096
Two fixes. As others have pointed out, you're using $id
for two things. Change:
$id= fgetcsv($handle)
to:
$data = fgetcsv($handle, 0, '|')
Also, when you're writing the output file, you're wrapping an array in another array, which isn't needed. Change:
$list = array($data);
fputcsv($fptemp, $list);
to:
fputcsv($data);
Upvotes: 1
Reputation: 360702
A few things
a) fgetcsv() returns an array of fields from the line of csv data just read. You compare that entire array to a (single?) item, $data[22]
. Unless data[22] is itself an array, this is not going to work as you expect
if $data is NOT an array, it'd be treated as a string, and you're comparing $id against the 22nd character in the string.
b) $data is already an array, which you then wrap in another array - array($data)
. So... i'm guessing $data is actually a string.
c) since you only do fputcsv if your id v.s data comparison DOESN'T match, you aren't deleting just a field - you skip the entire line. To delete the 22nd item in the array only, it'd have to be
if ($id == $data[22) {
unset($data[22]);
}
fputcsv($fptemp, $data);
d) and as Jack points out in his comment , you're overwriting $id in your loop, so the $_POST value you had in it is destroyed the moment you read your first line of csv data.
Upvotes: 0