Resetting MYSQL Database Tables Upon User Login

Resetting MYSQL Database Tables Upon User Login

There could come a time when you want to allow people to login and test an application. Then, you may want the application to go back to an original state. One such method to accomplish this is to drop the database tables which change. Then, create and copy backup tables into the changeable tables. Now, each new user gets a fresh look at the website upon login.

The if statement below checks to see if the session exists. If it does, the tables are dropped and are recreated with new content.

if ($_SESSION['SESS_id_123']) {
$command = "DROP table tablename" ;
$result = mysqli_query($db, $command);
$command = "CREATE TABLE tablename SELECT * FROM tablename_backup ";
$result = mysqli_query($db, $command);

$command = "DROP table tablename_footer" ;
$result = mysqli_query($db, $command);
$command = "CREATE TABLE tablename_footer SELECT * FROM tablename_footer_backup ";
$result = mysqli_query($db, $command);

$command = "DROP table tablename_header" ;
$result = mysqli_query($db, $command);
$command = "CREATE TABLE tablename_header SELECT * FROM tablename_header_backup ";
$result = mysqli_query($db, $command);
}