Connecting Tech Pros Worldwide Help | Site Map

Deleting Post and More

By Blair Ireland
Senior Editor, TheScripts.com

Now, the last of the post part. Deleting. After this small bit, we will be making our index.php3 page, listing all topics, and how to create new ones.

function deletepost () {
global $postID;
$post_query = mysql_query("DELETE FROM posts WHERE (ID='$postID')");
 if (mysql_error() != "") {
    showheader("MySQL Error ".mysql_error());
    ?>
    <br><p><center><b><FONT SIZE="-1" FACE="Trebuchet MS,Arial,Helvetica">There was a MySQL Error -- <?php echo mysql_error() ?></b></center></p>
    <?php
    showfooter();
    exit;
  }
}

You can view the file here.

There is your function. I doubt it really needs explanation.... most of the work is actually done in the actual file actually.

And now we need our index.php3 page done.

To view the file, click here.

The coding, as you should notice, is quite similar to our view posts function already made. All we are really doing is just looking at another database. It's the same theory backing it though.

Our last thing to do is adding topics.... this will be an extension of the add-post.php3 file really, but all in its own unique file. We will also be utilizing our addpost() function as well, for less code writing, and better efficiency.

To view the file, click here.

function addtopic () {
global $topicname;

This shouldn't be new.....

$new_topicname = addslashes($topicname);
$topic_query = mysql_query("INSERT INTO topics VALUES ('NULL','$new_topicname')");

The reason we are adding the value 'NULL' is so that our table seeks the next available ID number, hence the 'auto_increment' part of our table.

  if (mysql_error() != "") {
    showheader("MySQL Error ".mysql_error());
    ?>
    <br><p><center><b><FONT SIZE="-1" FACE="Trebuchet MS,Arial,Helvetica">There was a MySQL Error -- <?php echo mysql_error() ?></b></center></p>
    <?php
    showfooter();
    exit;
  }
  return mysql_insert_id();

Ok, this is new. The function mysql_insert_id() returns the last made ID number in any auto_increment field. Since we just added a new ID number, it will be returned here. In add-topic.php3, we have two lines like

  $topicID = addtopic();
  addpost();

So, since we are 'returning' the ID of our topic number, it goes into $topicID, which is what we specified above. This $topicID is then used in our general addpost() function. Get it? I hope..... You can view the file here

}

This concludes our php forum tutorial. I know its not suffering from featuritis (lots of features), but it gets the job done, and well. You now should have learned about MySQL queries, functions, returning values, passing values to other files, while loops and the like. Now that I have given you the skeleton for the forum, you should be able to expand upon it with what you have learned, even adding another level for the forums, like categories or something.

All the files are available in a zip file here.

Build your own php/mysql apps now.....

« Add or Edit Post