Jump to content

Unable to show Session name


cool_techie

Recommended Posts

where are you getting $_REQUEST[uname] from? is it a form on a different page? also this line is wrong:

 

$select=mysql_query("SELECT * FROM user_id WHERE uname='$_REQUEST[uname]'",$connection);

 

it should be:

 

$select=mysql_query("SELECT * FROM user_id WHERE uname='".$_REQUEST['uname']."'");

 

is user_id the name of the database table?

Link to comment
Share on other sites

for a better chance of seeing the error do this. replace the top php code with:

 

$connection=mysql_connect("localhost","root","");
mysql_select_db("forum",$connection);
$select=mysql_query("SELECT * FROM user_id WHERE uname='".$_REQUEST[uname]."'") or die(mysql_error());
$row=mysql_fetch_array($select);

if($row)
{
    if($row['password']==$_REQUEST['password'])
    {
        session_start();
        $_SESSION['name']=$_REQUEST['uname'];
    }
    else
    {
        header("Location:err-login.php");
    }
}
else
{
    die('Query Failed');
}

 

i have added the mysql_error() function which will show any errors within your query. I have also fixed your query.

in the bit where you call $_SESSION['name'] replace it with:

 

if(isset($_SESSION['name']))
{
    echo "Welcome ".$_SESSION['name'];
}
else
{
    echo 'No Username Set Into Session[\'name\']';
}

 

this will say if there is no session set.

 

I must say though your code is a mess. split the css, html and php into different files. also on html attributes like:

 

<p align=center>

 

use quotes on the values:

 

<p align="center">

 

or for better markup use proper css styling:

 

<p style="text-align:center;">

Link to comment
Share on other sites

Somehow I was able to manage the problem.

What i did was that my earlier script was:

 

<h2><?php

        {

if(isset($_SESSION['name']))

echo "Welcome ".$_SESSION['name'];

}

?></h2>

 

 

I only removed that h2 element and inserted in echo like this:

if(isset($_SESSION['name']))

echo "<h2>Welcome ".$_SESSION['name'],"</h2>";

 

And now it is working fine..!

Link to comment
Share on other sites

@doddsey_65

 

Sorry for the messy code....

It was because my friend handles all the html+css and I handle the php..So i did not try to make any efforts there...!

 

I have some problems here:

like what is the difference b/w $row['name'] and '$row[name]' 

...??

I always had trouble in this quoting stuff..

Link to comment
Share on other sites

$row[name]

means nothing, it would throw up the error use of undefined constand because name hasnt been defined.

 

however

$row['name']

is the right way to use it.

 

consider:

 

$array = array('one', 'two');
echo $array[one];
echo $array['two']

 

the first echo would cause an error stopping the script entirely(depending on your error handling) but he second echo is correct.

 

Make sense?

 

also

'$row[name]'

would just print $row[name] and not the actual array value since anything in single quotes isnt parsed by php.

Link to comment
Share on other sites

Yeah I got it...but still when we modify scripts we use it:

Consider this code

 

$x="INSERT INTO user_id (name,uname,password,email,contact) VALUES ('$_POST[name]','$_POST[uname]','$_POST[password]','$_POST','$_POST[contact]')";

 

It is working fine.....

whereas when i changed it to $_POST['name'] it was giving error.

Link to comment
Share on other sites

well if its working thats news to me but it is bad coding practice. it should be:

 

$x="INSERT INTO user_id (name,uname,password,email,contact) 
VALUES ('".$_POST['name']."','".$_POST['uname']."','".$_POST['password']."','".$_POST['email']."','".$_POST['contact']."')";

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.