Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
trying to retrieve data from table and put into a session variable, but can't
Old 08-01-2010, 05:44 AM trying to retrieve data from table and put into a session variable, but can't
Webmaster Talker

Posts: 611
Trades: 0
Hi, I'm trying to use this code below, I echo it out on intro.php page, and it shows blank.

Code:
$_SESSION['charname']=mysql_query("SELECT charname FROM users WHERE username='$username'");
//set character's name in a session.
here is what im trying to echo

Code:
<?php echo $_SESSION['charname'];?>
to try to retrieve a character's name and put it into a session variable I can use throughout the game. Here is the page it is from, it doesn't work, any help greatly appreciated. Thanks. Derek

Code:
<?php

include("connect1.php");
session_start();

 //mysql_error();
 
 $_POST['email']='';
 $_POST['password']='';

// ini_set('display_errors', 1);
//  error_reporting(E_ALL);   // GREAT SNIPPET FOR DEBUGGING OUTPUTS ERRORS TO SCREEN */

 // this is the session declaration , one per page.
/// data is set up in the mysql lite table , rows are 
// record_id, int, 11, not null checked, default, null
//username, varchar, 20, default null
// password, varchar, 20 default, null
//Name, varchar, 20 default null

 //$username='';//we just turned error reporting off to get rid of undeclared message 
 //dont need this
////////////////////////////////////////
////////////////////////////////////////
// a while loop is used to loop through and display output, like a table info, etc.dynamic rows.
$username = $_POST['username'];
$password = $_POST['password'];
 



//echo "<pre>".print_r($_POST, 1)."</pre>";

//$hack = $_GET['hack']; /// we got the hack variable from other page with GET

 
 

// if logoff is set, destroy the session, or unset it.
if(isset($_GET['logoff'])){

    
   unset($_SESSION['userid']);
   
//session_destroy();

$message = "You have been logged off"; // notice here that he used the same variable as before but just changed value


     

   }

 
/*if(isset($hack)){    

   $message = "Naughty Naughty!  "; // COOL

    }*/
 
 
// escape username and password for use in SQL//person said on board "looks fine" like this
//to prevent sql injections
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
 

 
// if fields username and password have contents, then...
if($username && $password){



//$password = md5($password); password not encrypted in database so exclude this
    
$query = mysql_query("SELECT * FROM users WHERE username = '$username' AND (password = '$password' )");

if(!mysql_num_rows($query))
   {
$message = "You do not exist on the system";
   }
else
   {
$result = mysql_fetch_array($query); //creates array called result,//notice we dont need a while loop here.
                                          //if its found a user it will create a populated array, if find nothing, it creates a blank array.
//the mysql_fetch_array automatically gives us our keys for us.
 // if username is set, go on...username is a key for $result, and a field in the table.
   
$message = "You have been logged in";
// session is an array, a php defined word, becomes like a variable.which can be accessed on any page.
// 'userid'here is like a variable. we are going to assign to $_SESSION whatever the $result array contains which is 'username' in this case.
   
$_SESSION['userid'] = $result['Email'];
   
$_SESSION['charname']=mysql_query("SELECT charname FROM users WHERE username='$username'");
//set character's name in a session. 
   
    echo "<script type='text/javascript'>window.location='intro.php'</script>";
   }
   
   

}

 
 


/////////BAN USER CODE/////////////


