Frigol33t
Frigol33t

Reputation: 87

Keep variable result during reload/redirect


I have a webpage where i upload a csv file that is then imported to a mysql database with php.
I have a mysql query that goes like "SELECT * FROM wifi" and then count the lines in the database. Then it should run the import of the csv and make the same query and count the lines again showing me a "before and after" number. This i got working, sort of.

The issue is, the first query counting the "before" number is in the top of the page and it shows the number nice, when i hit the submit-button it sends my csv into the php inserting it into the databse and redirecting to "pagehere?success=1" where it should show me the "after" number. But as the page reloads/redirects, the first query with the "before" number is re-run and shows the "after" number.

So i have one "before" number, imports csv and page reloads, then i have two "after" numbers.

How can i keep the "before" number during the redirect of the page?
Code goes like this (i´m still learning, might be something wrong)

(THIS CODE IS IN WEBPAGE)
//Count before import
$before = mysql_query("SELECT * FROM wifi");
$num_rows_before = mysql_num_rows($before);

(THIS CODE IS WHERE THE FORM UPLOAD FILE TO AFTER SUBMIT-BUTTON)
<?php 

if ($_FILES[csv][size] > 0) {

//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");

//loop through the csv file and insert into database
do {
    if ($data[0]) {
        mysql_query("INSERT IGNORE INTO wifi (bssid, channel, privacy, ciper, auth, power, essid, latitude, longitude, first_seen, last_seen) VALUES
            (
                '".addslashes($data[0])."',
                '".addslashes($data[1])."',
                '".addslashes($data[2])."',
                '".addslashes($data[3])."',
                '".addslashes($data[4])."',
                '".addslashes($data[5])."',
                '".addslashes($data[6])."',
                '".addslashes($data[7])."',
                '".addslashes($data[8])."',
                '".addslashes($data[9])."',
                '".addslashes($data[10])."' 
            )
        ");
    }
} while ($data = fgetcsv($handle,1000,","));

//redirect
header('Location: index.php/upload?success=1'); die;

}

//Count after import
$after = mysql_query("SELECT * FROM wifi");
$num_rows_after = mysql_num_rows($after);


//echo stats
echo "Number of rows before - ";
echo "$num_rows_before";
<br>
echo "Number of rows after - ";
echo "$num_rows_after";

//generic success notice
if (!empty($_GET[success])) { echo "<br><b>Result: Your file is imported!</b><br>"; } 

//Close connection to databse
mysql_close($connect) ; 

?>

Upvotes: 1

Views: 327

Answers (2)

nurdglaw
nurdglaw

Reputation: 2127

Pass the "before" number to the second webpage as an argument. So, change the last line of your code to

header('Location: index.php/upload?success=1&before=' . $num_rows_before); die;

Upvotes: 5

Tan Nguyen
Tan Nguyen

Reputation: 3354

You have several options to keep data between requests

  1. Use session as Orangepill suggested. For PHP, you can store session in $_SESSION if you are using raw PHP
  2. Use URL query params. After processing the data, you can set the params for the URL in order to save the last value. yourdomain.com?previous=somepreviousdata
  3. Use cookies. You can also store the previous data in cookie. If you are using raw PHP without any framework, you can do it by using setcookie("previous", $data);

Upvotes: 0

Related Questions