Jump to content

Recommended Posts

I'm trying to be able to delete a specific post from a forum.

 

How do I make it so that my function knows which post to delete? I know it needs to pull the id from the boards table, but how do I do that? In the readboard function, I've added a link to try and pull the ID of that specific post, but I don't think I have it right...

 

Readboard function:

<?
$command = 1;
if (getlevel(get("plane"),coords(),"board") == "") echo "There is no bulletin board here.<br>\n"; else{
$board = getlevel(get("plane"),coords(),"board");
	if ($admin == "Y+" || $admin == "Y"){
	$x = " --- <a href=main.php?username=$username&password=$password&action=delpost>Delete Post</a>";}
	$dbh=dbconnect() or die ("Userlist read error: " . mysql_error()."<br>");
	mysql_select_db("XX");
	$result = mysql_query("SELECT * FROM boards WHERE boardname='$board' ORDER BY id LIMIT 10");
	$post = mysql_fetch_assoc($result);
	while ($row = mysql_fetch_array($result)){
  echo stripslashes($row['message']) . "$x<br>" . " --- ".stripslashes($row['username']). " on ".stripslashes($row['time']) ."<hr width=90%>";
}
}
?>

 

 

Here's my current delpost function:

function delpost() {
if (getlevel(get("plane"),coords(),"board") == "" && ( get("admin") == "Y+" || get("admin") == "Y")) echo "There is no bulletin board here.<br>\n"; else{
$board = getlevel(get("plane"),coords(),"board");
$dbh=dbconnect() or die ("Userlist read error: " . mysql_error()."<br>");
mysql_select_db("XX");
$result = mysql_query("DELETE * FROM `boards` WHERE `boardname`='$board', `id`='$id'");
echo "Message deleted.<br>\n";
}}

Link to comment
https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/
Share on other sites

If I understand correctly.. does each post have a unique id?

 

<a href=main.php?username=$username&password=$password&action=delpost>Delete Post</a>

 

to

 

<a href=main.php?username=$username&password=$password&action=delpost&postid=$postid>Delete Post</a>

 

and

 

$postid = $_GET['postid'];

 

$result = mysql_query("DELETE * FROM `boards` WHERE `boardname`='$board', `id`='$id' AND postid='$postid'");

Well then shouldnt you GET the id to delete it?

 

redboard

 


<?
$command = 1;
if (getlevel(get("plane"),coords(),"board") == "") echo "There is no bulletin board here.<br>\n"; else{



$board = getlevel(get("plane"),coords(),"board");





if ($admin == "Y+" || $admin == "Y"){





$x = " --- <a href=main.php?username=$username&password=$password&action=delpost&id=$id>Delete Post</a>";}





$dbh=dbconnect() or die ("Userlist read error: " . mysql_error()."<br>");





mysql_select_db("XX");





$result = mysql_query("SELECT * FROM boards WHERE boardname='$board' ORDER BY id LIMIT 10");





$post = mysql_fetch_assoc($result);





while ($row = mysql_fetch_array($result)){
  echo stripslashes($row['message']) . "$x<br>" . " --- ".stripslashes($row['username']). " on ".stripslashes($row['time']) ."<hr width=90%>";
}
}
?>

 

delpost

 

function delpost() {
if (getlevel(get("plane"),coords(),"board") == "" && ( get("admin") == "Y+" || get("admin") == "Y")) echo "There is no bulletin board here.<br>\n"; else{



$board = getlevel(get("plane"),coords(),"board");



$dbh=dbconnect() or die ("Userlist read error: " . mysql_error()."<br>");



mysql_select_db("XX");


$id = $_GET['id'];
$result = mysql_query("DELETE * FROM `boards` WHERE `boardname`='$board', `id`='$id'");



echo "Message deleted.<br>\n";
}}

 

Doesn't this work?

Hmm, no errors, but it doesn't delete it either.

 

Here's what I have now:

 

