Hello experts,
I am using a query string code. I want to “get” variable of one page to another page.
Query1.php:
<?php
$konek = mysql_connect("localhost","root","") or die("Cannot connect to server");
mysql_select_db("test",$konek) or die("Cannot connect to the database");
$query = mysql_query("select * from persons where id='1'");
$row = mysql_fetch_array($query);
$q= $row['details'];
?>
<a href='query_1.php?id=" . $q . "'>aa</a>
query_1.php
<?php
//$ab=1;
//if(isset($_GET['id']))
$ab= $_GET['id'];
echo $ab;
?>
The above code does not run and give error:
Notice: Undefined index: id
I am not geting what i am missing.
So, can some help me out wid my mistake…
What’s the value of the ‘id’ variable in the URL if you ‘view source’ on the query1.php page, before you click the link to query_1.php?
Also you should not be using the mysql_* functions as they are deprecated.
More info http://www.php.net/manual/en/intro.mysql.php
In your example, it looks like you are trying to use the $q variable without “going back into” PHP.
Well…, the code works perfectly fine in Query1.php page. But if i would nt have pasted my link
i.e echo “<a href='edit_last_follow.php?id=” . $a . “'>aa</a>”;
inspite only :
echo “<a href='edit_last_follow.php?id=” . $a . “'></a>”;
so how will i get the $a on my next page which is query_1.php page:
<?php
if(isset($_GET['id']))
$ab= $_GET['id'];
echo $ab;
?>
I want to echo $a of Query1.php page on query_1.php page.
I tried with the code above. I am stuck what i am missing.
PHP will only process PHP inside of <?php … ?> (technically not entirely correct, but close enough for this discussion)
So once you have ?> the rest is interpreted as HTML untl it next sees <?php
i.e. you need to enlose $q inside of <?php … ?>
Or
If your latter examples are closer to what you’re doing, you need to be careful using quotes so the output isn’t broken, eg. if the variale is 14
<a href='edit_last_follow.php?id=14'>aa</a>
<a href='edit_last_follow.php?id=14'></a>
then as long as edit_last_follow.php is the file with the echo $_get you should see it OK
Mittineague:
PHP will only process PHP inside of <?php … ?> (technically not entirely correct, but close enough for this discussion)
So once you have ?> the rest is interpreted as HTML untl it next sees <?php
i.e. you need to enlose $q inside of <?php … ?>
Or
If your latter examples are closer to what you’re doing, you need to be careful using quotes so the output isn’t broken, eg. if the variale is 14
<a href='edit_last_follow.php?id=14'>aa</a>
<a href='edit_last_follow.php?id=14'></a>
then as long as edit_last_follow.php is the file with the echo $_get you should see it OK
I have enclosed it in my Query1.php page:
<?php
$konek = mysql_connect("localhost","root","") or die("Cannot connect to server");
mysql_select_db("test",$konek) or die("Cannot connect to the database");
$query = mysql_query("select * from persons where id='1'");
$row = mysql_fetch_array($query);
$q= $row['details'];
echo "<a href='query_1.php?id=" . $q . "'>aa</a>";
?>
But i dont want to give any link just get the variable $q in next page query_1.php
If you try
<?php
$konek = mysql_connect("localhost","root","") or die("Cannot connect to server");
mysql_select_db("test",$konek) or die("Cannot connect to the database");
$query = mysql_query("select * from persons where id='1'");
$row = mysql_fetch_array($query);
$q= $row['details'];
var_dump($row);
echo "<br />";
var_dump($q);
//echo "<a href='query_1.php?id=" . $q . "'>aa</a>";
?>
what do you see?
I get all my row value as an output.
See, what i want is get th variable value on next page
So you don’t want a link that you have to press, you just want to go directly to the query_1.php page without clicking anything?
If so, you could use:
header("Location: query_1.php?id=" . $q );
instead of echoing the link, I think. There’s probably a better way. When you displayed the link, did $q have the correct value in it? Remember we don’t know your database layout so can’t say whether $q= $row[‘details’] is going to need escaping for example.