homepage Welcome to WebmasterWorld Guest from 54.234.0.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
php flat file single row delete
php flat file single row delete
GearStudios




msg:3817401
 4:55 pm on Jan 1, 2009 (gmt 0)

Hello Webmaster World!

A noob here to php. I know enough to get myself in trouble.

I have been trying to delete a single row out of a flat file and can't get the code to work properly. The funny thing is I can delete using a different browser but not with internet explorer.

Here is the form that reads the file to allow you to click on a delete button.

<table id="list"><form name="list" action="listdelete.php" method="GET"><tr><td colspan="4" id="header"><h2>List</h2></td></tr><tr id="header"><td>Name</td><td>Age</td><td>Folder</td><td>Delete</td></tr>
<?php
$userinfo = file("../user/userdata.txt");
foreach($userinfo as $key => $val)
{
//explode that data into a new array:
$data[$key] = explode("##", $val);
}

for($k = 0; $k < sizeof($userinfo); $k++)
{
echo '<tr><td>'.$data[$k][0].'</td><td>'.$data[$k][1].'</td><td>'.$data[$k][2].'</td><td style="text-align: center;"><input type="image" src="images/button_delete.gif" name="username" value="'.$data[$k][0].'"></td></tr>';
}

echo '</table>';

}
?>

Here is the code that processes and deletes:


$some = $_GET['username'];
$file = file('../user/userdata.txt');
$data = '';
for ($i=0; $i<count($file); $i++)
{
if (!strstr($file[$i],$some))
{
$data .= $file[$i];
}
}
//Then delete the original file
if (file_exists('../user/userdata.txt'))
{
unlink('../user/userdata.txt');
}
//and write the new data
$fp = fopen('../user/userdata.txt',"w");
if ($fp)
{
fwrite($fp,$data);
fclose($fp);
}

Thanks for any help to teach me what I did wrong.

 

adb64




msg:3817412
 5:43 pm on Jan 1, 2009 (gmt 0)

Hi GearStudios, welcome to Webmaster World!

Your code looks fine to me. What exactly happens when using IE instead of eg FF?
You could add some debug info using "echo" to see whether the name you want to delete is correct. Add the following line after $some = $_GET['username'];

echo "<br><br>Name to delete: '$some'<br><br>\n";

BTW, there is not need to unlink the file in case it exists. When using fopen with "w" like you do, the existing file will be overwritten with the new data.

Regards,
Arjan

Mahabub




msg:3817413
 5:45 pm on Jan 1, 2009 (gmt 0)

Hi, I just take a look on your code and get that you have a extra brace in the below ocde.....

<?php
$userinfo = file("../user/userdata.txt");
foreach($userinfo as $key => $val)
{
//explode that data into a new array:
$data[$key] = explode("##", $val);
}// This is the extra brace..

for($k = 0; $k < sizeof($userinfo); $k++)
{
echo '<tr><td>'.$data[$k][0].'</td><td>'.$data[$k][1].'</td><td>'.$data[$k][2].'</td><td style="text-align: center;"><input type="image" src="images/button_delete.gif" name="username" value="'.$data[$k][0].'"></td></tr>';
}

echo '</table>';

}
?>

Thanks
Mahabub

RaffSnaff




msg:3817710
 8:15 am on Jan 2, 2009 (gmt 0)

Mahabub, I think that is the closing brace for the foreach loop. Correct me if I'm wrong though, I may have missed something.

edit: okay i see the extra brace. I'm just frazzled right now.

[edited by: RaffSnaff at 8:21 am (utc) on Jan. 2, 2009]

GearStudios




msg:3817889
 4:28 pm on Jan 2, 2009 (gmt 0)

Thank you for the replies. When I use IE the row won't delete. It goes through the motions but the row is still in the file. If I use another browser the row will delete. Is there something special that I need to do for IE to work?

Also thanks for pointing out the fact that I don't have to unlink the file. I am such a goof. ;)

[edited by: GearStudios at 4:40 pm (utc) on Jan. 2, 2009]

GearStudios




msg:3817894
 4:35 pm on Jan 2, 2009 (gmt 0)

Also if I remove the extra brace the rows no longer appear within the table but below it. It displays the first row then two blank rows, the table closes and then the first row is repeated with the rest after it outside the table. :(

Mahabub




msg:3817898
 4:47 pm on Jan 2, 2009 (gmt 0)

GearStudios,

can you post the content of userdata.txt file i want to test in my localhost.

Thanks
Mahabub

GearStudios




msg:3817908
 4:55 pm on Jan 2, 2009 (gmt 0)

Hi Mahabub,

Here is the data:


julie¦##¦23¦##¦3
susan¦##¦34¦##¦2
jason¦##¦19¦##¦2

Mahabub




msg:3817924
 5:18 pm on Jan 2, 2009 (gmt 0)

GearStudios,

I've test in my localhost your code work perfect except the extra brace. I made a mistake that is given below and i am extremely sorry for that...

<?php
$userinfo = file("../user/userdata.txt");
foreach($userinfo as $key => $val)
{
//explode that data into a new array:
$data[$key] = explode("##", $val);
} // This one not extra brace..

for($k = 0; $k < sizeof($userinfo); $k++)
{
echo '<tr><td>'.$data[$k][0].'</td><td>'.$data[$k][1].'</td><td>'.$data[$k][2].'</td><td style="text-align: center;"><input type="image" src="images/button_delete.gif" name="username" value="'.$data[$k][0].'"></td></tr>';
}

echo '</table>';

} // This one is the extra brace
?>

I tried with IE, OPERA, FIREFOX, CHROME, SAFARI with all browser it works fine.

Try using POST method. also check that when you use IE you get the value of username in listdelete.php

$some = trim($_GET['username']); //I used trim cause blank space before and after username can be a problem
echo $some; //check in your IE that you get the value properly..
$file = file('../user/userdata.txt');
$data = '';
for ($i=0; $i<count($file); $i++)
{
if (!strstr($file[$i],$some))
{
$data .= $file[$i];
}
}
$fp = fopen('../user/userdata.txt',"w");
if ($fp)
{
fwrite($fp,$data);
fclose($fp);
}

Thanks
Mahabub

GearStudios




msg:3817992
 6:47 pm on Jan 2, 2009 (gmt 0)

Hello,

I tried it again in IE and this is what I started getting now after adding trim():

Warning: strstr() [function.strstr]: Empty delimiter in listdelete.php on line 7

Line 7 is if(!strstr($file[$i],$some))

Neither post or get is sending info to the listdelete.php. :(

Mahabub




msg:3817999
 6:53 pm on Jan 2, 2009 (gmt 0)

GearStudios,

Which I thought in IE you didnt get the value of username in GET method. I dont know why, upgrade your IE.

check with Another IE from a different PC. I am sure its your IE problem. Your code is okay.

Thanks
mahabub

[edited by: Mahabub at 6:54 pm (utc) on Jan. 2, 2009]

GearStudios




msg:3818010
 7:17 pm on Jan 2, 2009 (gmt 0)

Thanks Mahabub. I wasn't sure if my code was good or not. I totally dislike IE. :(

I am using IE7 on a pc.

Mahabub




msg:3818014
 7:21 pm on Jan 2, 2009 (gmt 0)

GearStudios,

I assure you that your code is okay.....

Thanks
Mahabub

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved