<input type="Button"> not deleting record properly
Posted: Fri Aug 06, 2010 8:23 am
Hello Experts,
I'm a beginner trying to learn PHP and have run into a problem that I can't seem to figure out. I've spent countless hours Google'ing, but haven't found any examples that help. Any help pointing me in the right direction would be greatly appreciated.
My code below is the second part of a webpage where I'm uploading a .pdf document to a MySQL db table. The first part of the webpage, which is not shown - is the browse for file and upload buttons. It works as expected by uploading file and assigning an 'id' and 'name'.
The code below works properly, by retrieving a list of file names in the 'documents' table and displaying the filenames in ASCending order by URL with "=$id". The problem I'm having is when I attempt to put a DELETE button next to each record. Once I put the code, that I think should work, all sorts of weird things start happening; like all records being deleted when I click the refresh button. The strange part is that I've assigned a matching $id number to the button, but I don't even have to click it before it deletes all the records.
Here it is WITHOUT the offending DELETE button:
...and the code I'm using directly after the VALUE="Delete"/> that is causing all the problems.
I've also tried it with onClick, but that doesn't help either.
So, the WHILE statement creates a URL list of all the uploaded files with unique $id and a delete button next to each URL with the same corresponding $id. It seems, to me, that it should only delete the matching $id, but that's not the case.
I did a test to see if the $id is being seen and it is, so why is the Delete button code deleting everything instead of just the corresponding record?
Thank you in advance for your help...
I'm a beginner trying to learn PHP and have run into a problem that I can't seem to figure out. I've spent countless hours Google'ing, but haven't found any examples that help. Any help pointing me in the right direction would be greatly appreciated.
My code below is the second part of a webpage where I'm uploading a .pdf document to a MySQL db table. The first part of the webpage, which is not shown - is the browse for file and upload buttons. It works as expected by uploading file and assigning an 'id' and 'name'.
The code below works properly, by retrieving a list of file names in the 'documents' table and displaying the filenames in ASCending order by URL with "=$id". The problem I'm having is when I attempt to put a DELETE button next to each record. Once I put the code, that I think should work, all sorts of weird things start happening; like all records being deleted when I click the refresh button. The strange part is that I've assigned a matching $id number to the button, but I don't even have to click it before it deletes all the records.
Here it is WITHOUT the offending DELETE button:
Code: Select all
<?php
include ("dbconnect.php");
$query = "SELECT id, name FROM documents ORDER BY name ASC";
$result = mysql_query($query,$con) or die('Error, query failed');
echo "<form name=\"doc_view\" action=\"\" method=\"post\">" ;
echo "<table width=\"400\" cellspacing=\"2\">" ;
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<tr>
<td width="336"><a href="../modules/mydocuments.php?id=<? echo $id; ?>"><? echo $name; ?></a><br></td>
<td width="52"><input name="Delete" type="button" value="Delete"/></td>
</tr>
<?
}
echo "</table>" ;
echo "</form>" ;
mysql_close($con);
?>
Code: Select all
onSubmit="<?php mysql_query("DELETE FROM documents WHERE id='$id'") or die(mysql_error());?>"/>
So, the WHILE statement creates a URL list of all the uploaded files with unique $id and a delete button next to each URL with the same corresponding $id. It seems, to me, that it should only delete the matching $id, but that's not the case.
I did a test to see if the $id is being seen and it is, so why is the Delete button code deleting everything instead of just the corresponding record?
Thank you in advance for your help...