PDA

View Full Version : PHP and mySQL Linking Problems



bradenkeithcom
01-04-2008, 11:10 PM
Alright so I created a user login mixing PHP and mySQL. It's pretty simple, they create account, it sends the login and other info to a database table called "users". After they login they are taken to members.php.
Here's the script for that page:




<html>
<head>
<title>LCF Games</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
// Connects to your Database
mysql_connect("XXXXX", "braden_gaming", "XXXXX") or die(mysql_error());
mysql_select_db("braden_gaming") or die(mysql_error());

//checks cookies to make sure they are logged in
if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{

//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password'])
{ header("Location: login.php");
}



//otherwise they are shown the admin area
else
{



echo "Admin Area<p>";
echo "<a href=logout.php>Logout</a>";

}
}
}
else

//if the cookie does not exist, they are taken to the login screen
{
header("Location: login.php");
}




?>

<iframe src="http://gamercard.xbox.com/<?php echo $tag; ?>.card" scrolling="no" frameBorder="0" height="140" width="204"><?php echo $tag; ?></iframe>


Test 1, 2 ,3
</body>
</html>


My problem is with this part:



<iframe src="http://gamercard.xbox.com/<?php echo $tag; ?>.card" scrolling="no" frameBorder="0" height="140" width="204"><?php echo $tag; ?></iframe>


Where you see <?php echo $tag; ?> I'm wanting to grab a particular information about the logged-in user from a field called "tag"... being their xbox live gamertag. This way when they navagate to the page that's what they see, their gamertag.

Now I know I have to define where to get tag somewhere previously (possibly via array?) but I'm not sure how as i just learned PHP and mySQL yesterday. So any help I could get would be great. Thanks!

thetestingsite
01-04-2008, 11:16 PM
If your table has a column named "tag", try the following:




<html>
<head>
<title>LCF Games</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
// Connects to your Database
mysql_connect("XXXXX", "braden_gaming", "XXXXX") or die(mysql_error());
mysql_select_db("braden_gaming") or die(mysql_error());

//checks cookies to make sure they are logged in
if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{

//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password'])
{ header("Location: login.php");
}



//otherwise they are shown the admin area
else
{



echo "Admin Area<p>";
echo "<a href=logout.php>Logout</a>";

}

$tag = $info['tag'];

}
}
else

//if the cookie does not exist, they are taken to the login screen
{
header("Location: login.php");
}




?>

<iframe src="http://gamercard.xbox.com/<?php echo $tag; ?>.card" scrolling="no" frameBorder="0" height="140" width="204"><?php echo $tag; ?></iframe>


Test 1, 2 ,3
</body>
</html>


Probably not the best way to do it, but should work nonetheless.

Hope this helps.

bradenkeithcom
01-04-2008, 11:21 PM
AHHH THAT WORKED!

Thank you so much! you're seriously a life savor... do you know how to make it to where if I don't have that information on them (it's an optional field) that it'll remain hidden... the whole <iframe> bit? or should I do a separate post?

thetestingsite
01-04-2008, 11:24 PM
Here you go:



<html>
<head>
<title>LCF Games</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
// Connects to your Database
mysql_connect("XXXXX", "braden_gaming", "XXXXX") or die(mysql_error());
mysql_select_db("braden_gaming") or die(mysql_error());

//checks cookies to make sure they are logged in
if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{

//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password'])
{ header("Location: login.php");
}



//otherwise they are shown the admin area
else
{



echo "Admin Area<p>";
echo "<a href=logout.php>Logout</a>";

}

$tag = $info['tag'];
?>
<iframe src="http://gamercard.xbox.com/<?php echo $tag; ?>.card" scrolling="no" frameBorder="0" height="140" width="204"><?php echo $tag; ?></iframe>
<?php
}
}
else

//if the cookie does not exist, they are taken to the login screen
{
header("Location: login.php");
}




?>

Test 1, 2 ,3
</body>
</html>


Hope this helps.

bradenkeithcom
01-04-2008, 11:28 PM
is that what would hide the iframe bit if i don't have the information "tag" for them?

