0

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....

user3185347
  • 33
  • 1
  • 2
  • 6

6 Answers6

5

HTML will not understand that you're using a PHP variable,

<a href='query_1.php?id="<?php echo $q; ?>"'>aa</a>

Warning: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
1

You can not use php variable inside html without php tag. replace this:

<a href='query_1.php?id=" . $q . "'>aa</a>

by this:

<a href="query_1.php?id=<?php echo $q; ?>">aa</a>
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53
1

Problem is passing $q in your <a> tag. Do this:

<a href='query_1.php?id=<?php echo $q;?>'>aa</a>
user2936213
  • 1,021
  • 1
  • 8
  • 19
1

You simply have to echo the variable:

<a href='query_1.php?id="<?php echo $q; ?>"'>aa</a>

and your done :)

Better use the object oriented form of mysqli in the future.

Xatenev
  • 6,383
  • 3
  • 18
  • 42
0

Just a guess, but mysql_fetch_array() retrieves a whole array. You only want a single result, which should be retrieved by using mysql_fetch_row().

0

Try to use mysqli because mysql is deprecated

user3025122
  • 17
  • 2
  • 9