Hi all,

Im relatively new to PHP, and i have come across a brick wall. What im trying to do is pass two variables $factid and $pathid to another PHP page and from there insert them into SQL Table. The only problem is that the $pathid is inside a while loop and $factid is echoed on the page from another query.

Both variables are integers so im puzzled why they are not echoing back.

Here is some of the code from a page called firstfact.php

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("194.81.104.27","www","www") or die(mysql_error());
	
//select which database you want to edit
mysql_select_db("dbaleister") or die(mysql_error()); 

$search=$_POST["search"];

$factid = $_REQUEST["factid"];

//get the mysql and store them in $result.
//change whatevertable to the mysql table you're using.
//change whatevercolumn to the column in the table you want to search.

$result = mysql_query("SELECT * FROM fact,url,keyword WHERE factid = urlid AND factid = keyid AND title LIKE '%$search%'");

//grab all the content.

while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
   $factid=$r["factid"];
   $title=$r["title"];
   $paragraph=$r["paragraph"];
   $image=$r["image"];
   $keyid=$r["keyid"];
   $kword=$r["kword"];
   $urlid=$r["urlid"];
   $link=$r["link"];
   
   //display the row
   //echo "$title <br /> $paragraph";
}
?>

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("194.81.104.27","www","www") or die(mysql_error());

//select which database you want to edit
mysql_select_db("dbaleister2") or die(mysql_error());

$pathid = $_REQUEST["pathid"];

$result = mysql_query ("SELECT pathid FROM lp ORDER BY pathid DESC LIMIT 0,1");

  echo "<center><table border='1' width='50%' cellpadding='20px'>
    <tr>
      <td align='center' colspan='2'><b>YOUR CURRENT LEARNING PATH IS:</b></td>
    </tr>";
    while ($row = mysql_fetch_array($result)) { 
      echo "<tr>";
      echo "<td>" . $row['pathid'] . "</td>";
      echo "</tr>";
    }
  echo "</table></center>";
?>

Here is the next page called addfirstfact.php, this is all of code.

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("194.81.104.27","www","www") or die(mysql_error()); 

//select which database you want to edit
mysql_select_db("dbaleister2") or die(mysql_error());


//GRAB THE VARIABLES

$pathid = $_REQUEST["pathid"];

$factid = $_REQUEST["factid"];


echo '$pathid';
echo '<br /><br />';
echo '$factid';
echo '<br /><br />';

//mysql_query("INSERT INTO step VALUES (null, '$factid', 0, 0, '$pathid', 0)");


header('Location: search2.php');

?>

If you need anymore information, let me know and i can send the whole of the page source code.

<?php session_start();
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("194.81.104.27","www","www") or die(mysql_error());
	
//select which database you want to edit
mysql_select_db("dbaleister") or die(mysql_error()); 

$search=$_POST["search"];

$factid = $_REQUEST["factid"];

//get the mysql and store them in $result.
//change whatevertable to the mysql table you're using.
//change whatevercolumn to the column in the table you want to search.

$result = mysql_query("SELECT * FROM fact,url,keyword WHERE factid = urlid AND factid = keyid AND title LIKE '%$search%'");

//grab all the content.

while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
   $factid=$r["factid"];
   $title=$r["title"];
   $paragraph=$r["paragraph"];
   $image=$r["image"];
   $keyid=$r["keyid"];
   $kword=$r["kword"];
   $urlid=$r["urlid"];
   $link=$r["link"];
   
   //display the row
   //echo "$title <br /> $paragraph";
}
?>

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("194.81.104.27","www","www") or die(mysql_error());

//select which database you want to edit
mysql_select_db("dbaleister2") or die(mysql_error());

$pathid = $_REQUEST["pathid"];

$result = mysql_query ("SELECT pathid FROM lp ORDER BY pathid DESC LIMIT 0,1");

  echo "<center><table border='1' width='50%' cellpadding='20px'>
    <tr>
      <td align='center' colspan='2'><b>YOUR CURRENT LEARNING PATH IS:</b></td>
    </tr>";
    $_SESSION['patharray']=array();
    while ($row = mysql_fetch_array($result)) { 
      $_SESSION['patharray'][]=$row['pathid'];
      echo "<tr>";
      echo "<td>" . $row['pathid'] . "</td>";
      echo "</tr>";
    }
  echo "</table></center>";
