Jump to content

Echo sql query to text box


will41

Recommended Posts

Hi im new to php. what im trying to do is echo the result out to a textbox and can't seem to figure it out.

I can echo the result to my screen just fine,its when i try to echo it oout to the textbox i cant figure out.

I have added my code below.

Thanks will

 

<?php
//DATABASE CONNECTION//
  include 'include/test.php';
$team = $_POST["Team"];
$COUNT = 'COUNT(id)';
$query = "SELECT COUNT(id)  FROM `teamrosters` WHERE TEAM = '$team'  ";    
$result = mysql_query($query); 

// Print out result 
while($row = mysql_fetch_array($result)) { 
echo 'ROSTER COUNT: '. $row  [$COUNT]; 

}

echo mysql_error() 
?>


  <h1 class="will"><span class="will">Welcome to B410 Team Rosters Page</span></h1>
    <p class="will"><span class="will">Just pick the team from the drop down menu you wan't to see and hit summit</span></p>
    <p>
    </p>
    <form action="formsummit.php" method="post" name="form1" id="form1">
      <label for="Team" class="will">Team</label>
      <select name="Team" id="Team">
        <option value="Auburn">Auburn</option>
        <option value="Texas">Texas</option>
        <option value="OSU">OSU</option>
        <option value="USC">USC</option>
        <option value="UCLA">UCLA</option>
        <option value="Vandy">Vandy</option>
        <option value="Michigan">Michigan</option>
        <option value="Texas AM">Texas AM</option>
        <option value="Texas Tech">Texas Tech</option>
        <option value="Colorado">Colorado</option>
        <option value="Washington">Wasington</option>
        <option value="Iowa ST">Iowa ST</option>
        </select>
      <input type="submit" name="summit" id="summit" value="Submit" />

      <label for="text">avg team speed</label>
      <input type="text" name="text" id="text" value=" <?php echo $row ['COUNT(id)'];?> " />
    </form>






Link to comment
https://forums.phpfreaks.com/topic/257705-echo-sql-query-to-text-box/
Share on other sites

$query = "SELECT COUNT(id)  FROM `teamrosters` WHERE TEAM = '$team'  ";   

should be

$query = "SELECT COUNT(id) as idcount  FROM `teamrosters` WHERE TEAM = '$team'  ";   

 

then you would use $row['idcount']

 

COUNT(id) just counts but assignes it no name

 

then echo 'ROSTER COUNT: '. $row  [$COUNT];

would be

 

echo 'ROSTER COUNT: '. $row['idcount'];

Because you are using $row after the end of the while(){} loop, it will contain a false value (the last thing assigned to it.) If you are only getting one row from your database, don't use a loop or if you do use a loop, you would need to assign the value to a variable in the code inside of the loop so that variable would have the only/last value after the end of the loop.

PFM here is my code. when you said i was losing my value is exactly what i was thinking ,but i didn't know where to where to get the value from.

 

<?php
//DATABASE CONNECTION//
  include 'include/test.php';
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
  
$team = $_POST["Team"];
$COUNT = 'COUNT(id)';
$query = "SELECT COUNT(id)  FROM `teamrosters` WHERE TEAM = '$team'  ";    
$result = mysql_query($query)or die(mysql_error()); 

// Print out result 
while($row = mysql_fetch_array($result))   { 
echo $row['COUNT(id)'] ; 
}
echo mysql_error() 
?>

         <form action="formsummit.php" method="post" name="form1" id="form1">
           <p>
             <label for="Team" class="will">Team</label>
             <select name="Team" id="Team">
               <option value="Auburn">Auburn</option>
               <option value="Texas">Texas</option>
               <option value="OSU">OSU</option>
               <option value="USC">USC</option>
               <option value="UCLA">UCLA</option>
               <option value="Vandy">Vandy</option>
               <option value="Michigan">Michigan</option>
               <option value="Texas AM">Texas AM</option>
               <option value="Texas Tech">Texas Tech</option>
               <option value="Colorado">Colorado</option>
               <option value="Washington">Wasington</option>
               <option value="Iowa ST">Iowa ST</option>
             </select>
             <input type="submit" name="summit" id="summit" value="Submit" />
           </p>
           <p>
             <label for="1">roster count</label>
             <input type="text" name="1" id="1" value="<?php echo $row ?>"/>
           </p>
           <p></p>
    </form>
</body>
</html>






Queries that SELECT a COUNT(), that don't have a GROUP BY term, only return one row. There's no point in using a loop.

 

Change your while(){} code -

while($row = mysql_fetch_array($result))   { 
echo $row['COUNT(id)'] ; 
}

 

To just the following -

 

$row = mysql_fetch_array($result);

 

You would then echo $row['COUNT(id)'] wherever you need the value.

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.