Reputation: 87
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
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
Reputation: 3354
You have several options to keep data between requests
session
as Orangepill suggested. For PHP, you can store session in $_SESSION
if you are using raw PHPyourdomain.com?previous=somepreviousdata
setcookie("previous", $data);
Upvotes: 0