?>
<?php session_start();
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("194.81.104.27","www","www") or die(mysql_error()); 

//select which database you want to edit
mysql_select_db("dbaleister2") or die(mysql_error());


//GRAB THE VARIABLES

$pathid = $_REQUEST["pathid"];

$factid = $_REQUEST["factid"];


echo '$pathid';
echo '<br /><br />';
echo '$factid';
echo '<br /><br />';
foreach ($_SESSION['patharray'] AS $value) {
echo $value.'<br>'; //path id
}
//mysql_query("INSERT INTO step VALUES (null, '$factid', 0, 0, '$pathid', 0)");

//below line won't work due to above browser output
//header('Location: search2.php');

?>

I don't quite understand your while loop. Each time you loop, aren't you just replacing all the variables? As far as I can tell, the only variables that you have after the loop is finished is the result of the final loop.

I don't quite understand your while loop. Each time you loop, aren't you just replacing all the variables? As far as I can tell, the only variables that you have after the loop is finished is the result of the final loop.

The trick is in the SQL request:

$result = mysql_query ("SELECT pathid FROM lp ORDER BY pathid DESC [b]LIMIT 0,1[/b]");

The limit causes the statement to only return one row of data, regardless of how many potential matches it could have found. Which, in hindsight, begs the question of why there's a loop there in the first place. :)

I don't quite understand your while loop. Each time you loop, aren't you just replacing all the variables? As far as I can tell, the only variables that you have after the loop is finished is the result of the final loop.

Neither do i, can i just echo the result normally using an echo command?

The trick is in the SQL request:

$result = mysql_query ("SELECT pathid FROM lp ORDER BY pathid DESC [b]LIMIT 0,1[/b]");

The limit causes the statement to only return one row of data, regardless of how many potential matches it could have found. Which, in hindsight, begs the question of why there's a loop there in the first place. :)

I'm not sure why im using the while loop, can i just use a normal echo in a table to bring the value back?

Hi all, sorry if im confusing you, what im trying to do is this. I have created a fact repository frame which holds facts for the use to look for. The user looks for facts through a search text field in which they submit.

The search variable brings up on the page anything which matches the search to a title or description in my database.

If the search matches, it echos all the data from that factid on the screen. What i want to do is grab the $factid and pass it to the next page.

The $pathid is a problem too. The while loop in the code brings back results from query. This query is supposed to bring up the last record or pathid from a table in a database.

I want to pass both the $factid and $pathid variable to another page so i could insert both values in another table.

Does that make more sense? Sorry im not good at wording.


This is my first page, in which the user submits a search.

<form method="post" action="firstfact.php">

			<input type="text" name="search">

			<input type="Submit" name="Submit" value="Search">

			</form>

This is the page in which the user's search is submitted to and brings up all data related to that search.

<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
<html>

<head><title>Login Panel</title></head>

	<!-- stylesheets -->
  	<link rel="stylesheet" href="style.css" type="text/css" media="screen" />

<body>
<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("194.81.104.27","www","www") or die(mysql_error());
	
//select which database you want to edit
mysql_select_db("dbaleister") or die(mysql_error()); 

$search=$_POST["search"];

$factid = $_REQUEST["factid"];

//get the mysql and store them in $result.
//change whatevertable to the mysql table you're using.
//change whatevercolumn to the column in the table you want to search.

$result = mysql_query("SELECT * FROM fact,url,keyword WHERE factid = urlid AND factid = keyid AND title LIKE '%$search%'");

//grab all the content.

