DEVILofDARKNESS Posted March 27, 2009 Share Posted March 27, 2009 I am (trying) to create a text based game. For wich they have to create a user name, ... (wich works perfectly) and when they login for the first time, they should be redirected to createnation?page=1 (also the redirection works fine), but on this page I get some problems. -If you have finished page 1, you get redirected to page 2, but nothing is insert in my database. -if you come on page 2, you see the fields from the database, but every field is shown followed by the radio box (if you don't know what I mean http://europia.uuuq.com/createnation.php?page=2) Can somebody help me? <?php session_start(); require_once 'config.php'; $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); $page = (int)$_GET['page']; $username = $_SESSION['username']; $query = "SELECT user_id FROM users WHERE user_name = '$username'"; $result = mysql_query($query); list($id) = mysql_fetch_row($result); if(isset($_POST['submit1'])) { $name = $_POST['Name']; $capital = $_POST['capital']; $religion = $_POST['religion']; $government = $_POST['government']; $query = "SELECT COUNT(nation_id) FROM nations"; $result = mysql_query($query); list($nationid) = mysql_fetch_row($result); $query = "INSERT INTO nations(nation_id,nation_name,nation_ruler_id,nation_capital,nation_religion,nation_government,nation_money) VALUES ('$nationid','$name','$id','$capital','$religion','$government','500'"; $result = mysql_query($query); header('location:/createnation.php?page=2'); }elseif(isset($_POST['submit2'])) { $regchk = $_POST['region']; $query = "SELECT region_cost FROM regions WHERE region_name = '$regchk'"; $result = mysql_query($query); list($chkregprice) = mysql_fetch_row($result); if($chkregprice > 500) { echo "Sorry You don't have enough money (You have €500)"; }elseif($chkregprice <= 500) { $query = "UPDATE regions SET region_ruler_id = '$id' WHERE region_name='$regchk"; $result = mysql_query($query); $newmoney = 500 - $chkregprice; $query = "UPDATE nations SET nation_money = '$newmoney' WHERE nation_ruler_id = '$id'"; $result = mysql_query($query); echo "You have enough Money!,<br> The region is now yours.<br> You have now €{$newmoney} left<p>"; header('location:/createnation.php?page=2'); } } ?> <html> <head> <title> EUROPIA! -- Create A Nation! </title> <meta name="author" content="Kruptein"> <link rel="shortcut icon" href="/images/favicon.ico"> </head> <body> <table border="1" width="100%" height="100%"> <tr> <td colspan = "3" height="15%"> <b><u><center>EUROPIA! -- Create A Nation</center></u></b> </td> </tr> <tr> <td width="10%"> <iframe src="/navigation/loggedin.php" frameborder="0" width="100%" scrolling="no" height="100%"></iframe> </td> <td height="80%" width="90%"> <center> <?php if($page == 1) { echo '<b>Welcome,' . $username . '</b><br> It\'s now up to you to create your own empire and conquer the whole world!<p> But first we have to get some information about your new nation.<br> STEP ONE:<p> <form action="" method="post"> <table border="0"> <tr><td>Nation Name: </td><td><input type="text" name="Name"></td><td></td></tr> <tr><td>The Capital Name of Your Nation: </td><td><input type="text" name="capital"></td><td></td></tr> <tr><td>Your Primary Religion*: </td><td><select name="religion"><option value="Buddhism">Buddishm</option><option value="Christianity">Christianity</option><option value="islam">Islam</option><option value="nonreligious">Non Religious</option><option value="Hinduism">Hinduism</option></select></td><td><a href="http://www.orionsarm.com/civ/government.html" target="_blank"><i>What are the differences?</i></a></td></tr> <tr><td>Government Type: </td><td><select name="government"><option value="Democracy">Democracy</option><option value="Republic">Republic</option><option value="Dictatorship">DictatorShip</option><option value="Monarchy">Monarchy</option><option value="Plutocracy">Plutocracy</option><option value="Stratocracy">Stratocracy</option><option value="Theocracy">Theocracy</option></select></td><td><a href="http://www.adherents.com/Religions_By_Adherents.html" target="_blank"><i>What are the differences?</i></a></td></tr> <tr><td colspan = "3"><i>*You can create an own religion if you have more money</i></td></tr> <tr><td><input type="submit" name="submit1" value="Create!"></td><td>--</td><td<input type="reset"></td></tr> </table> </form>'; }elseif($page == 2) { echo 'Now you have created a nation, you also need a region to rule over.<p> STEP TWO:<br> <form action="" method="post"> <table border="0"> <tr>'; $query = "SELECT region_name,region_population,region_outputs,region_price FROM regions"; $result = mysql_query($query); while($fields = mysql_fetch_row($result)) { foreach($fields as $field) { echo '<td>' . $field . '</td><td><input type="radio" name="region" value=\"' . $field["name"] . '\"></td></tr>'; } } echo '<tr><td><input type="submit" name="submit2" value="Buy"> -- <input type="reset"></td></tr> </table> </form>'; }elseif($page == 3) { echo 'Congrulations! You have Completed All Steps,<br> You can now start to play. Log once again in and you are able to play Europia!</p> <a href="/login.php">Login</a>'; }else{ echo 'There was a problem'; }?> </center> </td> </tr> <tr> <td colspan = "3" height="5%"> <center>Made by Kruptein</center> </td> </tr> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted March 27, 2009 Author Share Posted March 27, 2009 Ok, found why it didn't add a row to the database. There is still one problem left. I get this: http://europia.uuuq.com/createnation.php?page=2 and there should be one row each country, with a checkbox. what's wrong? I think the problem is here: <?php$query = "SELECT region_name,region_population,region_outputs,region_price FROM regions"; $result = mysql_query($query); while($fields = mysql_fetch_row($result)) { foreach($fields as $field) { echo '<td>' . $field . '</td><td><input type="radio" name="region" value="' . $field["region_name"] . '"></td></tr>'; } } echo '<tr><td><input type="submit" name="submit2" value="Buy"> -- <input type="reset"></td></tr> </table> </form>';?> Quote Link to comment Share on other sites More sharing options...
Brian W Posted March 27, 2009 Share Posted March 27, 2009 On line 10, 18, 21, 26, 32, and 35 change it to: $result = mysql_query($query) or die(mysql_error()."<br>".$query); mysql_error() will spit our your problems. If you don't want to do it in all those places, it does seem that the issue is going on at the occurrence at line 26, so at least set up that error reporting there. I know it is terribly to look for other's problems, but please can sombody look what is probably wrong? LOL, I was already replying before you made that comment... Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted March 27, 2009 Author Share Posted March 27, 2009 That is the problem, I don't get any error at all. if you look at the link http://europia.uuuq.com/createnation.php?page=2 You can see there isn't any link (because you asked it I also added the or die's, but it didn't change anything). The rows are just wrong Now it's showen as name checkbox value1 checkbox value2 checkbox ... name2 checkbox ... And it should be name1 value1 value2 ... checkbox name2 value2... Quote Link to comment Share on other sites More sharing options...
Brian W Posted March 27, 2009 Share Posted March 27, 2009 umm, select a country and click "buy"... you get this error: Unknown column 'region_cost' in 'field list' SELECT region_cost FROM regions WHERE region_name = '2' in the regions table, there is not column called region_cost Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted March 27, 2009 Author Share Posted March 27, 2009 Ok, fixed the problem, still the rows aren't changed... and if you click on buy you get an error, which is logical because there is a problem near: $query = "SELECT region_name,region_population,region_outputs,region_price FROM regions"; $result = mysql_query($query); while($fields = mysql_fetch_row($result)) { foreach($fields as $field) { echo '<td>' . $field . '</td><td><input type="radio" name="region" value="' . $field["region_name"] . '"></td></tr>'; } } -The rows aren't right, and $field["region_name"] doesn't work like I suspected. :-s Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted March 28, 2009 Author Share Posted March 28, 2009 still not fixed :-( Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted March 28, 2009 Author Share Posted March 28, 2009 btw how should you declare just a piece of a foreach loop? $field['piece'] ??? Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted March 28, 2009 Share Posted March 28, 2009 btw how should you declare just a piece of a foreach loop? $field['piece'] ??? Don't know what exactly you mean with that. Maybe using print_r() would help you to see whats in the array. That way you will see what it is you can use for your output and if a value is present in that array. echo "<pre>"; print_r($field); echo "</pre>"; Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted March 28, 2009 Author Share Posted March 28, 2009 can you help me with the rows? Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted March 29, 2009 Share Posted March 29, 2009 change : mysql-fetch-row to : mysql_fetch_array Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted March 30, 2009 Author Share Posted March 30, 2009 Nope, it only had as result that every row is shown twice :-s Quote Link to comment 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.