Jump to content

username not showing up in the table


berry05

Recommended Posts

theres no username showing up in table users_items but there is the item...i got a error in a code saying..

Parse error: syntax error, unexpected $end in C:\wamp\www\test\login\hoe.php on line 47

 

so i got rid of the { in

session_start();
if(isset($_SESSION['Username'])) {

 

 

but when i do that i get no error and the username doesnt show up in the table when a user buys a hoe..

Link to comment
https://forums.phpfreaks.com/topic/137261-username-not-showing-up-in-the-table/
Share on other sites

hoe.php

 

<?php
session_start();
if(isset($_SESSION['Username'])) {
?>


<p></p>
<p><a href="shop.php">Shop</a> </p>
<?php

//connect
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("textgame") or die(mysql_error());

// switch

$HOE = "HOE";
echo "you have bought a hoe!!";
switch ($HOE){
case "not enough gold":
		$Result = mysql_query("SELECT gold FROM users WHERE username='$username'");
	$Row = mysql_fetch_array($Result );
if ($Row < 50)
	echo "You dont have enough gold to purchase a hoe!!";
	break;
case "you have already baught a hoe":
$Username = $_SESSION['Username'];
	$query = "SELECT * FROM users_items WHERE username='$Username' AND item = 'hoe'";
	$result = mysql_query($query) or die( mysql_error());
	     if ($row = mysql_fetch_row($result)) 
	echo "you have already bought a hoe!!";
	break;	
case "HOE":
		mysql_query("INSERT INTO users_items (username, item) VALUES('$Username', 'hoe' ) ") 
	or die(mysql_error());
	break;	
}


//session_register("inventory");
$Query = "SELECT item FROM users_items WHERE username='$Username'";
$Result = mysql_query($Query);
$Row = mysql_fetch_array($Result);
$_SESSION['inventory'] = $Row['item'];


?>

 

insert2.php

 

<?php
//ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="textgame"; // Database name
$tbl_name="users"; // Table name 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$Username = (isset($_POST['username'])) ? mysql_real_escape_string($_POST['username']) : FALSE;
$password1 = (isset($_POST['password1'])) ? $_POST['password1'] : FALSE;
$password2 = (isset($_POST['password2'])) ? $_POST['password2'] : FALSE;
if(!$Username) die('username not set');
if(!$password1) die('Password1 not set');
else $password1 = md5($password1);
if(!$password2) die('Password2 not set');
else $password2 = md5($password2);
$query = "SELECT * FROM users WHERE username='$Username' AND password1='$password1' AND password2='$password2'";
$result = mysql_query($query) or die( mysql_error());
$count=mysql_num_rows($result);
if($count==1)
{
session_start();
//session_register("username");
$_SESSION['username'] = $Username;
$queryy = "SELECT gold FROM users WHERE username='$Username'";
$result1 = mysql_query($queryy);
$row = mysql_fetch_assoc($result1);
$_SESSION['gold'] = $row['gold'];
//session_register("inventory");
$Query = "SELECT item FROM users_items WHERE username='$Username'";
$Result = mysql_query($Query);
$Row = mysql_fetch_array($Result);
$_SESSION['inventory'] = $Row['item'];
  

header("location:index2.php"); 
}
else {
echo "Wrong Username or Password";
}

?>

here is an updated hoe.php file

 

<?php
session_start();
if(isset($_SESSION['Username'])) {
?>


<p></p>
<p><a href="shop.php">Shop</a> </p>
<?php

   //connect
   mysql_connect("localhost", "root", "") or die(mysql_error());
   mysql_select_db("textgame") or die(mysql_error());
   
   // switch
   
   $HOE = "HOE";
echo "you have bought a hoe!!";
switch ($HOE){
   case "not enough gold":
         $Result = mysql_query("SELECT gold FROM users WHERE username='$username'");
      $Row = mysql_fetch_array($Result );
if ($Row < 50)
      echo "You dont have enough gold to purchase a hoe!!";
      break;
   case "you have already baught a hoe":
   $Username = $_SESSION['Username'];
      $query = "SELECT * FROM users_items WHERE username='$Username' AND item = 'hoe'";
      $result = mysql_query($query) or die( mysql_error());
           if ($row = mysql_fetch_row($result)) 
      echo "you have already bought a hoe!!";
      break;   
   case "HOE":
         mysql_query("INSERT INTO users_items (username, item) VALUES('$Username', 'hoe' ) ") 
      or die(mysql_error());
      break;   
}


//session_register("inventory");
$Query = "SELECT item FROM users_items WHERE username='$Username'";
$Result = mysql_query($Query);
$Row = mysql_fetch_array($Result);
$_SESSION['inventory'] = $Row['item'];
} //I added this bracket here


?>

try this

 

<?php
if ($Row < 50){
      echo "You dont have enough gold to purchase a hoe!!";
}
      break;
   case "you have already baught a hoe":
   $Username = $_SESSION['Username'];
      $query = "SELECT * FROM users_items WHERE username='$username' AND item = 'hoe'";
      $result = mysql_query($query) or die( mysql_error());
           if ($row = mysql_fetch_row($result)) 
      echo "you have already bought a hoe!!";
      break;   
   case "HOE":
         mysql_query("INSERT INTO users_items (username, item) VALUES('$username', 'hoe' ) ") 
      or die(mysql_error());
      break;   
}
?>

<?php
if ($Row < 50){
      echo "You dont have enough gold to purchase a hoe!!";
}
      break;
   case "you have already baught a hoe":
   $Username = $_SESSION['Username'];
      $query = "SELECT * FROM users_items WHERE username='$Username' AND item = 'hoe'";
      $result = mysql_query($query) or die( mysql_error());
           if ($row = mysql_fetch_row($result)) 
      echo "you have already bought a hoe!!";
      break;   
   case "HOE":
         mysql_query("INSERT INTO users_items (username, item) VALUES('$Username', 'hoe' ) ") 
      or die(mysql_error());
      break;   
}
?>

 

I forgot to recapitalize your username var. Try the above

Remember php is CasE SenSitIve

 

<?php
session_start();
if(isset($_SESSION['Username'])) {
$username = $_SESSION['Username']; // make sure it should be Username and not username for the session part.
?>


<p></p>
<p><a href="shop.php">Shop</a> </p>
<?php

   //connect
   mysql_connect("localhost", "root", "") or die(mysql_error());
   mysql_select_db("textgame") or die(mysql_error());
   
   // switch
   
   $HOE = "HOE";
echo "you have bought a hoe!!";
switch ($HOE){
   case "not enough gold":
         $Result = mysql_query("SELECT gold FROM users WHERE username='$username'");
      $Row = mysql_fetch_array($Result );
if ($Row < 50)
      echo "You dont have enough gold to purchase a hoe!!";
      break;
   case "you have already baught a hoe":
   $Username = $_SESSION['Username'];
      $query = "SELECT * FROM users_items WHERE username='$username' AND item = 'hoe'"; // needed to be lowercase $username
      $result = mysql_query($query) or die( mysql_error());
           if ($row = mysql_fetch_row($result)) 
      echo "you have already bought a hoe!!";
      break;   
   case "HOE":
         mysql_query("INSERT INTO users_items (username, item) VALUES('$username', 'hoe' ) ") 
      or die(mysql_error()); // also needed to be lowercase $username
      break;   
}


$Query = "SELECT item FROM users_items WHERE username='$username'"; // also needs to be $username
$Result = mysql_query($Query);
$Row = mysql_fetch_array($Result);
$_SESSION['inventory'] = $Row['item'];
} //I added this bracket here


?>

 

Give that a try.

still not showing the username in the table...i gotta eat so i'll be back later...idk what to do...here was my old code that was worknig...

<?php
session_start();
if(isset($_SESSION['username'])){
?>


<p></p>
<p><a href="shop.php">Shop</a> </p>
<?php

//connect
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("textgame") or die(mysql_error());

//check if hoe is already bought by that user
$username = $_SESSION['username'];
$query = "SELECT * FROM users_items WHERE username='$username' AND item = 'hoe'";
$result = mysql_query($query) or die( mysql_error());
if($row = mysql_fetch_row($result))
{
echo "You only need to buy one hoe!!";
	}
	else
	{
	// check if user has enough gold
	$Result = mysql_query("SELECT gold FROM users WHERE username='$username'");
	$Row = mysql_fetch_array($Result );
	if ($Row < 50) }
	echo "You dont have enough gold to purchase a hoe!!";

	}
	else
	{
	// start adding data
	mysql_query("INSERT INTO users_items (username, item) VALUES('$username', 'hoe' ) ") 
	or die(mysql_error());
	echo "you have bought a hoe!!";
}
}

//session_register("inventory");
$Query = "SELECT item FROM users_items WHERE username='$username'";
$Result = mysql_query($Query);
$Row = mysql_fetch_array($Result);
$_SESSION['inventory'] = $Row['item'];


?>

Yep, your switch statement is setup wrong. I think If's are better for this scenario anyhow.

 

A switch statement is really for when you know what data to expect, let's say for 1 page to do multiple functions.

 

Say I have a page I want to use for adding/deleteing or modfiying an item:

 

<?php

$page = isset($_GET['page'])?$_GET['page']:'home';

switch ($page) {
     case 'add':
           echo 'Do adding here';
     break;
     case 'delete':
           echo 'Do your deleteing here';
     break;
     case 'modify':
     case 'edit':
            echo 'Do modiying and editing here (since both are the same thing)';
     break;
     case 'home':
            echo 'Here is the homepage, choose to edit or delete or add.';
     break;
}
?>

 

Hope that helps you know what a switch is really for.

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.