Jump to content

Archived

This topic is now archived and is closed to further replies.

cle13

Passing variables from php script to another

Recommended Posts

Hi, I have a php script which searches for school name results in a database and displays them back as links to another php script which displays detailed information about the a particular school from the same table and database. Now the second php script is supposed to use a variable from the first php script. How can I send that variable. Please help.

Below are the two scripts in question.

search script.

[color=brown][code]<html>
<head>
<title>School Search Results</title>
</head>
<body>
<h3>School Search Results</h3>
<?php
//create short variable names
$search1=$HTTP_POST_VARS['name'];
$search2=$HTTP_POST_VARS['type'];
$search3=$HTTP_POST_VARS['year'];
$search3=trim($search3);
if(!$search1)
    {
      echo 'You have not entered search details please go back and try again';
      exit;
      }
$search1=addslashes($search1);
$search2=addslashes($search2);
$search3=addslashes($search3);

$conn = mysql_connect('localhost','','pass');
if (!$conn)
      {
      echo 'Error: could not connect to database. Please try again later.';
      exit;
      }
mysql_select_db('school');
$query = "select * from SCHOOL where sch_name like '%".$search1."%' or type like '%".$search2."%' or year like '%".$search3."%'";
$result = mysql_query($query) or die (mysql_error()."<br>Couldn't execute query: $query");
$num_results = mysql_num_rows($result);

echo '<p>Search results found: '.$num_results.'</p>';

for ($i=0; $i < $num_results; $i++)
    {
    $row = mysql_fetch_array($result);
    echo '<p>&nbsp;'.($i+1).'<a href=schselect.php>&nbsp;';
    echo htmlspecialchars(stripslashes($row['sch_name']));
    echo '</a><br>&nbsp;&nbsp;&nbsp;';
    echo stripslashes($row['type']);
    echo '<br>&nbsp;&nbsp;&nbsp;';
    echo stripslashes($row['year']);
    echo '</p>';
    }
?>

</body>
</html>[/code][/color]

view details script

[color=brown][code]<?
$sch_name=$_GET[sch_name];
mysql_connect("localhost", "","pass") or
    die ("Could not connect to database");
mysql_select_db("school") or
    die ("Could not select database"); 
// define the SQL command
$query = "select * from school where sch_name = '$sch_name'";
// submit the query to the database
$res=mysql_query($query);
// make sure it worked!
if (!$res) {
echo mysql_error();
exit;
}
echo "<div align=\"center\">";
echo "<center>";
echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"8\" width=\"98%\">";
echo "<tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $res )) {
  // Print out the contents of each row into a table
  echo "<tr>";
  echo "<td width=\"100%\" height=\"58\" colspan=\"2\"><strong><font size=\"7\">";
  echo $row['sch_name'];
  echo "</font></strong>";
  echo "</td>";
  echo "</tr>";
  echo "<tr>";
  echo " <td width=\"20%\"><font size=\"2\">YEAR OF ESTABLISHMENT</font>";
  echo "<font size=\"3\"><br>";
  echo $row['year'];
  echo "</font>";
  echo "<font size=\"2\">";
  echo "<br>";
  echo "<p><font size=\"2\">LOCATION</font><font size=\"4\"><br>";
  echo "</font><font size=\"2\">";
  echo $row['location'];
  echo "<br>";
  echo "<p><font size=\"2\">KIND</font><br>";
  echo "<font size=\"2\">";
  echo $row['kind'];
  echo "<br><br>";
  echo "<p><font size=\"3\">TYPE</font></p>";
    echo "<p>";
  echo $row['type'];
  echo "</p>";
  echo "<p>TEL:</p><p><font size=\"3\">";
  echo $row['phone'];
  echo "</font><br>";
  echo "<p>POSTAL ADDRESS:</p><p><font size=\"3\">";
  echo $row['address'];
  echo "</font><br>";
  echo "</td>";
  echo "<td width=\"80%\"><p><strong><font size=\"4\">Headteacher's Message</font></strong></p><p>";
  echo $row['ht_msg'];
  echo "</p></font></td></tr>";
 
}
echo "</table>";
echo "</center></div>";
echo "</table>"

?>[/code][/color]

Thanx in advance.

Share this post


Link to post
Share on other sites
POST or GET or Sessions or Cookies

I would use get
<a href='page.php?school=".$school."'>View Info</a>

Share this post


Link to post
Share on other sites
<a href='page.php?school=".$school."'>View Info[/url]


Thanx but I dont know what to put where.

for example, in <a href='page.php?school=".$school."'>View Info[/url]

I put <a href='viewscript.php?

folder="
.$variable_to_be_sent."'>View Info[/url]

Is that how I do it? or I got it wrong somewhere. Coz that returns an error.

Thanx.

Share this post


Link to post
Share on other sites
exactly

but remember that the information is in the address bar, so this is not a good option for sensative information

Share this post


Link to post
Share on other sites
Hey man, I've done exactly that but the details script doesnt display anything! Try to look at the code for me and tell me where my mistakes are. Thanx.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.