开发者

How to move pages in MySQL using PHP

开发者 https://www.devze.com 2023-01-26 06:07 出处:网络
I don\'t have the PHP code sorted yet but I\'m hoping someone will have a solution with any code as an example.

I don't have the PHP code sorted yet but I'm hoping someone will have a solution with any code as an example.

Example of MySQL Tables:

  • Pages
  • Trash

etc

I want to create a delete page which will once the delete link is clicked the page is moved from the pages table into the trash table.

So the file is not completely deleted from the website but moved to another table (trash) and 开发者_如何转开发is not visible on the website.

What code would be used to move the page for example about_us.php from the Pages table into the Trash Table?


If I understood correctly, your database will have at least two tables: "Pages" and "Trash". These tables will contain the name of certain files, that you want to move between "Pages" and "Trash".

The Table

If this is the case, I'd advice you to do everything in one table, by using a field that would be a flag for weather the page is "Trash" or not. Let's call this table "Pages", and it would be something like this:

----------------------------
| PageName | isTrash |
|---------------------------|
| about.php |     1        |
| home.php |     0        |

In this case, the field isTrash indicates weather a page is trash or not, by using the values 1 and 0, respectively.

The Code

I didn't quite understand if you were asking for PHP code or SQL, so here's both:

First, you need to setup some DB information and make the connection, like this:

<?
    //Information needed to make the connection
    $user="DB_username";
    $password="DB_password";
    $database="DB_name";
    //Same machine = localhost
    $host = "localhost";

    //Connect to the host
    mysql_connect(localhost,$user,$password);
    //Select the database
    @mysql_select_db($database) or die("Unable to select database");
?>

You can find more information on this subject here: http://www.w3schools.com/PHP/php_mysql_intro.asp

Now that we have our database ready to accept queries, lets make the page "home.php" a trash page:

<?
    //Information needed to make the connection
    $user="DB_username";
    $password="DB_password";
    $database="DB_name";
    //Same machine = localhost
    $host = "localhost";

    //Connect to the database
    mysql_connect(localhost,$user,$password);
    //Select the database
    @mysql_select_db($database) or die("Unable to select database");

    //MySQL query to make "home.php" a trash page
    $query = "UPDATE pages SET isTrash = 1 WHERE PageName = 'home.php'";
    //Execute the query
    mysql_query($query);
    //Close the connection
    mysql_close();
?>

Also, you can find more information about SQL here http://www.w3schools.com/sql/default.asp


I've done this a few times. It usually involves creating a generic trash table with the following fields:

|------------|----------|------------|------|
| objectType | objectId | title      | data |
|------------|----------|------------|------|
| page       | 1        | Homepage   | ...  |
|------------|----------|------------|------|

Because you don't really want to create a trash table for each table in your database.

Basically, the objectType field contains the table name of the object that was deleted, along with its id in objectId. title contains a user-friendly description of the object, because when showing the contents of the trash to the user, you can't expect him to understand what "page 1" means. "Page: Homepage" is more explanatory.

The last field, data contains a serialized copy of the row from the page table. To undelete, you unserialize the row and re-insert.

Deleting a page involves fetching the page record, serializing it, inserting it into the trash table and then deleting it from the original page table.

The other solution is to have a flag in your page table that tells whether or not the record is trashed. However, this other solution involves leaving a bunch of deleted records in the original table (and in the index), and can slow down queries. It mostly depends on your traffic, number of records and expected response times.


I'm not sure doing it in one table is a good idea actually it depends on the number of page... Cause it makes the db table much bigger with records that are not actually used... So everytime you do a select or anything you've got to consider them... In my opinion it will slow the DB (except if you don't have only a smal amount of record in it) what about doing an Insert Into (Select ...)) and then a delete... Having 2 query there is not dramatic as it's not something you're gonna do really often no?

I think that doing slow things while wrtiting in the DB is much better than something that will affect the reading... Always remember you do read more than you write.

0

精彩评论

暂无评论...
验证码 换一张
取 消