Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: PHP and mySQL Linking Problems

  1. #1
    Join Date
    Jan 2008
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation PHP and mySQL Linking Problems

    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:

    PHP Code:

    <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:

    PHP Code:
    <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!

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    If your table has a column named "tag", try the following:

    Code:
    <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.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jan 2008
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Here you go:

    Code:
    <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.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Jan 2008
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    is that what would hide the iframe bit if i don't have the information "tag" for them?

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    That is. Basically what the script does is the following:

    Code:
    <?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

    Code:
    //checks cookies to make sure they are logged in 
    if(isset($_COOKIE['ID_my_site'])) 
    {
    if the cookie is set, then contiue

    Code:
    $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.

    Code:
    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

    Code:
    //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.

    Code:
    //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.

    Code:
    $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

    Code:
    } 
    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.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  7. #7
    Join Date
    Jan 2008
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  8. #8
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    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:

    Code:
    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
    
    }
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. #9
    Join Date
    Jan 2008
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

    Code:
    //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!

  10. #10
    Join Date
    Jan 2008
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:

    Code:
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •