Jump to content

Basic Help on PHP


maarthala

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.