zelig Posted April 5, 2012 Share Posted April 5, 2012 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"; }} Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/ Share on other sites More sharing options...
downah Posted April 5, 2012 Share Posted April 5, 2012 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'"); Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334669 Share on other sites More sharing options...
zelig Posted April 5, 2012 Author Share Posted April 5, 2012 Yes, each post has a unique id, simply the id field (primary). I didn't create a separate postid field. Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334671 Share on other sites More sharing options...
downah Posted April 5, 2012 Share Posted April 5, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334673 Share on other sites More sharing options...
zelig Posted April 5, 2012 Author Share Posted April 5, 2012 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"; }} Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334675 Share on other sites More sharing options...
downah Posted April 5, 2012 Share Posted April 5, 2012 Hmm try $result = mysql_query("DELETE * FROM `boards` WHERE `boardname`='$board', `id`='$id'"); to $result = mysql_query("DELETE FROM `boards` WHERE `boardname`='$board', `id`='$id'"); I don't think you need the star for delete Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334678 Share on other sites More sharing options...
Jessica Posted April 5, 2012 Share Posted April 5, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334679 Share on other sites More sharing options...
zelig Posted April 5, 2012 Author Share Posted April 5, 2012 A single post within a thread. Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334680 Share on other sites More sharing options...
Jessica Posted April 5, 2012 Share Posted April 5, 2012 Then why are you deleting a board in your query? Also, where clauses are joined with AND, not by commas. Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334684 Share on other sites More sharing options...
downah Posted April 5, 2012 Share Posted April 5, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334686 Share on other sites More sharing options...
zelig Posted April 5, 2012 Author Share Posted April 5, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334687 Share on other sites More sharing options...
downah Posted April 5, 2012 Share Posted April 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334702 Share on other sites More sharing options...
zelig Posted April 5, 2012 Author Share Posted April 5, 2012 Didn't do anything. Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334705 Share on other sites More sharing options...
Jessica Posted April 5, 2012 Share Posted April 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334710 Share on other sites More sharing options...
zelig Posted April 5, 2012 Author Share Posted April 5, 2012 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%>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334714 Share on other sites More sharing options...
downah Posted April 5, 2012 Share Posted April 5, 2012 The message should have its own unique ID Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334719 Share on other sites More sharing options...
Jessica Posted April 5, 2012 Share Posted April 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334720 Share on other sites More sharing options...
zelig Posted April 5, 2012 Author Share Posted April 5, 2012 Each message has its own unique id. That's the auto-increment primary key of id on the boards table. How do I define what the $id variable is? I tried the $id=$_GET['id'] , but that didn't seem to pull it. Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334721 Share on other sites More sharing options...
Jessica Posted April 5, 2012 Share Posted April 5, 2012 When you display the message you have to select it out of the DB, that's where you get your id from. When you loop through to print them, you print the message, right? The ID is in that same set. Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334724 Share on other sites More sharing options...
zelig Posted April 5, 2012 Author Share Posted April 5, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334727 Share on other sites More sharing options...
Jessica Posted April 5, 2012 Share Posted April 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334732 Share on other sites More sharing options...
zelig Posted April 5, 2012 Author Share Posted April 5, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/260411-delete-an-entry-from-a-forum/#findComment-1334738 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.