Page 1 of 1

Passing results of SQL query to another page

Posted: Thu Feb 18, 2010 5:11 am
by sunegtheoverlord
Hello all,

This is my first post and my first outing into the the world of web design and php so forgive me if i'm asking an obvious question.

i have 2 php pages at the moment.

a.php, calls a SQL query to retreive 4 fields (FirstName, Surname, PostCode and a Reference number) from a table based on an input from a form (Postcode). these fields are then displayed as a drop down menu (apart from the reference which i want to remain invisible to the user) from which you can select the one you want.

b.php, echos the 3 visible fields using echo $_GET["form_name"];

this works fine but i want to be able to pass the Reference number for the record that has been selected from the drop down list seperatly to b.php so i can use that as an input into another table of the sql database.

i'm sure this is possible but i just can't see how to do it.

help

S

Re: Passing results of SQL query to another page

Posted: Thu Feb 18, 2010 5:28 am
by realnsleo
Hi!!
Welcome to the world!!!

Now in a.php store the reference number as a session variable like this:

Code: Select all

<?php
 
[color=#0000FF]session_start()[/color]; // Make sure is called before everything else
 // Blah blah blah
[color=#0000FF]$_SESSION[/color][[color=#FF0000]'reference_number'[/color]] = $reference_number; // Store your reference number like this
 
?>
Then in b.php access your reference number like this:

Code: Select all

<?php
 
[color=#0000FF]session_start()[/color]; //You need to call this in all your pages to access variables
 
$reference_number = [color=#0000FF]$_SESSION[/color][[color=#FF0000]'reference_number'[/color]]; // Get back your reference number like this
 
?>
i hope this helps you.

Re: Passing results of SQL query to another page

Posted: Thu Feb 18, 2010 5:57 am
by sunegtheoverlord
Hi realnsleo,

Thanks for the reply. apologies for my lack of knowledge. where can i store the reference number that is selected from the drop down list?

i've included my code below:-


<html>

<body>



<form action="/a.php" method="GET">
Postcode: <input type="text" name="postcode" />
<input type="submit"/>
</form>

<?php

$myServer = "host";
$myUser = "uname";
$myPass = "pass";
$myDB = "enquiries";

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");

//execute the SQL query and return records
$client_result = mssql_query("EXEC enquiries.dbo.clients_pr_Lookup @postcode = '" . $_GET["postcode"] . "'");

$numRows = mssql_num_rows($client_result);
echo "<h1>Please select from " . $numRows . " Row" . ($numRows == 1 ? "" : "s") . "</h1>";



//display the results
echo "<form action=\"b.php\" method=\"GET\">";
echo "<select name=\"TheClient\">";
while($row = mssql_fetch_array($client_result))
{
echo "<option>" . $row["Surname"] . ", " . $row["Forenames"] . ($row["PartnerForenames"] == NULL ? "" : " and " . $row["PartnerSurname"] . ", " . $row["PartnerForenames"]) . " - " . $row["HomePostCode"] . "</option>";
}

echo "</select>\n <input type=\"submit\" value=\"Submit\" /></select></form>";

//close the connection
mssql_close($dbhandle);
?>

</body>
</html>