lmninfo Posted March 27, 2009 Share Posted March 27, 2009 I'm having problems with the following scripts. The labor.php script is a form that changes the players peasants to labors, and the peasant2labor.php script handles the info from the labor.php and changes the info in the SQL database. The player begins with the following: Peasants: 50 Labors: 50 Foood: 100 labor.php <?php //killmonster amin index include 'connect.php'; session_start(); ?> <?php if (isset($_SESSION['player'])) { $player=$_SESSION['player']; $userstats="SELECT * from km_users where playername='$player'"; $userstats2=mysql_query($userstats) or die("Could not get user stats"); $userstats3=mysql_fetch_array($userstats2); if($userstats3[food]<5) { print "You cannot train labor at this time. <A href='index.php'>Go back to main</a>."; } else if($userstats3[peasants]<1) { print "You cannot train labor at this time. <A href='index.php'>Go back to main</a>."; } else if($userstats3[food]>=5) { print "Labor<br>"; print "<br>"; print "Food: 5<br>"; print "Research: 1<br>"; print "<br>"; print "Cost:<br>"; print "Food: 10<br>"; print "Labor: 1<br>"; print "<form action='peasant2labor.php?ID=$user3stats[iD]' method='post'>"; print "<input type='text' name='peasant2labor' size='5'><br>"; print "<input type='submit' name='submit' value='Change'></form>"; } } Code to peasant2labor.php <?php //killmonster amin index include 'connect.php'; session_start(); ?> <?php if (isset($_SESSION['player'])) { $player=$_SESSION['player']; $userstats="SELECT * from km_users where playername='$player'"; $userstats2=mysql_query($userstats) or die("Could not get user stats"); $userstats3=mysql_fetch_array($userstats2); $update="update km_users set peasants=peasants-'$peasant2labor',labor=labor+'$peasant2labor',food=food-food where ID='$userstats3[iD]'"; mysql_query($update) or die("Could not get user stats: ".mysql_error()); print "Trained Labor"; print "<br>"; print "Labor: $userstats3[labor]"; print "<br>"; print "Food: $userstats3[food]"; print "<br>"; print "Peasants: $userstats3[peasants]"; print "<br>"; print "<tr class='mainrow'><td><A href='index.php'>Back</a></td></tr>"; } When you post the information into the labor.php file and submit, then the page transfers to the peasant2labor, the information isn't updated! Could someone please take a look and tell me what is going wrong there is NO error message display .. Thanks Link to comment https://forums.phpfreaks.com/topic/151384-sql-not-update-with-forms/ Share on other sites More sharing options...
Mark Baker Posted March 27, 2009 Share Posted March 27, 2009 One possibility is that you're reading the currently stored data, then trying to update the database, but still displaying the current data rather than the updated values. a second is your update statement: $update="update km_users set peasants=peasants-'$peasant2labor',labor=labor+'$peasant2labor',food=food-food where ID='$userstats3[iD]'"; where is $peasant2labor coming from? why are you quoting it? why bother saying food = food-food when you could simply say food = 0? Why are you not using $userstats3['ID'] (ID in quotes)? Link to comment https://forums.phpfreaks.com/topic/151384-sql-not-update-with-forms/#findComment-795146 Share on other sites More sharing options...
lmninfo Posted March 27, 2009 Author Share Posted March 27, 2009 $pesants2labor comes from the labor.php in the following print "<form action='peasant2labor.php?ID=$user3stats[iD]' method='post'>"; print "<input type='text' name='peasant2labor' size='5'><br>"; print "<input type='submit' name='submit' value='Change'></form>"; } } The $pesant2labor should be the value the player wants to change the number of peasants to labor. Link to comment https://forums.phpfreaks.com/topic/151384-sql-not-update-with-forms/#findComment-795220 Share on other sites More sharing options...
Mark Baker Posted March 27, 2009 Share Posted March 27, 2009 $pesants2labor comes from the labor.php in the following The $pesant2labor should be the value the player wants to change the number of peasants to labor. Ah right, because I see nothing in the code you've listed for peasant2labor.php that actually reads anything from the form data Link to comment https://forums.phpfreaks.com/topic/151384-sql-not-update-with-forms/#findComment-795249 Share on other sites More sharing options...
apw Posted March 27, 2009 Share Posted March 27, 2009 Then what would be the easies, most effective way to fix the problem using the info in the labor.php and peasants2labor.php Link to comment https://forums.phpfreaks.com/topic/151384-sql-not-update-with-forms/#findComment-795353 Share on other sites More sharing options...
Mark Baker Posted March 27, 2009 Share Posted March 27, 2009 Something like $peasant2labor = $_POST['peasant2labor']; plus some reading up on the myriad of postings here about how to handle form data, and how to escape it to ensure that nobody tries SQL injection on your database Link to comment https://forums.phpfreaks.com/topic/151384-sql-not-update-with-forms/#findComment-795370 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.