while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
   $factid=$r["factid"];
   $title=$r["title"];
   $paragraph=$r["paragraph"];
   $image=$r["image"];
   $keyid=$r["keyid"];
   $kword=$r["kword"];
   $urlid=$r["urlid"];
   $link=$r["link"];
   
   //display the row
   //echo "$title <br /> $paragraph";
}
?>
<!-- Main page starts here -->	

    <div id="container">

		<div id="content" style="padding-top:50px;">

			
			<table id="main">

								
				<tr>

				<td>

					<table id="title">

								
					<tr>

					<td>

					<p><u>Title</u><h4><? echo "$title";?></h4></p>

					</td>

					</tr>

					</table><br />

				</td>

				<tr>

				<td>

					<table id="image">

								
					<tr>

					<td>

					<p><img src="<? echo "$image";?>"></p>

					</td>

					</tr>

					</table><br />

				</td>

				<td>


					<table id="paragraph">

								
					<tr>

					<td>

					<p><? echo "$paragraph";?></p>

					</td>

					</tr>

					</table><br />

				</td>


				</tr>

				<tr>

					<td>

					<table id="url">

								
					<tr>

					<td>

					<p><u>URL Address</u><br /><br /><a href="<? echo "$link";?>"><? echo "$link";?></a></p>

					</td>

					</tr>

					</table>

				</td>

				<td>


					<table id="keyword">

								
					<tr>

					<td>

					<p><u>Associated Keywords:</u><br /><br /><? echo "$kword";?></p>

					</td>

					</tr>

					</table>

				</td>


				</tr>

			</table>

<br />


				
<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("194.81.104.27","www","www") or die(mysql_error());

//select which database you want to edit
mysql_select_db("dbaleister") or die(mysql_error());

$result = mysql_query("SELECT factid,title FROM fact WHERE factid IN (SELECT fk2_factid FROM relationship WHERE fk1_factid = ".$factid.")");

$factid=$r['factid'];

  echo "<table border='1' width='100%' cellpadding='20px'>
    <tr>
      <td align='center' colspan='2'><b>LIST OF RELATED FACTS</b></td>
    </tr>";
    while ($row = mysql_fetch_array($result)) { 
      echo "<tr>";
      echo "<td>" . $row['factid'] . "</td>";
      echo "<td>" . $row['title'] . "</td>";
      echo "</tr>";
    }
  echo "</table>";
?>
<br />
<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("194.81.104.27","www","www") or die(mysql_error());

//select which database you want to edit
mysql_select_db("dbaleister2") or die(mysql_error());

$pathid = $_REQUEST["pathid"];

$result = mysql_query ("SELECT pathid FROM lp ORDER BY pathid DESC LIMIT 0,1");

  echo "<center><table border='1' width='50%' cellpadding='20px'>
    <tr>
      <td align='center' colspan='2'><b>YOUR CURRENT LEARNING PATH IS:</b></td>
    </tr>";
    while ($row = mysql_fetch_array($result)) { 
      echo "<tr>";
      echo "<td>" . $row['pathid'] . "</td>";
      echo "</tr>";
    }
  echo "</table></center>";
?>
<br />
<center><table border="1" width="20%" cellpadding="10px">

    	<tr>

      		<td align="center" colspan="2"><b>ADD THIS STEP TO LEARNING PATH</b></td>

    	</tr>

	<tr>
		<td><center><form action="addfirstfact.php" method="post"><input type="submit" value="Add"></form></center></td>

	</tr>

</table></center>

<br />

			<table id="footer">

				<tr>

				<td>

					<p>Hello <?php echo $_SESSION['myusername'];?>, Would you like to <a href="logout.php">Logout?</a></p>



				</td>


				</tr>

			</table>

<br />					<form method="link" action="search.php">

					<input type="submit" value="Back to Search">

					</form>


</div><!-- content -->		
	</div><!-- container -->
</body>

</html>

I want the two variable $factid and $pathid to be passed to this page so i could insert them in a table.

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("194.81.104.27","www","www") or die(mysql_error()); 

//select which database you want to edit
mysql_select_db("dbaleister2") or die(mysql_error());


//GRAB THE VARIABLES

$pathid = $_REQUEST["pathid"];

$factid = $_REQUEST["factid"];


mysql_query("INSERT INTO step VALUES (null, '$factid', 0, 0, '$pathid', 0)");


header('Location: search2.php');

?>

Why You Created So Much Topics for one Question ?This is not a way to ask a question.:angry:

I don't quite understand your while loop. Each time you loop, aren't you just replacing all the variables? As far as I can tell, the only variables that you have after the loop is finished is the result of the final loop.

It does not replace the variables value but instead append to the value in the variable array. So any subvalue can then be retrieved with a foreach loop or using parameters like $_SESSION[0]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.