我創建了一個論壇,人們可以註冊/登錄以發佈主題和回覆。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.";
}
}
仍然不工作,只是追溯到指數
如果可以的話,你應該[停止使用'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)。 –
[您的腳本存在SQL注入風險。](http:// stackoverflow。com/questions/60174/how-can-i-prevent-sql -injection-in-php) –
'topics'表中沒有'uid'列? – Rahul