Page 1 of 1

something wrong?????

Posted: Fri Dec 04, 2009 8:58 am
by Goofan
Hi i got this code
and i dont see whats wrong with it as i use "exact same code" before but after it get messages of undifined variables.......
code:
Infanteries.php

Code: Select all

 
<?php
    // Get the database connector stuff
    include "../login/database.php";
     
    // Build our query
    $sql1 = "SELECT * FROM Units";
     
    // Get the result if there is one
   // DO NOT die() IN PRODUCTION!!!
   if (!$result1 = mysql_query($sql1))
    {
     die('The query<br /><strong>' . $sql1 . '</strong><br />failed:<br />' . mysql_error());
    }
?>
<?php
 
 
  $id =(isset($_GET['saved_id'])) ? (int)$_GET['saved_id'] : false;
    if($id !== false)
 {
    $sql="SELECT * FROM konto WHERE saved_id=$id";      //selecting all from DB "Konto" where saved_id is the same as in the array $id
 }
    else
 {
    echo "NO saved_id!";
 }
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tall. //hämtar all info från tabell
                 
while($row = mysql_fetch_array( $result )) //hämtar info från tabell.
 
{
    
    $pengar = $row['pengar'];
    $my['Swordmen'] = $row['swordmen'];
    $my['Macemen'] = $row['macemen'];
    $my['Pikemen'] = $row['pikemen'];
 
}
 
