rastus Posted December 5, 2006 Share Posted December 5, 2006 I'm having trouble with this....I'm at wits end why it does not work. when I click add city, the page refreshes but nothing is written to the database. I can manually enter the information into the database and it will show up for editing or deletion, but the post action on the script refuses to work. Any help or ideas would be much much appreaciated.Kenny[code]}function addcity(){global $db, $ir, $c, $h, $userid;$minlevel=abs((int) $_POST['minlevel']);$name=$_POST['name'];$desc=$_POST['desc'];if($minlevel and $desc and $name){$q=$db->query("SELECT * FROM cities WHERE cityname='{$name}'");if($db->num_rows($q)){print "Sorry, you cannot have two cities with the same name.";$h->endpage();exit;}$db->query("INSERT INTO cities VALUES(NULL, '$name', '$desc', '$minlevel')");print "City {$name} added to the game.";stafflog_add("Created City $name");}else{print "<h3>Add City</h3><hr /><form action='staff_cities.php?action=addcity' method='post'>Name: <input type='text' name='name' /><br />Description: <input type='text' name='desc' /><br />Minimum Level: <input type='text' name=minlevel' /><br /><input type='submit' value='Add City' /></form>";}[/code]I attached the full page, in case anyone needs to see more of the code.[attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/29481-methodpost-not-working/ Share on other sites More sharing options...
wildteen88 Posted December 5, 2006 Share Posted December 5, 2006 Chnage your addcity function to the following:[code]function addcity(){ global $db, $ir, $c, $h, $userid if(isset($_POST['minlevel']) && isset($_POST['desc']) && isset($_POST['name'])) { $minlevel=abs((int) $_POST['minlevel']); $name = $_POST['name']; $desc = $_POST['desc']; $q=$db->query("SELECT * FROM cities WHERE cityname='{$name}'"); if($db->num_rows($q)) { print "Sorry, you cannot have two cities with the same name."; $h->endpage(); exit; } $db->query("INSERT INTO cities VALUES(NULL, '$name', '$desc', '$minlevel')"); print "City {$name} added to the game."; stafflog_add("Created City $name"); } else { print "<h3>Add City</h3><hr /><form action='staff_cities.php?action=addcity' method='post'>Name: <input type='text' name='name' /><br />Description: <input type='text' name='desc' /><br />Minimum Level: <input type='text' name='minlevel' /><br /><input type='submit' value='Add City' /></form>"; }}[/code]The was with your html for the minlevel form field:[code]Minimum Level: <input type='text' name=minlevel' /><br />[/code]Notice there is no apostrophe after name=Also I have edited the syntax of the function slightly too. You should check/validate user input before your use it.Another thing is to try and format your code. It will make your code easier to read and maintain rather than having all you code in one big jumbled mess. You press the tab button or use 2 to 4 spaces everytime you go into a code block such as a function, if/else statements, switch statements etc. Then when you come out of the code block (closing curly brace }) you delete 2 or 4 spaces. Example:[code=php:0]if(bla blah){ // this opens up a code a block so we indent the contents $someVar = blah; // another code block switch(blah blah) { // this another code block so we indent the contents again (twice) case blah : // again indent the content for the case break; // place the break back in line with the case it belongs to default: // some default value break; } // close the code block for the switch, notice it goes in line with the opening curly brace for the code block} // close if statement[/code]As you can see it can make the code more easier to read and maintain as you can see the code blocks. Quote Link to comment https://forums.phpfreaks.com/topic/29481-methodpost-not-working/#findComment-135632 Share on other sites More sharing options...
rastus Posted December 5, 2006 Author Share Posted December 5, 2006 thanks very much.. I'll give it a try and see what happens.. :) Quote Link to comment https://forums.phpfreaks.com/topic/29481-methodpost-not-working/#findComment-135648 Share on other sites More sharing options...
rastus Posted December 6, 2006 Author Share Posted December 6, 2006 when using the code you posted I get [code]Parse error: parse error, unexpected T_IF, expecting ',' or ';' in /home/my4x4toy/public_html/*******/staff_cities.php on line 18[/code]this is line 18[code]if(isset($_POST['minlevel']) && isset($_POST['desc']) && isset($_POST['name']))[/code]But changing this as you suggested [code]Minimum Level: <input type='text' name=minlevel' /><br />[/code]to this [code]Minimum Level: <input type='text' name='minlevel' /><br />[/code]works. ;D Quote Link to comment https://forums.phpfreaks.com/topic/29481-methodpost-not-working/#findComment-135887 Share on other sites More sharing options...
wildteen88 Posted December 6, 2006 Share Posted December 6, 2006 [quote author=rastus link=topic=117382.msg479493#msg479493 date=1165376543]when using the code you posted I get [code]Parse error: parse error, unexpected T_IF, expecting ',' or ';' in /home/my4x4toy/public_html/*******/staff_cities.php on line 18[/code]this is line 18[code]if(isset($_POST['minlevel']) && isset($_POST['desc']) && isset($_POST['name']))[/code]But changing this as you suggested [code]Minimum Level: <input type='text' name=minlevel' /><br />[/code]to this [code]Minimum Level: <input type='text' name='minlevel' /><br />[/code]works. ;D[/quote]The line 18 error is caused by the line above it. I must of accidentally deleted the semi-colon at the end of line 16. If you add it in it will solve this error. Line 16 should be this:[code] global $db, $ir, $c, $h, $userid[/code]Just add a semi-colon ( ; ) at the end of the line. Quote Link to comment https://forums.phpfreaks.com/topic/29481-methodpost-not-working/#findComment-136300 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.