Getting buttons and textboxes within loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Getting buttons and textboxes within loop

Post by iankent »

Goofan wrote:look tha the first "without get" querry and then the second "with get"
what? lol. you've lost me again!

$my is only going to be set if $sql returns a result ($result). if you're getting the error that $my isn't defined, the query for whatever reason definately isn't returning any results.

I don't know why you've used a while() loop on line 31 because there'll only ever be 0 or 1 results to that query. But regardless, $my is set if any result is returned, $my isn't set if the result is empty

right, whichever program you use to create/edit/view your database (the one you've given screenshots of) should somehow let you run SQL queries against the database directly. after line 20 (put it on the line after $sql is set), add this:

Code: Select all

echo "<p>$sql</p>";
save and run your PHP file and it should output an SQL query to your page. Copy and paste that into your database program above to execute the SQL (if you say what program it is I might be able to give you more info on how) - it looks like Access? If so, create a new Query in your database, click the design mode button (usually top-left) and choose SQL from the drop down list, delete everything in there and paste the SQL from the page. Run that query and see what you get.

update:
if it displays a 0 when you used that code then your query is returning nothing, so I assume the saved_id your adding to the query is incorrect
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

i use navicat lite as database program =)
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

if u look into my code i got these to querries...

first one!

Code: Select all

 
<?php
    // Get the database connector stuff
    include "../login/database.php";
     
    // Build our query
    $sql1 = 'SELECT * FROM `infantries`';
     
    // 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());
    }
 ?>
 
Second one!

Code: Select all

 
<?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
 echo "Results: ".mysql_num_rows($result); //to see if u get any 
                 
while($row = mysql_fetch_array( $result )) //hämtar info från tabell.
 
{
    $my['Swordmen'] = $row['swordmen'];
    $my['Macemen'] = $row['macemen'];
    $my['Pikemen'] = $row['pikemen'];
}
if(isset($_POST['Swordmen']))
{
        // the Swordmen Buy button was clicked
        $unit = "swordmen";
        $qty = $_POST['qty_swordmen'];
}
if(isset($_POST['Macemen']))
{
        // the Macemen Buy button was clicked
        $unit = "macemen";
        $qty = $_POST['qty_macemen'];
}   
if(isset($_POST['Pikeman']))
{
        // the Pikeman Buy button was clicked - so you need to use qty_Pikemen
        $unit = "pikeman";
        $qty = $_POST['qty_pikemen'];
}
if(isset($unit) && is_numeric($qty) && $qty > 0) { // if isset($unit) then one of the three buttons was clicked
    $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.
}
?>
 
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Getting buttons and textboxes within loop

Post by iankent »

Have a look online or in the help file to see if you can run SQL queries directly.

Either way, we've established that mysql_query is returning 0 results, which is why $my isn't getting any values. Check the query to make sure its right (i.e., correct table name, correct column names etc) and try echoing out mysql_error() anyway just to make sure its empty.

this line must be wrong somehow, though I really can't see where:
$sql="SELECT * FROM konto WHERE saved_id=$id";
Unless you're not actually passing your script any id when you run it? You're getting the value from $_GET['saved_id'], but are you calling the script as yourfile.php?saved_id=1 or whatever?
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

ok saved id is 0 somehow i dont know how becouse before " i started to modify it were working and the saved_id = 2... ill look into it... if u know something plz post =)
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

well it seems like this is the code thats wrong
this is from the page im sending from... so i can GET saved_id=2
see anything incorrect?

Code: Select all

 
 <a href="sidor/Infanteries.php?saved_id='.$row[saved_id]" target="main">Infanteries</a><br>        <!-- link to go to page Infanteries -->
 
then i get it by using this code:

Code: Select all

 
  $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
    echo "<p>$sql</p>";
 } 
    else 
 {
    echo "NO saved_id!";
 }
 
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Getting buttons and textboxes within loop

Post by iankent »

Is that inside PHP (i.e. using echo) or outside as plain HTML?