/*//this is the file that connect to sql
$s=$_SERVER["REMOTE_ADDR"];
//draws IP address of visitor
$ipbancheck="SELECT * from banip where IP='$s'";
$ipbancheck2=mysql_query($ipbancheck);
if (!$ipbancheck2) {
die("query error in $ipbancheck<br.".mysql_error());
}
if (mysql_num_rows($ipbancheck2) > 0)
{
header("Location:http://www.derekvanderven.com/hacker.html");
}
else
{
//put content you want unbanned users to see here
}*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Realm of the Sun Dragon</title>
<link rel="stylesheet" type="text/css" href="css/mainpage.css">
<style type="text/css">
<!--
body {
   background-color: #000000;
}
.style7 {
   font-family: "Times New Roman", Times, serif;
   font-weight: bold;
}
.style8 {
   font-size: larger;
   color: gold;
}
-->
</style></head>

<body>

<div id="container">
<!-- ALL CENTERING DIV -->

<div align="center"> 
 <div  id="header"> <img src="images/images/header.jpg" alt="Realm of  the Sun Dragon" /></div><div id="links"> <table  align="center" cellpadding=3>
   <tr>
  <td style=" font-family: arial; font-size: 12px; ">
    <div align="center"><a href="whatis.html">What is ROSD?</a>
        <b>::</b>
      
        <a href="register_new.php">Create an Account</a>
        <b>::</b>
        <a target=_blank href="#">Forums</a>
        <b>::</b><a href="sendmail.php">Contact Us</a>
      
      </font></div></td>
</tr></table></div> 
<!--end header-->
<div id="leftnav">
 <table   width="450" align="center" cellpadding=0   cellspacing=0><tr><td style="color: white;" align=center  ><div align="center" class="style7">  
   <p><br />
     </p>
   <p class="style8">Sign In:</p>
 </div></td>
</tr><tr><td style="padding: 5px;">
 <div align="center">
         <table>
          <tr><td width="450"><table width="291" align=center>
        <form name="form1" action="checklogin.php" method="post">
          
          <tr>
             <td width="106" align=right class=small>User  name:</td><td width="173" align=left><input class=text  type=text
           name=myusername size=16 ></td></tr>
           <tr><td align=right class=small><div  align="right">Password:</div></td><td align=left> 
            <input class=text type=password name=mypassword size=16>
          </td></tr>
           <tr><td colspan=2>            &nbsp;&nbsp;  &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;  &nbsp;
            <div align="center">
              <input class=button type=submit value="Log In" name=submit id=Submit>
              <br />
              <br />
              <p> If you've forgotten your password,  please <a  href="passrecovery8.php">click here</a>. </p>               
          <p>Logging in constitutes agreement to abide by the  <a href="policies.html">Policies of  ROSD</a>.</p></td>
           </tr>
          </form>
          </table>
        
         </td></tr>
          </table>
  </div></td></tr><tr><td height=4> </td></tr></table>

 </td>
<td valign=top><div align="center"><p>Support the Realm</p> </td>
<p align="center"><b> <p align="center">A friendly message from the Creator, Derek.</p> </b>
<p>  As the number of ROSD players increases, so does the cost of hosting.  I  very much want to keep the game free, so please consider supporting the  development and maintenance of the Realms by donating (once you're  logged in...) or picking up some groovy merchandise from our <a  target="_blank" href="#">Store</a>.  With a donation of 10  dollars or more, you'll receive an in-game gift!  Check the donate page  (accessible from your top menu after you've logged in) for details.  </font></p>
<p align="center"><font  face=Arial>Copyright &copy; 2010 by <a  href="http://derekvanderven.com"> Derek Van  Derven</a></font></p>
</div>
    <div id="rightpic"> <img src="images/images/rightpic.jpg" alt="Realm of the Sun Dragon" />
    </div><!--end rightpic-->
    </div><!-- end center --> 
    </div><!--end container-->
</body>
</html>
silverglade is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-01-2010, 08:19 AM Re: trying to retrieve data from table and put into a session variable, but can't
Extreme Talker

Posts: 173
Trades: 0
Based on my experience, it sounds like you are either not getting any data returned by the query, run the code in your db to ensure it is returning a value. Get ride of all the variable between the php and the back end or the fetch did not work. Again, my experience tells me that you have to fetch a response from the db.
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 08-01-2010, 01:37 PM Re: trying to retrieve data from table and put into a session variable, but can't
Webmaster Talker

Posts: 611
Trades: 0
Thanks. can I do this? what I did on this page, to choose a character name and insert it into the database based on what their current username is in the session? Please any more help greatly appreciated. as in my editor it doesn't look like it's going to work based on the code colors and the red x in eclipse. lol. Thanks. Derek

Code:
<?php
 include("connect1.php");

 include('bouncer_final.php');
 
 //include("bouncer.php"); // kicks the person off if session is not set, its the bouncer, big and fat man.
 

$_SESSION['player']['currentHP'] = 100;
$_SESSION['player']['damage']=10;
$_SESSION['player']['damageLow'] = 1;
$_SESSION['player']['damageHigh'] = 15;

/* End */

