Jump to content

SQL Not update with forms!


lmninfo

Recommended Posts

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

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)?

$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.

$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

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.