mrherman Posted April 15, 2010 Share Posted April 15, 2010 I'm a PHP novice. When an html table page is opened on a new, a temporary table is created. When the user selects records from the table, they are inserted into the temporary table. But if the user decides to select more records on the current page, or go to the next page to select records, the temporary table is RE-CREATED and records are, of course, lost. What is the concept for making sure that as long as the user is logged in, the temporary table is created only once? Do I need to use cookies (which I haven't done before). Thanks! Link to comment https://forums.phpfreaks.com/topic/198676-temp-table-is-deleted-do-i-need-to-use-cookies/ Share on other sites More sharing options...
Ken2k7 Posted April 15, 2010 Share Posted April 15, 2010 Sessions would be better, but I don't like the thought of creating all those temporary tables. What exactly is it that you're trying to accomplish? Link to comment https://forums.phpfreaks.com/topic/198676-temp-table-is-deleted-do-i-need-to-use-cookies/#findComment-1042616 Share on other sites More sharing options...
mrherman Posted April 15, 2010 Author Share Posted April 15, 2010 Thanks for your reply! This is not a live site yet. Here is what I'm trying to do: 1. Several pages of records are shown to the user, 20 records to a page. 2. User selects records from page 1 (using checkboxes). The records are inserted into the temporary table which has been created when the user selects the first set of records. 3. The user selects records from other pages (as the user chooses). After each page of selections, the records are added to the same temp table. 4. When the user has completed selecting all relevant records, the records from the temp table are added to a "permanent" table, and the temp table is deleted. Interestingly, I tried to do something with SESSIONS. When the user gets to the first page of records, this is the code: session_start () ; $_SESSION [ 'tempdb' ] = "false" ; Then when the temp table is created, this is the code: if ( $_SESSION [ 'tempdb' ] = "false" ) { include_once "db_login.php" ; $sql = "DROP TEMPORARY TABLE IF EXISTS temp_sap_id_select" ; mysql_query ( $sql ) or ( "Error " . mysql_error () ) ; $sql = " CREATE TEMPORARY TABLE temp_sap_id_select ( `current_page` INT NOT NULL, `total_pages` INT NOT NULL, `select_date` DATE NOT NULL, `select_schcode` CHAR(6) NOT NULL, `select_user` CHAR(30) NOT NULL, `select_id` CHAR(9) NOT NULL ) " ; mysql_query ( $sql ) or ( "Error " . mysql_error () ) ; $_SESSION [ 'tempdb' ] = "true" ; } However, this doesn't work, because (I guess) the SESSION variable is reset to "false" every time the user updates the temp table with records. Link to comment https://forums.phpfreaks.com/topic/198676-temp-table-is-deleted-do-i-need-to-use-cookies/#findComment-1042646 Share on other sites More sharing options...
PFMaBiSmAd Posted April 15, 2010 Share Posted April 15, 2010 Temporary database tables are per-connection. When the code on any page ends and the database connection is closed, any temporary tables created using that connection are deleted. Link to comment https://forums.phpfreaks.com/topic/198676-temp-table-is-deleted-do-i-need-to-use-cookies/#findComment-1042651 Share on other sites More sharing options...
jcbones Posted April 15, 2010 Share Posted April 15, 2010 The biggest problem using Temp MySQL tables in a PHP script, is the tables are destroyed when the script completes, as the mysql connection automatically closes. Thus the MySQL session expires. Link to comment https://forums.phpfreaks.com/topic/198676-temp-table-is-deleted-do-i-need-to-use-cookies/#findComment-1042652 Share on other sites More sharing options...
mrherman Posted April 15, 2010 Author Share Posted April 15, 2010 The biggest problem using Temp MySQL tables in a PHP script, is the tables are destroyed when the script completes, as the mysql connection automatically closes. Thus the MySQL session expires. Thanks, PFMaBiSmAd and jcbones. This is very helpful. So, does the script complete when the PHP/HTML page has run its course, or does the script continue until the user logs out after further transactions? Oh, wait, wait...I get your point...Right, this is why a db login must be done each time, isn't it? OK, I think I get it. Obviously, this wasn't clear to me before. So temp tables are out, and a permanent table will have to be used. Thanks very much! Link to comment https://forums.phpfreaks.com/topic/198676-temp-table-is-deleted-do-i-need-to-use-cookies/#findComment-1042660 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.