2015-06-18 46 views
-1

我創建了一個論壇,人們可以註冊/登錄以發佈主題和回覆。PHP MYSQL:用戶刪除自己的帖子

現在我在每個主題旁邊添加了一個刪除鏈接,如果按下的話將會刪除topic.php,並且如果用戶創建了這個主題,它將被刪除,如果沒有,它會說你沒有創建這個主題。

這是deletetopic.php

<?php 
session_start(); 
include("config.php"); 

if(!isset($_SESSION['uid'])){ 
      echo "<p><b>ERROR: Please log in to delete a topic."; 
     } 

if(isset($_SESSION['username'])) 
{ 
    $uid = $_SESSION['uid']; 
    $id=$_GET['id']; 

    $query1=mysql_query("delete FROM topics WHERE id='$id' and uid='$uid'"); 
    if($query1){ 
     header('location:index.php'); 
    } 
    else{ 
     echo "<p><b>ERROR: You didnt make this topic."; 
    } 
} 

它不工作,它只是給我的其他{}錯誤

這裏是我的表:

CREATE TABLE `users` (
    `id` INT(11) NOT NULL AUTO_INCREMENT, 
    `firstname` VARCHAR(255) NOT NULL, 
    `lastname` VARCHAR(255) NOT NULL, 
    `email` VARCHAR(255) NOT NULL, 
    `username` VARCHAR(255) NOT NULL, 
    `password` VARCHAR(100) NOT NULL, 

    PRIMARY KEY (`id`) 

CREATE TABLE `topics` (
    `id` INT(11) NOT NULL AUTO_INCREMENT, 
    `categoryID` TINYINT(4) NOT NULL, 
    `topicTitle` VARCHAR(150) NOT NULL, 
    `topicCreator` INT(11) NOT NULL, 
    `topicLastUser` INT(11) NOT NULL, 
    `topicDate` DATETIME NOT NULL, 
    `topicReplyDate` DATETIME NOT NULL, 
    `topicViews` INT(11) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`id`) 

編輯:

uid來自這裏我認爲:login.php

if (isset($_POST['username'])){ 
 
    $username = $_POST['username']; 
 
    $password = $_POST['password']; 
 
    $sql = "SELECT * FROM users WHERE username='".$username."' AND password='".$password."' LIMIT 1"; 
 
    $result = mysql_query($sql) or die(mysql_error()); 
 
    if (mysql_num_rows($result) == 1){ 
 
     $row = mysql_fetch_assoc($result); 
 
     $_SESSION['uid'] = $row['id']; 
 
     $_SESSION['username'] = $row['username']; 
 
     header("Location: index.php"); 
 
     exit(); 
 
    }else{ 
 
     echo "<p>Invalid information. Please return to the previous page."; 
 
     exit(); 
 
    } 
 
}

更新

if(isset($_SESSION['username'])) 
 
{ 
 
    $uid = $_SESSION['uid']; 
 
    $id=$_GET['id']; 
 
    
 
    $check = mysql_query("SELECT * FROM topics WHERE id = '$id' AND topicCreator = '$uid'"); 
 
    if($check){ 
 
     $query1=mysql_query("delete FROM topics WHERE id='$id' AND topicCreator='$uid'"); 
 
     header('location:index.php'); 
 
    } 
 
    else{ 
 
     echo "<p><b>ERROR: You didnt make this topic."; 
 
    } 
 
}

仍然不工作,只是追溯到指數

+1

如果可以的話,你應該[停止使用'mysql_ *'功能(http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-功能合PHP)。他們不再被維護,並[正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。瞭解[準備](http://en.wikipedia.org/wiki/Prepared_statement)[聲明](http://php.net/manual/en/pdo.prepared-statements.php),並考慮使用PDO ,[這真的不難](http://jayblanchard.net/demystifying_php_pdo.html)。 –

+0

[您的腳本存在SQL注入風險。](http:// stackoverflow。com/questions/60174/how-can-i-prevent-sql -injection-in-php) –

+3

'topics'表中沒有'uid'列? – Rahul

回答

2

有表topics沒有uid列,它我Š大概topicCreator

$query1=mysql_query("delete FROM topics WHERE id='$id' and topicCreator='$uid'");

你應該考慮離開這裏大約從MySQL切換到庫MySQLi或PDO的意見。並使用準備好的語句來防止SQL注入。

還有一個問題。在刪除主題之前,您需要檢查用戶是否是topicCreator。

$check = mysql_query("SELECT * FROM topics WHERE id = '$id' AND topicCreator = '$uid'"); 

if($check){ 
// Allow deletion 
} 
else{ 
// Don't allow deletion 
} 
0

好吧,這裏是我的代碼運行良好,但有一個問題。 當用戶不是帖子的創建者時,是的,它不刪除帖子,但它沒有說錯誤:你沒有發佈這個。

因此,基本上,在兩種情況下,它說主題已被刪除,即使它不刪除它。 else語句不運行。

任何想法爲什麼?

if(!isset($_SESSION['uid'])){ 
 
      echo "<p><b>ERROR: Please log in to delete a topic."; 
 
     } 
 
     
 
if(isset($_SESSION['username'])) 
 
{ 
 
    $uid = $_SESSION['uid']; 
 
    $id=$_GET['id']; 
 
    
 
    $check = mysql_query("SELECT * FROM topics WHERE id = '$id' AND topicCreator = '$uid'"); 
 
    if($check){ 
 
     $query1=mysql_query("delete FROM topics WHERE id='$id' AND topicCreator='$uid'"); 
 
     echo "<p>Topic has been successfully deleted. <a href='index.php'>Click here to return to home page.</a>"; 
 
    } 
 
    else{ 
 
     echo "<p><b>ERROR: You didnt make this topic."; 
 
    } 
 
}