Connecting Tech Pros Worldwide Help | Site Map

mysql values to display on PHP form

  #1  
Old June 18th, 2009, 12:53 AM
ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 204
Hi,

Im looking for sample code that will retrieve the values from mysql query. Most of my code has the "while($row = mysql_fetch_assoc($result))" and that will display the values by rows. How about the display are in indent format?


Belowi s the input text box that I wanted to show the values from mysql query. Anybody can show me how not using the while function?
Expand|Select|Wrap|Line Numbers
  1.  
  2. <td>Total English</td>
  3.       <Input type = 'text' Name ='TotalSTSEnglish' value='0' >
  4. <td>Total Spanish</td> 
  5.       <Input type = 'text' Name ='TotalSTSEnglish' value='0' >   
  #2  
Old June 18th, 2009, 04:36 AM
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,439

re: mysql values to display on PHP form


The while() loop will keep fetching new rows until mysql_fetch_assoc() returns FALSE, which means there are no more rows to fetch.
So if you only need to get one row from your mysql query (assuming your query returns only one row) here is the solution.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. mysql_connect("localhost","root","") or die(mysql_error());
  3. mysql_select_db("books");
  4. $result = mysql_query("select * from books "); // You can put your where clause here 
  5. $row = mysql_fetch_assoc($result);
  6. $book_name =  $row['name'];
  7. ?>
  8.  
Now print the variables inside the value attribute on the input box. hope this will help. Thanks
  #3  
Old June 18th, 2009, 06:34 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: mysql values to display on PHP form


you may also look at PDO's fetch*() methods.
  #4  
Old June 23rd, 2009, 12:51 AM
ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 204

re: mysql values to display on PHP form


Thanks ak1dnar it works!
I appreciate the knowledge shared :)
Quote:
Originally Posted by ak1dnar View Post
The while() loop will keep fetching new rows until mysql_fetch_assoc() returns FALSE, which means there are no more rows to fetch.
So if you only need to get one row from your mysql query (assuming your query returns only one row) here is the solution.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. mysql_connect("localhost","root","") or die(mysql_error());
  3. mysql_select_db("books");
  4. $result = mysql_query("select * from books "); // You can put your where clause here 
  5. $row = mysql_fetch_assoc($result);
  6. $book_name =  $row['name'];
  7. ?>
  8.  
Now print the variables inside the value attribute on the input box. hope this will help. Thanks
  #5  
Old June 24th, 2009, 01:11 AM
ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 204

re: mysql values to display on PHP form


A follow-up question on this topic... now I have a check box but Im having problem to pull the information. Inside the talbe the field value = 1, how come it wont display?

Expand|Select|Wrap|Line Numbers
  1. <table width='80%' border='0' summary=''>";
  2.     $result = mysql_query("select Total from recap where Cal_ID={$id} and category='Target Focus'  ");
  3.     $row = mysql_fetch_assoc($result);
  4.     $Total =  $row['Total'];
  5.     echo "<td><input type = 'checkbox' name ='Community' value='$row[Total]'/>Disabled Community </tr></td>";
  6.     echo "<tr>";                            
  7. echo "</table>
  8.  
  #6  
Old June 25th, 2009, 07:57 AM
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,439

re: mysql values to display on PHP form


check box values doesn't appear as the HTML output , did you check the generated html source from the browser view source option ?
  #7  
Old June 25th, 2009, 05:23 PM
ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 204

re: mysql values to display on PHP form


I check my sql and the value is 1. And yet still no check apeared.


PHP
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("select Total from tblevent_recap where Cal_ID={$id} and category='Target Focus' and Name ='Disabled Community' ");
  2. $row= mysql_fetch_assoc($result);
  3. if(mysql_num_rows($result)!=0) {
  4. echo "<td><input type = 'checkbox' name ='DisabledCommunity' value='checked'>Disabled Community </tr></td>";
  5. echo "<tr>";  }
  6. else 
  7. echo "<td><input type = 'checkbox' name ='DisabledCommunity' value=''>Disabled Community </tr></td>";
  8. echo "<tr>";
  9. }
  10.  
  11.  
I veiwed the HTML
Expand|Select|Wrap|Line Numbers
  1. ><input type = 'checkbox' name ='DisabledCommunity' value='checked'>Disabled Community </tr></td><tr><td>
  #8  
Old June 25th, 2009, 06:53 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,858
Provided Answers: 9

re: mysql values to display on PHP form


The correct attribute is 'checked'.

Expand|Select|Wrap|Line Numbers
  1. <input type='checkbox' checked='checked' />
  2.  
  #9  
Old June 26th, 2009, 04:57 AM
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,439

re: mysql values to display on PHP form


I think you may better to follow these tutorials on HTML form markup.
http://www.w3schools.com/html/html_forms.asp
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert List menu values to Mysql dongletran06 answers 7 November 7th, 2008 10:52 PM
Send Dynamic form Values to PHP ak1dnar answers 18 March 8th, 2007 12:31 PM
php extensions and windows... specifically extension_dir Chris Paul answers 3 December 5th, 2005 12:45 AM
Get Method Works but not Post Method on PHP 5.0.4 pages mickbw answers 4 July 17th, 2005 01:55 PM