if its outside PHP then you're not actually passing a value, i.e. it should be <?=$row['saved_id']?>
if its inside PHP then you've got your quotes wrong, as there should be a .' after $row[saved_id] (which should actually be $row['saved_id'], e.g.
<a href="sidor/Infanteries.php?saved_id='.$row[saved_id].'" target="main">Infanteries</a><br>
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

ok so it was outside php... so i changed it however it still dont work...

i trieed to "use ure code in these diffrent areas:

Code: Select all

 
<a href="sidor/Infanteries.php?saved_id=<?php=$row['saved_id']?>" target="main">Infanteries</a><br>     <!-- link to go to page Infanteries -->
 

Code: Select all

 
<a href="sidor/Infanteries.php<?php=$row['saved_id']?>" target="main">Infanteries</a><br>       <!-- link to go to page Infanteries -->
 

Code: Select all

 
<a href="sidor/Infanteries.php?<?php=$row['saved_id']?>" target="main">Infanteries</a><br>      <!-- link to go to page Infanteries -->
 
none of them seemed to work as it still returned 0
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

and i know the sending page got the saved_id to be set as 2 however its sent within php coding...
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

got a huge diffrent problem now though....
this is what is shown on the page!

and i dont even know what it mean...
Forbidden

You don't have permission to access /www/Projektarbete/sidor/Infanteries.php< on this server.
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Getting buttons and textboxes within loop

Post by iankent »

For some reason you don't have permission on that file - could be it doesn't exist or permissions are wrong.

The saved_id needs to be passed around your script so you can run the right queries. You're probably better starting a session and setting the saved_id once (if its even needed at all, why not just use the user id?), and get it from the session every time - that way you don't need to worry about including it in URLs etc. to know which table row you're using.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

i dont believe in sessions so im doing it "a other way"
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

ok now i got the saved_id to work :D and i get this error message:

NO saved_id!
Notice: Undefined variable: sql in C:\Program Files\wamp\www\www\Projektarbete\sidor\Infanteries.php on line 31
Query was empty

code:

Code: Select all

 
<?php
    // Get the database connector stuff
    include "../login/database.php";
     
    // Build our query
    $sql1 = 'SELECT * FROM `infantries`';
     
    // 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
    echo "<p>$sql</p>";
 } 
    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
 echo "Results: ".mysql_num_rows($result); //to see if u get any 
                 
while($row = mysql_fetch_array( $result )) //hämtar info från tabell.
 
{
    $my['Swordmen'] = $row['swordmen'];
    $my['Macemen'] = $row['macemen'];
    $my['Pikemen'] = $row['pikemen'];
}
if(isset($_POST['Swordmen']))
{
        // the Swordmen Buy button was clicked
        $unit = "swordmen";
        $qty = $_POST['qty_swordmen'];
}
if(isset($_POST['Macemen']))
{
        // the Macemen Buy button was clicked
        $unit = "macemen";
        $qty = $_POST['qty_macemen'];
}   
if(isset($_POST['Pikeman']))
{
        // the Pikeman Buy button was clicked - so you need to use qty_Pikemen
        $unit = "pikeman";
        $qty = $_POST['qty_pikemen'];
}
if(isset($unit) && is_numeric($qty) && $qty > 0) 
{ // if isset($unit) then one of the three buttons was clicked
    $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.
}
?>
<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>
 <?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="<?php echo $my[$row1['Units']]; ?>" size="250" class="textbox" value=""></font>
        <input style='width:45;height:30;font-weight:bold' name="<?php echo $my[$row1['Units']]; ?>" type="submit" class="submit" value="Buy!">
    </th>
   </tr>
 <?php endwhile; ?>
 </table>
</html>
 
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Getting buttons and textboxes within loop

Post by iankent »

If you're getting that error then the saved_id still isn't being passed to the script correctly. Because line 19 is returning false, $sql isn't being set at line 22. When you access the page, does the URL in your address bar show ?saved_id=n where n is a number? If not, the link isn't working.

you should probably add exit(); after line 27 - at the moment your script continues even if there's no saved_id!
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: Getting buttons and textboxes within loop

Post by Goofan »

ok, now i get:
SELECT * FROM konto WHERE saved_id=0
so i guess it still aint passing the saved id...
Post Reply