$command = 1;
if (getlevel(get("plane"),coords(),"board") == "") echo "There is no bulletin board here.<br>\n"; else{
$board = getlevel(get("plane"),coords(),"board");
	if ($admin == "Y+" || $admin == "Y"){
	$x = " --- <a href=main.php?username=$username&password=$password&action=delpost&id=$id>Delete Post</a>";}
	$dbh=dbconnect() or die ("Userlist read error: " . mysql_error()."<br>");
	mysql_select_db("XX");
	$result = mysql_query("SELECT * FROM boards WHERE boardname='$board' ORDER BY id LIMIT 10");
	$post = mysql_fetch_assoc($result);
	while ($row = mysql_fetch_array($result)){
  echo stripslashes($row['message']) . "$x<br>" . " --- ".stripslashes($row['username']). " on ".stripslashes($row['time']) ."<hr width=90%>";
}
}

function delpost() {
if (getlevel(get("plane"),coords(),"board") == "" && ( get("admin") == "Y+" || get("admin") == "Y")) echo "There is no bulletin board here.<br>\n"; 

else{
$board = getlevel(get("plane"),coords(),"board");
$dbh=dbconnect() or die ("Userlist read error: " . mysql_error()."<br>");
mysql_select_db("XX");
$id = $_GET['id'];
$result = mysql_query("DELETE * FROM `boards` WHERE `boardname`='$board', `id`='$id'");
echo "Message deleted.<br>\n";
}}

I think there may be a terminology issue.

 

A forum is made up of many parts. Boards which are for specific topics, Threads (or topics), which will have a subject post and replies, and posts which will have a single post by a user.

 

Are you trying to delete an entire board, a whole thread, or a single post within a thread?

 

 

 

 

On the errors, you won't get errors because you're not displaying them. your attempt to delete should read

$result = mysql_query("DELETE * FROM `boards` WHERE `boardname`='$board', `id`='$id'") or die(mysql_error());

 

Above post is correct, and you'd get an informative error about it if you check for errors :)

So basically:

 

$result = mysql_query("DELETE FROM `boards` WHERE `boardname`='$board' AND `id`='$id'") or die(mysql_error());

 

Although like jesi says you might be trying to delete the wrong part, don't you have a different table called post's / threads ?

 

PS. jesi you might be able to know my problem and seem very knowledgeable, mind having a read in the topic below about splitting up data?

I'm not deleting a board. Boards is the title of the table within the database. Inside boards are the fields: id, boardname, message.

 

I'm wanting to delete a specific message based on the id of the message.

 

I have multiple boards (thus the need for boardname) but each message has a unique id (id) to it.

 

I do notice that when I mouseover the link, it doesn't have the id of the message on the end of the url... So, I guess it's not pulling $id correctly then, right?

Well that should be fixed first then, try changing

 

$x = " --- <a href=main.php?username=$username&password=$password&action=delpost&id=$id>Delete Post</a>";}

 

to

 

$x = " --- <a href=main.php?username=$username&password=$password&id=$id&action=delpost>Delete Post</a>";}

 

That is the first thing I'd try, not sure if it will work though.

You need to have several tables. One for boards, one for threads, and one for posts. Your posts table will have foreign keys to thread_id and board_id. This is called normalizing data. I suggest googling that term and reading about how relational databases work, and changing your DB structure accordingly.

 

Your links should not have username and password in them, this is very unsecure. You need to manage login by session/cookies.

 

Lastly, to determine why $id isn't set we'd probably need to see more code. I suggest addressing the above issues first.

Okay, I guess the term "forum" is incorrect. There are no threads or overall board for everything in that sense.

 

I have a message system that allows people to post to "boards" inside of my game. Each room can have a board inside it, thus needing the boardname field within the boards table.

 