thetestingsite
01-04-2008, 11:35 PM
That is. Basically what the script does is the following:



<?php
// Connects to your Database
mysql_connect("XXXXX", "braden_gaming", "XXXXX") or die(mysql_error());
mysql_select_db("braden_gaming") or die(mysql_error());


connect to the database



//checks cookies to make sure they are logged in
if(isset($_COOKIE['ID_my_site']))
{


if the cookie is set, then contiue



$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];

$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());


assign some variables ($username and $pass), then check the database table "users" for that username. If no results, then kill the script and print the mysql error.



while($info = mysql_fetch_array( $check ))
{


basically, while you are fetching the columns associated with the above query to the database, continue executing the script



//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password'])
{ header("Location: login.php");
}


as the comments say, redirect (which will fail) if the password info is wrong.



//otherwise they are shown the admin area
else
{

echo "Admin Area<p>";
echo "<a href=logout.php>Logout</a>";

}


Show the stuff above if the login info is correct.



$tag = $info['tag'];
?>
<iframe src="http://gamercard.xbox.com/<?php echo $tag; ?>.card" scrolling="no" frameBorder="0" height="140" width="204"><?php echo $tag; ?></iframe>
<?php
}


assign the variable $tag and show the iframe



}
else

//if the cookie does not exist, they are taken to the login screen
{
header("Location: login.php");
}



Again, a failed redirect if the cookie doesn't exist.

To get the header redirects to work properly, you need to make sure that any text is sent to the browser after the header() tag.

Hope this helps.

bradenkeithcom
01-04-2008, 11:39 PM
Yeah that's what I'm saying. I appreciate the help btw.

But I'm saying, what if they have the right login and during registration they only filled out the id and pass but didn't fill out their gamercard ($tag). I wouldn't want to show the iframe because it would pop up an error. So how do i check to see if that column is blank and if so kill the <iframe>. You know what I mean?

Thanks

thetestingsite
01-04-2008, 11:42 PM
Oh, I see now; sorry for the confusion (long day at work). Anyways, this code should do it for you. Simply replace the highlighted part in my second post with the following:



if ($info['tag'] != '') { //check to see if tag field is empty and if not, continue
?>
<iframe src="http://gamercard.xbox.com/<?php echo $info['tag']; ?>.card" scrolling="no" frameBorder="0" height="140" width="204"><?php echo $info['tag']; ?></iframe>
<?php
} //end if tag is empty

}

bradenkeithcom
01-04-2008, 11:52 PM
ok... we're getting dangerously close. I can tell this is what I need, it's just the position of it. I replaced the highlighted on post 2 and it didn't even show my page now. :(


//otherwise they are shown the admin area
else
{




echo "User Area<p>";
echo "<a href=logout.php>Logout</a><br><br>";
}
$tag = $info['tag'];
}
}
else

//if the cookie does not exist, they are taken to the login screen
{
header("Location: login.php");
}




?>

<iframe src="http://gamercard.xbox.com/<?php echo $tag; ?>.card" scrolling="no" frameBorder="0" height="140" width="204"><?php echo $tag; ?></iframe>

</body>
</html>


where do i put it here?

BTW, wouldn't this also kill the rest of my document if they don't have that part filled out? I have a whole rest of the page that needs to be shown if it's not filled in.

Thanks!

bradenkeithcom
01-05-2008, 01:32 AM
ok I fixed it... I basically retyped what you did and it worked... I don't know the difference b/w them but this is what I cam up with:


echo "User Area<p>";
echo "<a href=logout.php>Logout</a><br><br>";
}
if ($info['tag'] != ''){ ?>
<iframe src="http://gamercard.xbox.com/<?php echo $info['tag']; ?>.card" scrolling="no" frameBorder="0" height="140" width="204"><?php echo $info['tag']; ?></iframe>
<?php
}}
}
else

bradenkeithcom
01-05-2008, 01:33 AM
Thank You So Much For Your Help!

thetestingsite
01-05-2008, 01:35 AM
No problem, but I think that the issue was with that comment I put into the code:



//check to see if tag field is empty and if not, continue


Glad to hear its working for you.