删除帖子php源码

时间:2014-06-05 14:59:28

标签: php post comments

我需要帮助。我创建了发布系统。我现在需要创建删除系统。我只需要把删除specefic的系统放到有人需要删除的帖子上。这是我的发布系统。

if(empty($_POST) === false){
 $status_data = array(
   'status' => $_POST['status'],
   'status_poster' => $_SESSION['user_id'],
   'date' => date('Y-m-d H:i:s')
   );
   update_status($id, $status_data, $user_id);
  }  
   if($profile_data['user_id']){

     ?>
        <form action="" method="post">
        <div class="field">
    <label for="Status" style="color: #7f7f7f; font-family: Cambria, Hoefler Text, Liberation Serif, Times, Times New Roman, serif;"></label>
    <textarea rows="4" cols="50" name="status"placeholder="say something" id="status_area" ></textarea>
    <div id="button">
    <input name="user_status" type="submit" value="Post">
    </div>
    </div>
       </form>
       </div>
       <div id="status">
        <?php
    if($_Get['delete']){
   $id = $_GET['delete'];
    $sql = "DELETE FROM status WHERE id='$id'";
    $res = mysql_query($sql) or die(mysql_error());
    echo"<meta http-equiv = 'refresh' content =' 0;url=index.php'>";
    }
      ?>
     <?php
    }
    if(user_exists($username) === true){
$user_id         = user_id_from_username($username);
$profile_data    = user_data($user_id, 'first_name', 'last_name', 'user_id');

    $find_post = mysql_query("SELECT * FROM status ORDER BY date DESC");
    while($rows = mysql_fetch_assoc($find_post)){
$post_id = $rows['id'];
$first_name = $profile_data['first_name'];
$last_name = $profile_data['last_name'];
$username = $profile_data['username'];
$user = $user_data['user_id'];
$data = $rows['date'];
$post_name = $rows['status_poster'];
$post = $rows['status'];
    if( $user_id === $post_name){
    ?>

    <div class='holder'>
   <div class='holder_half'>
   <?php
echo '<div id="picture">
<img src="', $profile_data['profile_picture'], '" alt="', $profile_data['first_name'],'\'s Profile image">';
echo "
</div>
<div class='ten'>
<div id='statusname'><a href='#'>$first_name $last_name</a></div>
<div class='date_time'>posted at $data</div>
<span class='span' data-ft='{&quot;tn&quot;:&quot;K&quot;}'>
<div id='statustext'>$post</div>
</span>
</div>
</div>
<textarea rows='4' cols='50' id='comment' name='comment' placeholder='' id='status_area' ></textarea>
</div>
";
        }
    }
 }
   }
   ?>   
   </div>
    </div>if(empty($_POST) === false){
$status_data = array(
'status' => $_POST['status'],
'status_poster' => $_SESSION['user_id'],
'date' => date('Y-m-d H:i:s')
);
update_status($id, $status_data, $user_id);
    }
   if($profile_data['user_id']){
      ?>
     <form action="" method="post">
<div class="field">
    <label for="Status" style="color: #7f7f7f; font-family: Cambria, Hoefler Text, Liberation Serif, Times, Times New Roman, serif;"></label>
    <textarea rows="4" cols="50" name="status"placeholder="say something" id="status_area" ></textarea>
    <div id="button">
    <input name="user_status" type="submit" value="Post">
    </div>
 </div>
    </form>
   </div>
   <div id="status">
   <?php
   if($_Get['delete']){
$id = $_GET['delete'];
$sql = "DELETE FROM status WHERE id='$id'";
$res = mysql_query($sql) or die(mysql_error());
echo"<meta http-equiv = 'refresh' content =' 0;url=index.php'>";
   }
    ?>
      <?php
        }
   if(user_exists($username) === true){
$user_id         = user_id_from_username($username);
$profile_data    = user_data($user_id, 'first_name', 'last_name', 'user_id');

    $find_post = mysql_query("SELECT * FROM status ORDER BY date DESC");
    while($rows = mysql_fetch_assoc($find_post)){
$post_id = $rows['id'];
$first_name = $profile_data['first_name'];
$last_name = $profile_data['last_name'];
$username = $profile_data['username'];
$user = $user_data['user_id'];
$data = $rows['date'];
$post_name = $rows['status_poster'];
$post = $rows['status'];
    if( $user_id === $post_name){
    ?>
     <div class='holder'>
     <div class='holder_half'>
      <?php
echo '<div id="picture">
<img src="', $profile_data['profile_picture'], '" alt="', $profile_data['first_name'],'\'s Profile image">';
echo "
</div>
<div class='ten'>
<div id='statusname'><a href=''>$first_name $last_name</a></div>
<div class='date_time'>posted at $data</div>
<span class='span' data-ft='{&quot;tn&quot;:&quot;K&quot;}'>
<div id='statustext'>$post</div>
</span>
</div>
</div>
<textarea rows='4' cols='50' id='comment' name='comment' placeholder='' id='status_area' ></textarea>
</div>
  ";
        }
    }
}
      }
    ?>  
     </div>
    </div>

1 个答案:

答案 0 :(得分:0)

我在代码中看到2个删除查询但你问的是如何创建一个..这是mysqli预备语句版本(防止sql注入)..

$database = new mysqli('localhost', 'root', 'password', 'database_name');
//you select like this
$stmt  = $database->prepare("select * from status order by date desc");
$stmt->execute();

$result = $stmt->get_result();

while($rows = $result->fetch_assoc()){

  //the rows here
  }

删除...我不确定您是否要在单独的页面上执行删除操作,这意味着您单击帖子并将其带到第二页,您将看到删除按钮,或[在同一页面上]删除通过选中每个帖子的复选框来发布帖子。如果你正在使用第一种方法,下面的代码将删除它,但首先你需要通过url将id传递给第二页,如点击

$id = $_GET['id']

$stmt = $database->prepare("DELETE from status where id = ?");
$stmt->bind_param('i', $id);//you could put 's' if the id contains other than numbers
$stmt->execute();

这将删除id匹配的帖子。