Forum Moderators: coopster
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.
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";
Regards,
Arjan
<?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
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]
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