jquery - Delete li entry in database with PHP -
i working on website user can enter money owes friend. on 1 page user can see entrys made far , should able delete them aswell. entrys inserted in db. overview page use following structure:
<ul class="list-group"> <?php $isempty = true; while ($data = mysql_fetch_array($res)) { if ($data['isdebt'] == 0) { $isempty = false; ?> <form method="post" action="<?php echo htmlspecialchars($_server['php_self']); ?>" autocomplete="off"> <li class="list-group-item" name="debt_id" value="<?php echo $data['debt_id'] ?>"><span class="badge"><?php echo $data['value']; ?></span><?php echo $data['name']; ?> <button name="delete" type="submit" id="delete" class="btn btn-xs btn-danger pull-right"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> </button> </li> </form> <?php } } if ($isempty) { ?> <li class="list-group-item">no debt!</li> <?php } ?> </ul>
one debt entry has following values:
- debt_id (numeric)
- name (like "pizza jack")
- value (the price. example "10")
the button each li element should delete entry in db via debt_id. i've used following sql statement far:
delete debt debt_id='$debt_id';
in php looked this:
if (isset($_get['delete'])) { $debt_id = trim($_post['debt_id']); $res = mysql_query("delete debt debt_id='$debt_id';"); if ($res) { $errtyp = "success"; $errmsg = "successfully deleted"; } else { $errtyp = "danger"; $errmsg = "error occured"; } }
my problem right not able debt_id i've entered php in li
element. know better approach or doing wrong? developping cloud9 invite if interested.
when load debts, can select
statement , loop through result set. in loop can put id in hidden inputs in each li element:
$sql = "select debt_id, name, value debts user_id = " . $user_id; $result = $conn -> query($sql); if ($result -> num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo '<li> <span>' . $row["name"] . '</span> <span>' . $row["value"] . '</span> <input type="hidden" value="' . $row['debt_id'] . ' name="id-to-delete"> </li>'; } } else { echo "no results"; }
the delete.php file should contain:
<?php if (isset($_post['id-to-delete'])) { $idtodelete = $_post['id-to-delete']; // sql delete record $sql = 'delete debts debt_id=' . $idtodelete; if ($conn->query($sql)) { echo "record deleted successfully"; } else { echo "error deleting record: " . $conn->error; } } ?>
Comments
Post a Comment