if(!empty($_POST['charname']) && !empty($_POST['charConfirm']) && !empty($_POST['submit']))
{
 
$charname = mysql_real_escape_string($_POST['charname']);
$charConfirm = mysql_real_escape_string($_POST['charConfirm']);


   if($charname != $charConfirm)
   {
   echo "<br>The two names did not match<br>";
   
   }
   else
   {
// Check if the email already exists in database
   
$query = "SELECT * FROM users WHERE charname = '$charname' ";
$results = mysql_num_rows(mysql_query($query));
   
   if ($results > 0)
   {
   echo "sorry that name  already exists";
    
   }
   else
   {
    
   
mysql_query("INSERT INTO users (charname)
            VALUES ('$charname') WHERE username='$_SESSION['username']")or die(mysql_error());  
 

   
   echo "success! you have been given the character name of ".$charname;
   }
   
   
   }//else



}//if



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Introduction</title>
<style type="text/css">
body {
background-color:#000000;
color: #ffffff;
}
a:link,a:hover,a:active,a:visited {
color: gold;
}
.style1 {font-family: "Times New Roman", Times, serif}
.style2 {color: #F4C25B}
.style3 {font-family: "Times New Roman", Times, serif; color: #F4C25B; }
.style4 {
   color: gold;
   font-weight: bold;
}
</style>
</head>

<body>


 <div align="right">
   <table align="left" width="206" border="0">
     <tr>
       <td width="200"><span class="style4"><a href="index.php">BACK HOME</a></span></td>
     </tr>
   </table>
    
 <strong><a href="choose_class.php">Go back to class choose</a></strong></div>
 
<a href="logout.php"><strong>LOG OFF</strong></a><br />
  <br />
  
<p align="center"><img src="dragon_images/glass_dragon_front_large.jpg" width="266" height="376" /></p>
<table align="center" width="700" border="0">
<form action="username.php" method="post> 
 Please choose a character name <input type="text" name="charname"> 
 <br /> 
 Please enter the character name again<input type="text" name="charConfirm">
 <br />
 <input type="submit" name="submit" value="Submit name"></form>
</table>
<h2 align="center" class="style3">&nbsp;</h2>
</body>
</html>
silverglade is offline
Reply With Quote
View Public Profile
 
Old 08-01-2010, 02:08 PM Re: trying to retrieve data from table and put into a session variable, but can't
Webmaster Talker

Posts: 611
Trades: 0
I tried this as well but it doesn't work.

Code:
$username=$_SESSION['username'];
                mysql_query("INSERT INTO users (charname)
                VALUES ('$charname') WHERE 'username' = '$username'")or die(mysql_error());
Code:
<?php
 include("connect1.php");

 include('bouncer_final.php');
 
 //include("bouncer.php"); // kicks the person off if session is not set, its the bouncer, big and fat man.
 

$_SESSION['player']['currentHP'] = 100;
$_SESSION['player']['damage']=10;
$_SESSION['player']['damageLow'] = 1;
$_SESSION['player']['damageHigh'] = 15;

/* End */

if(!empty($_POST['charname']) && !empty($_POST['charConfirm']) && !empty($_POST['submit']))
{
 
    $charname = mysql_real_escape_string($_POST['charname']);
    $charConfirm = mysql_real_escape_string($_POST['charConfirm']);


    if($charname != $charConfirm)
    {
        echo "<br>The two names did not match<br>";
    
    }
    else
    {
    // Check if the email already exists in database
    
        $query = "SELECT * FROM users WHERE charname = '$charname' ";
        $results = mysql_num_rows(mysql_query($query));
        
            if ($results > 0)
            {
                echo "sorry that name  already exists";
             
            }
            else
            {
                 
                $username=$_SESSION['username'];
                mysql_query("INSERT INTO users (charname)
                VALUES ('$charname') WHERE 'username' = '$username'")or die(mysql_error());  
 

                
                echo "success! you have been given the character name of ".$charname;
            }
    
    
    }//else



}//if



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Introduction</title>
<style type="text/css">
body {
background-color:#000000;
color: #ffffff;
}
a:link,a:hover,a:active,a:visited {
color: gold;
}
.style1 {font-family: "Times New Roman", Times, serif}
.style2 {color: #F4C25B}
.style3 {font-family: "Times New Roman", Times, serif; color: #F4C25B; }
.style4 {
    color: gold;
    font-weight: bold;
}
</style>
</head>

<body>


 <div align="right">
   <table align="left" width="206" border="0">
     <tr>
       <td width="200"><span class="style4"><a href="index.php">BACK HOME</a></span></td>
     </tr>
   </table>
    
 <strong><a href="choose_class.php">Go back to class choose</a></strong></div>
 
<a href="logout.php"><strong>LOG OFF</strong></a><br />
  <br />
  
<p align="center"><img src="dragon_images/glass_dragon_front_large.jpg" width="266" height="376" /></p>
<table align="center" width="700" border="0">
<form action="username.php" method="post> 
 Please choose a character name <input type="text" name="charname"> 
 <br /> 
 Please enter the character name again<input type="text" name="charConfirm">
 <br />
 <input type="submit" name="submit" value="Submit name"></form>
</table>
<h2 align="center" class="style3">&nbsp;</h2>
</body>
</html>
silverglade is offline
Reply With Quote
View Public Profile
 
Old 08-01-2010, 02:22 PM Re: trying to retrieve data from table and put into a session variable, but can't
Webmaster Talker

Posts: 611
Trades: 0
I'm getting close. I'm using this code

Code:
       $username=$_SESSION['username'];
                mysql_query("INSERT INTO users (charname)
                VALUES ('$charname') WHERE username = '$username'")or die(mysql_error());
but I'm getting this error

Code:
You have an error in your SQL syntax; check the manual that corresponds  to your MySQL server version for the right syntax to use near 'WHERE  username = ''' at line 2
any help greatly appreciated.

Last edited by silverglade; 08-01-2010 at 02:31 PM..
silverglade is offline
Reply With Quote
View Public Profile
 
Old 08-01-2010, 07:10 PM Re: trying to retrieve data from table and put into a session variable, but can't
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
$username is empty.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-03-2010, 09:04 AM Re: trying to retrieve data from table and put into a session variable, but can't
phpdasan's Avatar
Experienced Talker

Posts: 37
Name: Karthick B
Trades: 0
There is a string error in your code..

Try this..

mysql_query("INSERT INTO users (charname)
VALUES ('$charname') WHERE username = '" . $username ."'")or die(mysql_error());

if you still get error.. just echo the session variable and see what you are getting.. Make sure the session variable is not empty..

or else just echo the query and see what you get.. and change the code accordingly.
__________________
There is no secret ingredient.
phpdasan is offline
Reply With Quote
View Public Profile
 
Old 08-03-2010, 10:55 AM Re: trying to retrieve data from table and put into a session variable, but can't
Skilled Talker

Posts: 54
Trades: 0
cant use insert with WHERE clause. check it out. hope it helps.
kani alavi is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to trying to retrieve data from table and put into a session variable, but can't
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29980 seconds with 12 queries