maarthala Posted December 19, 2007 Share Posted December 19, 2007 Hi Friends, I have written the below code which call the same page. When i click on the user information provided on the page, my if ($id) is not working, but when i keep $id=1 , then it is working. Please let me know where I am doing wrong. Regards, Surendra <html> <body> <?php $db=mysql_connect("localhost","user1","newluck"); mysql_select_db("userinfo",$db); //$id=1; if ($id) { $result = mysql_query("SELECT * FROM employees WHERE id=$id",$db); $myrow = mysql_fetch_array($result); printf("First name: %s\n<br>", $myrow["first"]); printf("Last name: %s\n<br>", $myrow["last"]); printf("Address: %s\n<br>", $myrow["address"]); printf("Position: %s\n<br>", $myrow["position"]); } else { // show employee list $result = mysql_query("SELECT * FROM employees",$db); if ($myrow = mysql_fetch_array($result)) { // display list if there are records to display do { printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]); } while ($myrow = mysql_fetch_array($result)); } else { // no records to display echo "Sorry, no records were found!"; } } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 19, 2007 Share Posted December 19, 2007 if you dont set $id=1 where is $id defined? Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 19, 2007 Share Posted December 19, 2007 trie this.... <html> <body> <?php session_start(); $db=mysql_connect("localhost","user1","newluck"); mysql_select_db("userinfo",$db); $result = mysql_query("SELECT * FROM employees WHERE id=$id",$db); $myrow = mysql_fetch_array($result); $_SESSION['id']=$myrow; printf("First name: %s\n ", $myrow["first"]); printf("Last name: %s\n ", $myrow["last"]); printf("Address: %s\n ", $myrow["address"]); printf("Position: %s\n ", $myrow["position"]); } else { // show employee list $result = mysql_query("SELECT * FROM employees where id=" '".$_SESSION['id']."' ",$db); if ($myrow = mysql_fetch_array($result)) { // display list if there are records to display do { printf("<a href=\"%s?id=%s\">%s %s[/url] \n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]); } while ($myrow = mysql_fetch_array($result)); } else { // no records to display echo "Sorry, no records were found!"; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
maarthala Posted December 19, 2007 Author Share Posted December 19, 2007 HI Adam, Id will be selected when the user click on href printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"],$myrow["first"], $myrow["last"]); once i click on it , the url willl be appeared like this http://localhost/page4.php?id=1 so my if condition is satisfied and the id is passed to qry. Hope this is clear. Hi Redarrow, Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\MyProj\page7.php:3) in C:\MyProj\page7.php on line 3 the above error is displayed for the code you pasted. any help on it.?? Regards Quote Link to comment Share on other sites More sharing options...
revraz Posted December 19, 2007 Share Posted December 19, 2007 The <?php session_start(); needs to be before the HTML lines. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.