-1

I am new to php code, I would like to know how to pass the multiple variable data from one php file to another php file. below the detailed description.

In first file (file1) I have assigned some query value which i want to read in second file( File 2). please help. below the code

File 1:

$query = "SELECT * FROM table1 where field1 = 'abc' and feild2='xyz'"; 
$query1 = "SELECT * FROM table2 where field1= 'abc' and feild2='xyz'"; 
$query2 = "SELECT * FROM table3 where field1= 'abc' and feild2='xyz'";

<a target="_blank" href='file2.php' >Firstquery</a>
<a target="_blank" href='file2.php' >seconquery</a>
<a target="_blank" href='file2.php' >thirdquery</a>

File2:

echo $query;
echo $query1;
echo $query2;
MH2K9
  • 11,951
  • 7
  • 32
  • 49
  • possible duplicate of [PHP Pass variable to next page](http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page) – CS GO Oct 29 '14 at 16:37
  • You can pass that info through a hidden input type. –  Oct 29 '14 at 16:38
  • 4
    @Wiraj Passing sql through a form field is a very bad idea, anybody can modify it. – jeroen Oct 29 '14 at 16:48

3 Answers3

1

You just need to use get parameters in your links if I am understanding your question correctly. You do not however, want to pass the entire query or save that query to session. Instead you just want to pass the values you need for the query. To keep the queries themselves maintained in one place I would create some sort of model class or a set of functions that could be included in the files that need them.

<?php
// initial page
$field1 = 'abc';
$field2 = 'xyz';

$query = "SELECT * FROM table1 where field1 = ? and feild2=?"; 
$query1 = "SELECT * FROM table2 where field1= ? and feild2= ?"; 
$query2 = "SELECT * FROM table3 where field1= ? and feild2= ?";
?>

<a target="_blank" href="<?php printf('file1.php?field1=%s&field2=%s', $field1, $field2) ?>" >Firstquery</a>
<a target="_blank" href="<?php printf('file2.php?field1=%s&field2=%s', $field1, $field2) ?>">seconquery</a>
<a target="_blank" href="<?php printf('file3.php?field1=%s&field2=%s', $field1, $field2) ?>">thirdquery</a>

Then in File*.php

<?php
// File1.php
$field1 = isset($_GET['field1']) ? $_GET['field1'] : null;
$field2 = isset($_GET['field2']) ? $_GET['field2'] : null;

$query = "SELECT * FROM table1 where field1 = ? and feild2=?"; 
$query1 = "SELECT * FROM table2 where field1= ? and feild2= ?"; 
$query2 = "SELECT * FROM table3 where field1= ? and feild2= ?";

$db = new PDO($dsn, $user, $pass);
if ($field1 && $field2) {
  // do your querying
  // if you are not using PDO or mysqli which supports prepared statements then you
  // need to manually quote these variables since the come from the frontend
  // you also probably want to sanitize of validate them in some way as well.
  $stmt = $db->prepare($query);
  $stmt->execute(array($field1, $field2);
  $results1 = $pdo->fetchAll(PDO::FETCH_ASSOC);

  // rinse and repeat for the other 2 queries
} ?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
0

PHP is a Request/Response based language. That means that each time you load a page, the server believes that it's serving someone new. So when you go from File1 to File2, the web server does not know that you've even been to File1 in the first place.

Well, not unless you do something about it.

There are a few ways you could overcome this:

  1. Using session variables

Sessions are a simple way to store data for individual users against a unique session ID. This can be used to persist state information between page requests. Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data. The absence of an ID or session cookie lets PHP know to create a new session, and generate a new session ID.

You your code could work like the one below. (Note that this code is there to help explain the concept and should not be used in any real world application).

File1:

session_start();

$query = "SELECT * FROM table1 where field1 = 'abc' and feild2='xyz'"; 
$query1 = "SELECT * FROM table2 where field1= 'abc' and feild2='xyz'"; 
$query2 = "SELECT * FROM table3 where field1= 'abc' and feild2='xyz'";

$_SESSION['query'] = $query;
$_SESSION['query1'] = $query1;
$_SESSION['query2'] = $query2;

<a target="_blank" href='file2.php' >Firstquery</a>
<a target="_blank" href='file2.php' >seconquery</a>
<a target="_blank" href='file2.php' >thirdquery</a>

File2:

session_start();
echo $_SESSION['query'];
echo $_SESSION['query1'];
echo $_SESSION['query2'];
  1. Post values via a hidden field
dnshio
  • 914
  • 1
  • 8
  • 21
0

if u want to variable with url than

try this

yourfilename.php?var1=value&val2=value

First parameter is append with file name using ? and after that & is used to append other parameter

jay.jivani
  • 1,560
  • 1
  • 16
  • 33