database - How to get $id to delete in list item? -
i create function list_product
show list product.
i want have button delete
on each product on list products.
but can't $id
delete.
have method delete rows on list database?
my function like:
<?php function product_list() { ?> <div class="wrap"> <?php global $wpdb; $table_name = $wpdb->prefix . "product"; if (isset($_post['delete'])) { $wpdb->query($wpdb->prepare("delete $table_name id = %s", $id)); } else { $rows = $wpdb->get_results("select id,name $table_name"); } ?> <table> <tr> <th>id</th> <th>name</th> </tr> <?php foreach ($rows $row) { ?> <tr> <td><?php echo $row->id; ?></td> <td><?php echo $row->name; ?></td> <td><a href="<?php echo admin_url('admin.php?page=product_update&id=' . $row->id); ?>">cập nhật</a></td> <td><input type='submit' name="delete" value='delete' class='button' onclick="return confirm('¿ confirm delete?')"></td> </tr> <?php } ?> </table> </div> <?php }
thanks.
i not php expert think here going on.
not sending $id
variable anywhere. also, there isn't form in code. since aren't using js submission, there isn't submission.
try following code.
<?php function product_list() { ?> <div class="wrap"> <?php global $wpdb; $table_name = $wpdb->prefix . "product"; if(isset($_post['row_id'])){ $id = $_post['row_id']; $id = $id[0]; }else{ $id = ''; } if (isset($_post['delete'])) { $wpdb->query($wpdb->prepare("delete $table_name id = %s", $id)); } else { $rows = $wpdb->get_results("select id,name $table_name"); } ?> <form method="post"> <table> <tr> <th>id</th> <th>name</th> </tr> <?php foreach ($rows $row) { ?> <tr> <td><?php echo $row->id; ?></td> <td><?php echo $row->name; ?></td> <td><a href="<?php echo admin_url('admin.php?page=product_update&id=' . $row->id); ?>">cập nhật</a></td> <td> <input type="hidden" name="row_id[]" value="<?php echo $row->id; ?>"> <input type='submit' name="delete" value='delete' class='button' onclick="return confirm('¿ confirm delete?')"> </td> </tr> <?php } ?> </table> </form> </div> <?php }
Comments
Post a Comment