Each of these boards is its own separate entity that contain messages. So you can think of it as a message system tied to a specific room based upon its boardname (which is the room's title). Does that make more sense? It's just a bunch of single "topics" (if you want to go with forum terminology) that don't exist within any board or thread.

 

Here are all the functions that are in use for this system:

 

Post HTML:

if (getlevel(get("plane"),coords(),"board") == "") echo "There is no bulletin board here.<br>\n"; else{
$action = filter($action);
$data = explode("Post: ",getlevel(get("plane"),coords(),"board"));
$board = getlevel(get("plane"),coords(),"board");
$size = count($data);
$message = stripslashes(nl2br($action));
$thetime = gmdate("D, M jS, Y - g:i A") . " GMT";
$thisdata = explode(":::",$data[$data[0]]);
$thisdata[0] = get("name");
$thisdata[1] = $thetime;
$thisdata[2] = $message;
$thisdata = implode(":::",$thisdata);
$data[$data[0]] = $thisdata;
$data[0]++;
if ($data[0] > $size) $data[0] = 1;
$data = implode($data);
$dbh=dbconnect() or die ("Userlist read error: " . mysql_error()."<br>");
mysql_select_db("XXX");
$result = mysql_query("INSERT INTO `boards` SET `boardname`='$board', `message`='$message', `username`='$username', `time`='$thetime'") or die (mysql_error());
echo "Message posted.<br>\n";

}

 

 

Post function:

<?
$command = 1;
$dat = substr($action2,5);
if (getlevel(get("plane"),coords(),"board") == "") echo "There is no bulletin board here.<br>\n"; else{
echo "<center><form action=post.php method=post>\n<textarea name=action rows=10 cols=95%>$dat</textarea>\n<input type=hidden name=username value=$username>\n<input type=hidden name=password value=$password><br>\n<input type=submit value=Post>\n</form></center>\n";
}
?>

 

Readboard/Delete

<?
function delpost() {
if (getlevel(get("plane"),coords(),"board") == "" && ( get("admin") == "Y+" || get("admin") == "Y")) echo "There is no bulletin board here.<br>\n"; 

else{
$board = getlevel(get("plane"),coords(),"board");
$dbh=dbconnect() or die ("Userlist read error: " . mysql_error()."<br>");
mysql_select_db("XXX");
$id = $_GET['id'];
$result = mysql_query("DELETE FROM `boards` WHERE `boardname`='$board' AND `id`='$id'") or die(mysql_error());;
echo "Message deleted.<br>\n";
}}

$command = 1;
if (getlevel(get("plane"),coords(),"board") == "") echo "There is no bulletin board here.<br>\n"; else{
$board = getlevel(get("plane"),coords(),"board");
	if ($admin == "Y+" || $admin == "Y"){
	$x = " --- <a href=main.php?username=$username&password=$password&action=delpost&id=$id>Delete Post</a>";}
	$dbh=dbconnect() or die ("Userlist read error: " . mysql_error()."<br>");
	mysql_select_db("XXX");
	$result = mysql_query("SELECT * FROM boards WHERE boardname='$board' ORDER BY id LIMIT 10");
	$post = mysql_fetch_assoc($result);
	while ($row = mysql_fetch_array($result)){
  echo stripslashes($row['message']) . "$x<br>" . " --- ".stripslashes($row['username']). " on ".stripslashes($row['time']) ."<hr width=90%>";
}
}

?>

The same structure I described still applies. You are deleting the bottom most type of data in a structure of

Parent

Child Level 1

Child Level 2

 

You should normalize the data.

 

 

 

 

In the code you just posted, nowhere do you SET $id before attempting to use it. So that's what's causing your problem.

Okay, so when I call out the messages to be read with this query:

 

	$result = mysql_query("SELECT * FROM boards WHERE boardname='$board' ORDER BY id LIMIT 10");

 

I try to follow it up with:

 

	$id = $_GET['id'];

 

But that doesn't seem to work either. How can I get the $id variable to capture the id that is called during that result query?

first of all, I don't think you understand what $_GET is. Google.

Secondly, the SAME way you print the message. Where are you printing the message? I see it in your code, I want you to find it so you understand what you're doing, because you've gotten this far apparently without understanding some basic parts of PHP/MySQL.

I got a bit of what I'm doing...

 

I'm calling the URL to a main.php where this is not located.

 

How do I setup the Delete Post URL to run a function on the same page?

 

And I think I see what you're talking about.

 

I've added in the code:

 

$id = $row['id'];

 

Is that what you are trying to get me to see?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.