Page 1 of 1

How to make a form to update MySQL with the choice of job

Posted: Sat Mar 28, 2009 11:40 pm
by redfraggle
Hi. :) I am about a week into learning about PHP and MySQL. I did a tutorial the other night through the NetBeans site where we created a CRUD application. I thought I would try to make one myself based on that. The premise is that a user can either log in or create and account. That I have down. Either way they get taken to the dothejob.php page where they can see their current stats and then do a job. The stats would then update with the jobs stats being added to current totals. (I am basing the idea on Mob Wars). So I am working on the do the job page and am stuck with how to proceed with the radio buttons updating the database. I know the query works when I set it to job.id = 1 and stuff.id=1 or 2 or whatever (not on the page but in MySQL itself). Even just a push in the right direction would be awesome. I think I have seen the whole internet this week. :P

I have:

stuff (player stats):
id
user_name
nickname
level
cash
energy
xp

jobs (job stats):
id
job_name
level
cash
energy
xp

What I want to do is when they click the radio and click submit, the database would add the job stats to the players stats.

I am pretty sure this is totally wrong, but at least it shows the page with the form and not a blank screen. lol

Code: Select all

<?php
session_start();
if (array_key_exists("user", $_SESSION)) {
    echo "Hello " . $_SESSION["user"];
}
else {
   header('Location: index.php');
   exit;
}
 
?>
<html>
<head></head>
<body>
        <table border="black">
            <tr>
                <th>User Name</th>
                <th>Nickname</th>
                <th>Level</th>
                <th>Cash</th>
                <th>Energy</th>
                <th>XP</th>
            </tr>
 
            <?php
            require_once("Includes/db.php");
            $playerID = getajobDB::getInstance()->get_player_id_by_name(mysql_real_escape_string($_SESSION["user"]));
            $result = getajobDB::getInstance()->get_player_by_players_id($playerID);
            while($row = mysql_fetch_array($result)) {
        strip_tags($row["user_name"],'<br><p><h1>');
        echo "<tr><td>" . $row["user_name"]."</td>";
        strip_tags($row["nickname"],'<br><p><h1>');
        echo "<td>".$row["nickname"]."</td>";
        strip_tags($row["my_level"],'<br><p><h1>');
        echo "<td>".$row["my_level"]."</td>";
        strip_tags($row["cash"],'<br><p><h1>');
        echo "<td>".$row["cash"]."</td>";
        strip_tags($row["energy"],'<br><p><h1>');
        echo "<td>".$row["energy"]."</td>";
        strip_tags($row["xp"],'<br><p><h1>');
        echo "<td>".$row["xp"]."</td>";
        echo "</tr>\n";
            }
            
//the top part to here works great.
            
            ?>
   </table>
 
 
<br><br>
<?php
 
//the problem child.
 
if (!isset($_POST['jobs'])) {
     echo "You need select a job. Get to work you hippie!";
 } else {
     foreach ($_POST['jobs'] as $selected_job) {
          mysql_query("UPDATE stuff,jobs SET stuff.cash=stuff.cash+jobs.cash , stuff.energy=stuff.energy-jobs.energy, stuff.xp=stuff.xp+jobs.xp WHERE jobs.id='".$_POST["jobs"]."' AND stuff.id=$playerid");
 
         echo "You have done ".$selected_job."!<br>";
     }
 }
 ?>
<form name="Jobs" method="post" action="dothejob.php">
 
<table width="300"  border="0" cellspacing="0" cellpadding="0">
<tr>
  <td><input type="radio" name="jobs[]" value="1"></td>
     <td>Babysitting</td>
   </tr>
   <tr>
     <td><input type="radio" name="jobs[]" value="2"></td>
     <td>Mow the lawn</td>
   </tr>
   <tr>
     <td><input type="radio" name="jobs[]" value="4"></td>
     <td>Take out the trash</td>
   </tr>
   <tr>
     <td><input type="radio" name="jobs[]" value="3"></td>
     <td>Drive mom to work</td>
   </tr>
 </table>
 <p>
   <input type="submit" name="Submit" value="Submit">
 </p>
</form>
The radio form I pulled off of a tutorial here. it works, but the query does not.

I am running the latest Apache, PHP, and MySQL, and using the NetBeans IDE, on dare I say? A windows XP box. :P

Thanks in advance for any advise given, I look at it as a good learning tool. I have been reading stuff all over the place over the last week this is fun. :)

Re: How to make a form to update MySQL with the choice of job

Posted: Sun Mar 29, 2009 2:18 pm
by redfraggle
I finally figured it out. :) Although I know there is a better way to do it. :P Here is the code for the section, any thoughts on improvement would be appreciated.

Code: Select all

<?php
 
//the problem child.
 
if (!isset($_POST['jobs'])) {
     
     echo "You need to select a job. Get to work you hippie!";
 
} else {
 
    foreach ($_POST['jobs'] as $selected_job) {
        
        $_POST['jobs']= $selected_job;
            
            echo "You have completed ".$selected_job."!<br>";
      
      $query = "UPDATE stuff,jobs SET stuff.cash=stuff.cash+jobs.cash , stuff.energy=stuff.energy-jobs.energy, stuff.xp=stuff.xp+jobs.xp WHERE jobs.jobname='$selected_job' AND stuff.user_name='".$_SESSION['user']."'";
 
$error = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
     }
}
 
 ?>
<form name="Jobs" method="post" action="dothejob.php">
 
<table width="300"  border="0" cellspacing="0" cellpadding="0">
 
<tr>
  <td><input type="radio" name="jobs[]" value="babysitting"></td>
     <td>Babysitting</td>
   </tr>
   
   <tr>
     <td><input type="radio" name="jobs[]" value="mowing the lawn"></td>
     <td>Mow the lawn</td>
   </tr>
    
    <tr>
     <td><input type="radio" name="jobs[]" value="driving mom to work"></td>
     <td>Drive mom to work</td>
   </tr>
   
   <tr>
     <td><input type="radio" name="jobs[]" value="taking out the trash"></td>
     <td>Take out the trash</td>
   </tr>
 
 </table>
 <p>
   <input type="submit" name="Submit" value="Submit">
 </p>
</form>