mysql - Deleting single row from database -
mysql - Deleting single row from database -
i'm trying delete single row database based on it's id. user should able click delete button , in row should deleted - based on code have below, kinda works, deletes rows table opposed 1 specific row based on id.
i appreciate code see may not best practice, in basic testing phase personal project me improve larn php.
what doing wrong?
try { $results = $db->query("select * property_details order id asc"); } grab (exception $e) { echo "data not retrieved database."; exit; } $property = $results->fetchall(pdo::fetch_assoc); echo '<form method="post">'; echo '<table><th> property name </th><th> description </th><th> host name </th><th> location </th><th> cost </th>'; foreach ($property $property_details) { $id = $property_details['id']; echo '<tr>'; echo '<td>'; echo $property_details['property_name'] . '<br />'; echo '</td>'; echo '<td>'; echo $property_details['property_description'] . '<br />'; echo '</td>'; echo '<td>'; echo $property_details['host_name'] . '<br />'; echo '</td>'; echo '<td>'; echo $property_details['location'] . '<br />'; echo '</td>'; echo '<td>'; echo "£" . $property_details['price'] . " per night"; echo '</td>'; echo '<td>'; echo '<input type="submit" name="deleteitem" value="delete id '.$id.'" />'; echo '</td></tr>'; if(isset($_post['deleteitem'])) { seek { $results = $db->prepare("delete property_details id = :theid "); $results->bindparam(':theid', $id); $results->execute(); } grab (exception $e) { echo "could not insert info in database"; } } } echo '</table>'; echo '</form>';
crude way of sorting out processing.
this takes calue of deleteitem, explodes out based on space , uses 3rd item id of item delete:-
<?php if(isset($_post['deleteitem'])) { list($filler, $filler2, $id_to_delete) = explode(' ', $_post['deleteitem']); if (is_numeric($id_to_delete)) { seek { $results = $db->prepare("delete property_details id = :theid "); $results->bindparam(':theid', (int)$id_to_delete); $results->execute(); } grab (exception $e) { echo "could not delete info database"; } } } seek { $results = $db->query("select id, property_name, property_description, host_name, location, cost property_details order id asc"); } grab (exception $e) { echo "data not retrieved database."; exit; } $property = $results->fetchall(pdo::fetch_assoc); echo '<form method="post">'; echo '<table><th> property name </th><th> description </th><th> host name </th><th> location </th><th> cost </th>'; foreach ($property $property_details) { $id = $property_details['id']; echo '<tr>'; echo '<td>'; echo $property_details['property_name'] . '<br />'; echo '</td>'; echo '<td>'; echo $property_details['property_description'] . '<br />'; echo '</td>'; echo '<td>'; echo $property_details['host_name'] . '<br />'; echo '</td>'; echo '<td>'; echo $property_details['location'] . '<br />'; echo '</td>'; echo '<td>'; echo "£" . $property_details['price'] . " per night"; echo '</td>'; echo '<td>'; echo '<input type="submit" name="deleteitem" value="delete id '.$id.'" />'; echo '</td></tr>'; } echo '</table>'; echo '</form>'; ?>
probably should check 3rd part valid item delete (ie, valid id)
mysql sql database pdo
Comments
Post a Comment