More PHP help....

Talk about any languages right here. Share and discuss source, but don't expect your homework to be done for you.

More PHP help....

Postby penguinluvinman » Wed Feb 02, 2011 4:41 pm

Another question.....I'm getting into the actual functional part now. Right now I'm trying to allow the user to buy new soldiers. I'm just using a form that is labeled with a name out of mysql query. First am I allowed to do that (shown below) or is there a better way to make this work?
And I'm just having problems getting to the function apparently. I've tried multiple different things from what I have looked up, but it is just not getting into the function. I've tested by just a simple echo that should display after it is submitted, but it never gets to it. The function is on the same page as the form
The code for the form, and for what should happen when it is submitted is below.

I learned a good bit of PHP, but been out of practice for a while due to school. I've also been learning C++ and Java, so I think some of it is kind of melting together.

Code: Select all
if (isset($_POST['trainUnits'])){
      while($units = mysql_fetch_array($resultUnits)){
          $queryBuy="UPDATE army SET units[name]=units[name] + " . $_POST[unit] . "WHERE id=$_SESSION[user_id]";
          mysql_query($queryBuy)or die(mysql_error("*Query failed. Units were not purchased."));
      }
   //after making purchase, get users UPDATED army
   $queryArmy="SELECT * FROM army WHERE id=$_SESSION[user_id]";
   $resultArmy=mysql_query($queryArmy);
   $army=mysql_fetch_array($resultArmy);
     echo 'You successfully purchased your units.';
}


Code: Select all
        echo '<tr><td>Unit:</td><td>You have:</td><td>Cost</td><td>Upkeep(food)</td><td>Quantity:</td>';
        echo '<form action="" method="post">';
        while($units = mysql_fetch_array($resultUnits)){
         echo '<tr><td>' . ucfirst($units[name]) . '</td><td>' . $army[$units[name]] . '</td><td>' . $units[cost] . '</td><td>' . $units[food] . '</td><td><input type="text" size="3" name=" . $units[name] . "></td></tr>';
        }
        echo '</tr>';
        echo '<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>';
        echo '<input type="submit" value="Train" id="trainUnits" name="trainUnits">';
        echo '</form>';


Thanks!
penguinluvinman
Apprentice
 
Posts: 34
Joined: Sat Dec 15, 2007 5:15 pm
Location: Hicktown, AL

Re: More PHP help....

Postby Cool_Fire » Thu Feb 03, 2011 6:13 pm

First thing I noticed is that your SQL query is probably going to fail. It needs a space before WHERE.
If we're breaking the rules, then how come you can't catch us? You can't find us? I know why. Cause, it's ... MAGIC!

Hackerthreads chat, where the party is going 24/7.
User avatar
Cool_Fire
Not a sandwich
 
Posts: 1530
Joined: Fri May 09, 2003 1:20 pm
Location: 41 6d 73 74 65 72 64 61 6d

Re: More PHP help....

Postby penguinluvinman » Fri Feb 04, 2011 2:37 pm

Thanks for the tip. I haven't gotten that far yet because I can't get into the function
penguinluvinman
Apprentice
 
Posts: 34
Joined: Sat Dec 15, 2007 5:15 pm
Location: Hicktown, AL

Re: More PHP help....

Postby wilsondavid » Fri Dec 16, 2011 1:07 pm

Database Connectivity Code :

<?php

$link=mysql_connect("Your Server Name"," User Name"," Password");

mysql_select_db("sample",$link);

$result=mysql_query("SELECT * FROM EMPL0YEE",$link);

while($row=mysql_fetch_row($result))
{
print("$row[0] : $row[1] : $row[2]");
}

?>
wilsondavid
n00b
 
Posts: 5
Joined: Fri Dec 16, 2011 12:59 pm

Re: More PHP help....

Postby Cool_Fire » Thu Dec 22, 2011 4:52 am

wilsondavid wrote:Database Connectivity Code :
Code: Select all
<?php
$link=mysql_connect("Your Server Name"," User Name"," Password");

mysql_select_db("sample",$link);

$result=mysql_query("SELECT * FROM EMPL0YEE",$link);

while($row=mysql_fetch_row($result))
{
print("$row[0]  :   $row[1]   :  $row[2]");
}

?>

What is the problem with this code?
(Also, use the php mysqli library in favor of the mysql one, it's the newer version.)
If we're breaking the rules, then how come you can't catch us? You can't find us? I know why. Cause, it's ... MAGIC!

Hackerthreads chat, where the party is going 24/7.
User avatar
Cool_Fire
Not a sandwich
 
Posts: 1530
Joined: Fri May 09, 2003 1:20 pm
Location: 41 6d 73 74 65 72 64 61 6d


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests