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
https://forums.phpfreaks.com/topic/82316-basic-help-on-php/
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
https://forums.phpfreaks.com/topic/82316-basic-help-on-php/#findComment-418380
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
https://forums.phpfreaks.com/topic/82316-basic-help-on-php/#findComment-418420
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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