简体   繁体   中英

PHP buttons to delete a row in MYSQL table output

My goal is to display the entire table eg SELECT * FROM athlete and include button for each row in the table output, that when pressed will remove that row from the database.

I have followed another post and shown below is what I have so far but does not work:

//if(isset($_POST['delete_id']) && (!empty($_POST['delete_id']))) 
if(isset($_POST['Delete'])){ 

$delete_id = mysql_real_escape_string($_POST['delete_id']); 
echo("deletebutton" . $delete_id);
//mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id); 
//header('Location: main.php'); 
} 

else
{
$query = 'SELECT * FROM athlete';
$result = mysql_query($query);


$self = $_SERVER['PHP_SELF'];
$self = htmlentities($_SERVER['PHP_SELF']); // strip tags to improve security

echo("<table> 
<tr> 
<td> id </td> 
<td>last name</td> 
<td>first name</td> 
<td>delete data</td> 
</tr>");

while($row = mysql_fetch_array($result))
{ 
$id = $row['id'];

echo("<tr>");
        echo("<td>" . $row['id'] . "</td>");
        echo("<td>" . $row['lastName'] . "</td>");
        echo("<td>" . $row['firstName'] . "</td>");
        echo("<td> <form action='$self' method='post'> 
                <input type='hidden' name='delete_id value='$id' /> 
                <input type='submit' value='Delete' />  
            </form> 
        </td>
    </tr>");
}
mysql_free_result($result);
echo("</table>");

}

However this does not work. Does anyone know why. NOTE: I have left my login credentials out but I am connecting to the database elsewhere in the file.

The most probable cause is that your checking for $_POST['Delete'] having not given any of the submit buttons the attribute 'name' of 'Delete'. However, the line you have commented out above should work?

I think you should look into this areas

//if(isset($_POST['delete_id']) && (!empty($_POST['delete_id']))) 

if(isset($_POST['Delete'])){

and

<input type='submit' value='Delete' />

You should include: <input type='submit' name="Delete" value='Delete' />

That should solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM