Reputation: 650
I'm beginner in PHP MySQL,
I would like to ask how can I pass the value of variable to another page
for example:
I have input form where all dropdown this will be use for search condition.
It is possible to call the variable with value of SQL query like this:
In this sample, I set the $sql
to Page1.php
and I want to call it in Page2.php
$sql = "SELECT * table
WHERE column1 = '".$variable1."'
AND column2 = '".$variable2."'
AND column3 = '".$variable3."' ";
OR
If this kind of condition can be can be call or transfer or pass to another page?
if ($result=mysql_query($sql)) {
$query_num_rows = mysql_num_rows($result);
if($query_num_rows == 0){
echo "<script> alert('No Records Found!')</script>";
} else {
while($row=mysql_fetch_array($result)){
I'm doing this to because I want to have separate code for generating to csv due to if I use this in the same page due to ff:
For more info here is my all codes:
report.php
<?php
session_start();
include_once "dbconnect.php";
date_default_timezone_set('Hongkong');
//Validate Login
$res=mysql_query("SELECT * FROM accounts WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}//end of Validation Login
//Setting up Variables for Search form
if (isset($_POST['generate'])) {
$month = htmlentities(mysql_real_escape_string($_POST['month']));
$year = htmlentities(mysql_real_escape_string($_POST['year']));
$status = htmlentities(mysql_real_escape_string($_POST['status']));
if (empty($month) && empty($year) && empty($status)) {
echo '<script>alert(\'Select atleast 1 filter\')</script>';
exit;
}
//condition of search
//Single Search
if (!empty($month)) {
$sql = "SELECT `subscribers`.`cus_id`, `subscribers`.`fName`, `subscribers`.`lName`,`transactions`.`month`,
`transactions`.`year`, `transactions`.`subscriptionStart`,`transactions`.`subscriptionEnd`,`transactions`.`subsStatus`
FROM `subscribers`
INNER JOIN `transactions`
ON `subscribers`.`cus_id` = `transactions`.`cus_id`
WHERE
`transactions`.`month` = '".$month."'
ORDER BY `cus_id` ASC ";
} if (!empty($year)) {
$sql = "SELECT `subscribers`.`cus_id`, `subscribers`.`fName`, `subscribers`.`lName`,`transactions`.`month`,
`transactions`.`year`, `transactions`.`subscriptionStart`,`transactions`.`subscriptionEnd`,`transactions`.`subsStatus`
FROM `subscribers`
INNER JOIN `transactions`
ON `subscribers`.`cus_id` = `transactions`.`cus_id`
WHERE
`transactions`.`year` = '".$year."'
ORDER BY `cus_id` ASC ";
} if (!empty($status)) {
$sql = "SELECT `subscribers`.`cus_id`, `subscribers`.`fName`, `subscribers`.`lName`,`transactions`.`month`,
`transactions`.`year`, `transactions`.`subscriptionStart`,`transactions`.`subscriptionEnd`,`transactions`.`subsStatus`
FROM `subscribers`
INNER JOIN `transactions`
ON `subscribers`.`cus_id` = `transactions`.`cus_id`
WHERE
`transactions`.`status` = '".$status."'
ORDER BY `cus_id` ASC ";
//Compound Search
} if (!empty($month) && !empty($year)) {
$sql = "SELECT `subscribers`.`cus_id`, `subscribers`.`fName`, `subscribers`.`lName`,`transactions`.`month`,
`transactions`.`year`, `transactions`.`subscriptionStart`,`transactions`.`subscriptionEnd`,`transactions`.`subsStatus`
FROM `subscribers`
INNER JOIN `transactions`
ON `subscribers`.`cus_id` = `transactions`.`cus_id`
WHERE
`transactions`.`month` = '".$month."'
AND `transactions`.`year` = '".$year."'
ORDER BY `cus_id` ASC ";
} if (!empty($month) && !empty($status)) {
$sql = "SELECT `subscribers`.`cus_id`, `subscribers`.`fName`, `subscribers`.`lName`,`transactions`.`month`,
`transactions`.`year`, `transactions`.`subscriptionStart`,`transactions`.`subscriptionEnd`,`transactions`.`subsStatus`
FROM `subscribers`
INNER JOIN `transactions`
ON `subscribers`.`cus_id` = `transactions`.`cus_id`
WHERE
`transactions`.`month` = '".$month."'
AND `transactions`.`status` = '".$status."'
ORDER BY `cus_id` ASC ";
} if (!empty($month) && !empty($status) && !empty($year)) {
$sql = "SELECT `subscribers`.`cus_id`, `subscribers`.`fName`, `subscribers`.`lName`,`transactions`.`month`,
`transactions`.`year`, `transactions`.`subscriptionStart`,`transactions`.`subscriptionEnd`,`transactions`.`subsStatus`
FROM `subscribers`
INNER JOIN `transactions`
ON `subscribers`.`cus_id` = `transactions`.`cus_id`
WHERE
`transactions`.`month` = '".$month."'
AND `transactions`.`status` = '".$status."'
AND `transactions`.`year` = '".$year."'
ORDER BY `cus_id` ASC ";
}
//Is this possible to transfer to another page? I'll be using this for generate.csv
//FROM here
if ($result=mysql_query($sql)) {
$query_num_rows = mysql_num_rows($result);
if($query_num_rows == 0){
echo "<script> alert('No Records Found!')</script>";
} else {
while($row=mysql_fetch_array($result)){ // is this possible to move in another page?
echo "I removed the Table here because it's not important";
//Please any tips what I can add here?
}// end of while
}//end of else
// TO HERE
}//end of POST, setting up of variables
?>
<html>
<body>
<form action="generate-csv.php" method="POST">
<label>Month</label>
<select>
<option>JANUARY</option>
<option>fEBRUARY</option>
<option>MARCH</option>
<option>so on...</option>
</select>
<label>Year</label>
<select>
<option>2015</option>
<option>2014</option>
</select>
<label>Status</label>
<select>
<option>FRESH</option>
<option>RENEWAL</option>
<option>WINBACK</option>
</select>
<input type="submit" name="generate" value="GENERATE">
</form>
</body>
generate.php
<?php
include_once "dbconnect.php";
include_once "report.php";
//$sql will came from report.php
if($result = mysql_query($sql) ){
if (mysql_num_rows($result) > 0) {
$columns_total = mysql_num_fields($result);
// Get The Field Name
for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($result, $i);
$output .= '"'.$heading.'",';
}
$output .="\n";
} while($row = mysql_fetch_array($result)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}
$filename = "myFile.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;
exit;
}?>
Upvotes: 0
Views: 307
Reputation: 1115
In report.php store sql in $_SESSION
variable.
//Put your code here.
$_SESSION['sql_query'] = $sql;
//Is this possible to transfer to another page? I'll be using this for generate.csv
//FROM here
if ($result=mysql_query($sql)) {
$query_num_rows = mysql_num_rows($result);
if($query_num_rows == 0){
echo "<script> alert('No Records Found!')</script>";
} else {
while($row=mysql_fetch_array($result)){ // is this possible to move in another page?
echo "I removed the Table here because it's not important";
//Please any tips what I can add here?
}// end of while
}//end of else
and access it in generate.php
if($result = mysql_query($_SESSION['sql_query']) ) // first line of your code after include.
Upvotes: 1