PHP MYSQL: User Delete his own post -
i have created forum people can register/login post topics , replies.
now added delete link next each topic if pressed go deletetopic.php , if user has created topic deleted, if not, didn't create topic.
this deletetopic.php
<?php session_start(); include("config.php"); if(!isset($_session['uid'])){ echo "<p><b>error: please log in delete topic."; } if(isset($_session['username'])) { $uid = $_session['uid']; $id=$_get['id']; $query1=mysql_query("delete topics id='$id' , uid='$uid'"); if($query1){ header('location:index.php'); } else{ echo "<p><b>error: didnt make topic."; } }
it doesnt work, gives me else {error}
here tables:
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`)
edit:
uid comes here think: login.php
if (isset($_post['username'])){ $username = $_post['username']; $password = $_post['password']; $sql = "select * users username='".$username."' , 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 previous page."; exit(); } }
update
if(isset($_session['username'])) { $uid = $_session['uid']; $id=$_get['id']; $check = mysql_query("select * topics id = '$id' , topiccreator = '$uid'"); if($check){ $query1=mysql_query("delete topics id='$id' , topiccreator='$uid'"); header('location:index.php'); } else{ echo "<p><b>error: didnt make topic."; } }
still doesnt work, goes index
there no uid
column in table topics
, topiccreator
:
$query1=mysql_query("delete topics id='$id' , topiccreator='$uid'");
you should consider comments left here changing mysql mysqli or pdo. , use of prepared statements prevent sql injections.
there problem. need check if user topiccreator before deleting topic.
$check = mysql_query("select * topics id = '$id' , topiccreator = '$uid'"); if($check){ // allow deletion } else{ // don't allow deletion }
Comments
Post a Comment