will41 Posted February 24, 2012 Share Posted February 24, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/257705-echo-sql-query-to-text-box/ Share on other sites More sharing options...
blacknight Posted February 24, 2012 Share Posted February 24, 2012 $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']; Quote Link to comment https://forums.phpfreaks.com/topic/257705-echo-sql-query-to-text-box/#findComment-1320807 Share on other sites More sharing options...
will41 Posted February 24, 2012 Author Share Posted February 24, 2012 I can echo it out ,but im wanting to echo out to a textbox i changes the code to this but it still is not woking <label for="text">roster count</label> <input type="text" name="text" id="text" value=" <?php echo $row['idcount'];?> " /> Quote Link to comment https://forums.phpfreaks.com/topic/257705-echo-sql-query-to-text-box/#findComment-1320849 Share on other sites More sharing options...
premiso Posted February 24, 2012 Share Posted February 24, 2012 I would alias the column coming from mysql, but since you used fetch_array, just call it by the index: <input type="text" name="text" id="text" value="<?php echo $row[0];?>" /> Should give you the right value. Quote Link to comment https://forums.phpfreaks.com/topic/257705-echo-sql-query-to-text-box/#findComment-1320850 Share on other sites More sharing options...
will41 Posted February 24, 2012 Author Share Posted February 24, 2012 Are you saying put 0 in after $row if so i tried that and it did not work I have inserted a image you can see i can echo out the page but still not to the textbox. Quote Link to comment https://forums.phpfreaks.com/topic/257705-echo-sql-query-to-text-box/#findComment-1320888 Share on other sites More sharing options...
will41 Posted February 25, 2012 Author Share Posted February 25, 2012 when i tried putting the zero in that didn't work. Im not sure what im missing here Quote Link to comment https://forums.phpfreaks.com/topic/257705-echo-sql-query-to-text-box/#findComment-1320990 Share on other sites More sharing options...
Pikachu2000 Posted February 25, 2012 Share Posted February 25, 2012 Post your revised code. Quote Link to comment https://forums.phpfreaks.com/topic/257705-echo-sql-query-to-text-box/#findComment-1321019 Share on other sites More sharing options...
PFMaBiSmAd Posted February 25, 2012 Share Posted February 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257705-echo-sql-query-to-text-box/#findComment-1321020 Share on other sites More sharing options...
will41 Posted February 25, 2012 Author Share Posted February 25, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/257705-echo-sql-query-to-text-box/#findComment-1321115 Share on other sites More sharing options...
PFMaBiSmAd Posted February 25, 2012 Share Posted February 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257705-echo-sql-query-to-text-box/#findComment-1321121 Share on other sites More sharing options...
will41 Posted February 25, 2012 Author Share Posted February 25, 2012 Thanks man that worked like a charm. Quote Link to comment https://forums.phpfreaks.com/topic/257705-echo-sql-query-to-text-box/#findComment-1321129 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.