berry05 Posted December 16, 2008 Share Posted December 16, 2008 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.. Quote Link to comment https://forums.phpfreaks.com/topic/137261-username-not-showing-up-in-the-table/ Share on other sites More sharing options...
dennismonsewicz Posted December 16, 2008 Share Posted December 16, 2008 can you post the whole script? its usually cause you have forgotten a bracket somewhere Quote Link to comment https://forums.phpfreaks.com/topic/137261-username-not-showing-up-in-the-table/#findComment-717145 Share on other sites More sharing options...
berry05 Posted December 16, 2008 Author Share Posted December 16, 2008 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137261-username-not-showing-up-in-the-table/#findComment-717147 Share on other sites More sharing options...
dennismonsewicz Posted December 16, 2008 Share Posted December 16, 2008 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/137261-username-not-showing-up-in-the-table/#findComment-717159 Share on other sites More sharing options...
berry05 Posted December 16, 2008 Author Share Posted December 16, 2008 omg..its still not updating the table users_items with the username...only the item!!! why!! i cant figure this thing out at all! Quote Link to comment https://forums.phpfreaks.com/topic/137261-username-not-showing-up-in-the-table/#findComment-717161 Share on other sites More sharing options...
dennismonsewicz Posted December 16, 2008 Share Posted December 16, 2008 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137261-username-not-showing-up-in-the-table/#findComment-717166 Share on other sites More sharing options...
berry05 Posted December 16, 2008 Author Share Posted December 16, 2008 it still doesn't show the username in the table... do you think its something on insert2.php? Quote Link to comment https://forums.phpfreaks.com/topic/137261-username-not-showing-up-in-the-table/#findComment-717170 Share on other sites More sharing options...
dennismonsewicz Posted December 16, 2008 Share Posted December 16, 2008 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/137261-username-not-showing-up-in-the-table/#findComment-717172 Share on other sites More sharing options...
premiso Posted December 16, 2008 Share Posted December 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/137261-username-not-showing-up-in-the-table/#findComment-717173 Share on other sites More sharing options...
berry05 Posted December 16, 2008 Author Share Posted December 16, 2008 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/137261-username-not-showing-up-in-the-table/#findComment-717175 Share on other sites More sharing options...
berry05 Posted December 16, 2008 Author Share Posted December 16, 2008 i found the problem ....the $username = $_SESSION['username']; had to be on top of the code...but now my switch isnt working..i did my switch wrong.. Quote Link to comment https://forums.phpfreaks.com/topic/137261-username-not-showing-up-in-the-table/#findComment-717190 Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/137261-username-not-showing-up-in-the-table/#findComment-717265 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.