if(isset($_POST['Swordmen']))
{
        // the Swordmen Buy button was clicked
        $unit = "swordmen";
        $unit2 = "Swordmen";
        $qty = $_POST['qty_Swordmen'];
}
if(isset($_POST['Macemen']))
{
        // the Macemen Buy button was clicked
        $unit = "macemen";
        $unit2 = "Macemen";
        $qty = $_POST['qty_Macemen'];
}  
if(isset($_POST['Pikemen']))
{
        // the Pikeman Buy button was clicked - so you need to use qty_Pikemen
        $unit = "pikemen";
        $unit2 = "Pikemen";
        $qty = $_POST['qty_Pikemen'];
}
if(isset($unit) && is_numeric($qty) && $qty > 0)
{ // if isset($unit) then one of the three buttons was clicked
    
    
 
 
if(isset($_POST['Swordmen']))
{
        // the Swordmen Buy button was clicked
        $sql = "select buyfor from units WHERE number = 1 ";
        $result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
        $buyfor = mysql_result($result,0);
        
}
if(isset($_POST['Macemen']))
{
        // the Macemen Buy button was clicked
        $sql = "select buyfor from units WHERE number = 2";
        $result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
        $buyfor = mysql_result($result,0);
        
}  
if(isset($_POST['Pikemen']))
{
        // the Pikeman Buy button was clicked - so you need to use qty_Pikemen
        $sql = "select buyfor from units WHERE number = 3";
        $result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
        $buyfor = mysql_result($result,0);
        
}
 
$product_price = $buyfor * $qty;
 
if ($pengar >= $product_price)
    {
        $sql="UPDATE konto SET $unit=$unit + $qty WHERE saved_id=$id";//Sätt upp SQL fråga.
        $result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
        
        $sql="UPDATE konto SET pengar=$pengar - $product_price WHERE saved_id=$id";//Sätt upp SQL fråga.
        $result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
        
        echo "You have succesfully bought $qty $unit ";
        header('Refresh: 0; url=infantries_success.php?saved_id='.$row[saved_id]);//Skickas till angiven sida.
        $result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.
    }
else
    {
        echo "You dont afford that many.";
    }
}
 
 
?>
<html>
<head>
<link href="../css/style.css" rel="stylesheet" type="text/css">
<title>Infantrie</title>
</head>
<body bgcolor="#A4A8B0">
    <style type="text/css">     <!--selecting the style type-->
        <!--
        a:link {text-decoration: none; color: black}
        a:visited {text-decoration: none; color: black}
        a:active {text-decoration: none; color: black}
        a:visited {text-decoration: none; color: black}
        .textbox { background-color: #DEB887; }
        .submit { background-color: #DEB887; }
        -->
    </style>        <!--ending the style type-->
    <p>                    
        Är du villig att köpa soldater? Tror du att du är man nog att göra det?
    </p>
 <table border="1">
   <tr>
    <th>Units</th>
        <th>AtkArm</th>
    <th>Buyfor</th>
        <th>Sellfor</th>
    <th>You Own</th>
        <th>Buy</th>
   </tr>
<form action="#" method="post">
 <?php while ($row1 = mysql_fetch_assoc($result1)): ?>
 <tr>
        <th><?php echo $row1['Units'] ;?></th>
    <th><?php echo $row1['AtkHp'] ;?></th>
        <th>$<?php echo $row1['Buyfor'] ;?></th>
    <th>$<?php echo $row1['Sellfor'] ;?></th>
        <th><?php echo $my[$row1['Units']]; ?></th>
    <th>
        <font color="black" valign= "top"><input style='width:45;height:20' type="text" name="qty_<?php echo $row1['Units']?>" size="250" class="textbox" value=""></font>
        <input style='width:45;height:30;font-weight:bold' name="<?php echo $row1['Units']?>" type="submit" class="submit" value="Buy!">
    </th>
   </tr>
 <?php endwhile; ?>
 </form>
 </table>
</html>
 
all began to go wrong when i inputted this line: 104
echo "You have succesfully bought $qty $unit ";
this----> header('Refresh: 0; url=infantries_success.php?saved_id='.$row[saved_id]);//send to page
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tabell.

code for
infantries_success.php

Code: Select all

 
<html>
<head>
<title>menu main (menu)</title>
<script type="text/javascript">
parent.main.location.href = "Infanteries.php?saved_id=<?php echo $_GET['saved_id'] ?>"
parent.OverMain.location.href = "../over_menu_user.php?saved_id=<?php echo $_GET['saved_id'] ?>"
</script>
</head>
</html>
<body>
 
</body>
</html>
 
have i made something wrong "or cant u do like this?"


code for last page:
over_menu_user.php

Code: Select all

 
 <?php
include "login/database.php";       //page to where the database setting is, including this file into the php script of wichh is below it
  $id =(isset($_GET['saved_id'])) ? (int)$_GET['saved_id'] : false;
    if($id !== false) 
 {
    $sql="SELECT * FROM konto WHERE saved_id=$id";      //selecting all from DB "Konto" where saved_id is the same as in the array $id
    
 } 
    else 
 {
    echo "NO saved_id!";
 }
$result = mysql_query($sql) or die(mysql_error());      //choose all from table //fetch all info in the table
 
                 
while($row = mysql_fetch_array( $result ))      //fetching info from file and putting it in $row
{
    $user=$row['user'];     //fetching the "user" information in the tabledata
    $pengar=$row['pengar'];     //fetching the "pengar" information in the tabledata
}
?>
<html>
<body bgcolor="#A4A8B0">
    <style type="text/css">     <!--selecting the style type-->
        <!--
        a:link {text-decoration: none; color: black}
        a:visited {text-decoration: none; color: black}
        a:active {text-decoration: none; color: black}
        a:visited {text-decoration: none; color: black}
        .textbox { background-color: #DEB887; }
        .submit { background-color: #DEB887; }
        -->
    </style>        <!--ending the style type-->
    <table bordercolor="white" border="1" width = "100%" height = "100%" >
    <tr>
    <td>
    Username: <font color="black"><b> <?php echo $user; ?></b> | Money: <b>$<?php echo $pengar; ?></b> | General status: (rank) | Income:
    </td>
    </tr>
    </table>
</body>
</html>
 
errors:
on side inf...
---------------------------------------------------------
Notice: Undefined variable: my in C:\***\***\***\***\***\***\Infanteries.php on line 149 <----- 149 does not exist (in real code //end of code is 149)
---------------------------------------------------------
on side over_main...
---------------------------------------------------------
Notice: Undefined variable: user in C:\***\***\***\***\***\over_main_user.php on line 37
Notice: Undefined variable: pengar in C:\***\***\***\***\***\over_main_user.php on line 37
---------------------------------------------------------
Now just to be sure u understand.
i got so that when "the user" log in he can see the sides. he sees them just fine.
But when i run the script on inf... its getting these errors. remember these only exists after i run the code...

Re: something wrong?????

Posted: Fri Dec 04, 2009 9:28 am
by Goofan
please ask if u dont understand


-Thanks in advance
-Thomas

Re: something wrong?????

Posted: Fri Dec 04, 2009 10:46 am
by Goofan
I´ve made a slide show :D to show u better

Re: something wrong?????

Posted: Fri Dec 04, 2009 11:38 am
by jackpf
I think it may be because you're setting the variables in a while loop that depends on the query returning a result set.

If the query finds no results, the while loop will not be executed, hence your variables won't be set.

Try setting them to null before the query, or checking to see if they have been set before displaying them.

Re: something wrong?????

Posted: Fri Dec 04, 2009 12:34 pm
by Goofan
which ones to be exact would i need to null? got a little problem finding the right one :D


-Thanks for all help
-Thomas

Re: something wrong?????

Posted: Fri Dec 04, 2009 12:40 pm
by Goofan
ok so i found the problem and i fixed it (basicly nulled all of them). But i got a new problem now as these aint showing

Code: Select all

 
<th><?php echo $my[$row1['Units']]; ?></th>
 
anyone got an idé of how to fix it?
Im in need of showing them after the "uppdate" (show of "new site"(same))

Re: something wrong?????

Posted: Fri Dec 04, 2009 5:25 pm
by Goofan
ok so i found out why but cant manage to fix it
i aint passing my saved_id to the next page...
::| problem is here |::
from the next passing page infanties_success.php.
code:

Code: Select all

 
<script type="text/javascript">
parent.main.location.href = "Infanteries.php?saved_id=<?php echo $_GET['saved_id'] ?>" <----- I think the problem is that i aint sending here
parent.OverMain.location.href = "over_main_user.php?saved_id=<?php echo $_GET['saved_id'] ?>"
</script>
 
only problem i dont know how to fix it