从mysql表中获取post的id

时间:2014-06-16 05:29:35

标签: php mysql unique-id

我在表中有帖子,每个表都有唯一的ID。我想使用此id让用户从mysql表中删除他或她的帖子。我怎样才能找到桌子的唯一ID?

while($posts=mysqli_fetch_array($data)){
$tasks=$posts['task'];
$time=$posts['time'];
$date=$posts['date'];
$imp=$posts['important'];
$del=$posts['delete'];
$id=$posts['id'];

//define delete button
$button='<form method="post" action="remindMe.php"><input type="submit" value="Delete Post" name="delete" id='.$id.' /></form>';


if($imp==0 && $del != 0){
echo '<p id="tasks">'.$tasks. '</p><p id="date">'.$date.'</p><p id="time">'.$time.'</p>'.$button.'     <hr />';
}
}


 //see if button is clicked
if(isset($_POST['delete'])){
  $query="UPDATE `task` SET `delete`=0 WHERE `id`='/*id from mysql table*/'";
  mysqli_query($con, $query)
    or die(mysqli_error($con));
}

2 个答案:

答案 0 :(得分:1)

从表单中发布id并在提交中检索它,然后执行删除操作。如果你愿意,你可以使用像隐藏元素甚至是ajax之类的东西。 试试这个

$button='<form method="post" action="remindMe.php">
           <input type="submit" value="Delete Post" name="delete" id='.$id.' />
           <input type="hidden" name="del_id" value='.$id.'>
         </form>';

//查看是否点击了按钮

if(isset($_POST['delete'])){
  $delid = $_POST['del_id'];
  $query="UPDATE `task` SET `delete`=0 WHERE `id`= $delid";
  mysqli_query($con, $query)
    or die(mysqli_error($con));
}

答案 1 :(得分:0)

您的$button变量显示在while循环内。因此,它可能会随着新id值的更改而迭代。我在echo

之间没有看到while声明

无论哪种方式 如果您想使用POST方法,请使用hidden变量

//define delete button
$button='<form method="post" action="remindMe.php">
                 <input type="hidden" name="post_id" value='.$id.' />
                 <input type="submit" value="Delete Post" name="delete" id='.$id.' /> 
              </form>';

添加您的查询将是这样的,

$post_id = $_POST['post_id'];
$query="UPDATE `task` SET `delete`=0 WHERE `id`='".$post_id."'";