mysql values to display on PHP form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    #1

    mysql values to display on PHP form

    Hi,

    Im looking for sample code that will retrieve the values from mysql query. Most of my code has the "while($row = mysql_fetch_ass oc($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?
    Code:
    	
    <td>Total English</td>
          <Input type = 'text' Name ='TotalSTSEnglish' value='0' >
    <td>Total Spanish</td> 
          <Input type = 'text' Name ='TotalSTSEnglish' value='0' >
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    The while() loop will keep fetching new rows until mysql_fetch_ass oc() 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.
    Code:
    <?php
    mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("books");
    $result = mysql_query("select * from books "); // You can put your where clause here 
    $row = mysql_fetch_assoc($result);
    $book_name =  $row['name'];
    ?>
    Now print the variables inside the value attribute on the input box. hope this will help. Thanks

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      you may also look at PDO's fetch*() methods.

      Comment

      • ddtpmyra
        Contributor
        • Jun 2008
        • 333

        #4
        Thanks ak1dnar it works!
        I appreciate the knowledge shared :)
        Originally posted by ak1dnar
        The while() loop will keep fetching new rows until mysql_fetch_ass oc() 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.
        Code:
        <?php
        mysql_connect("localhost","root","") or die(mysql_error());
        mysql_select_db("books");
        $result = mysql_query("select * from books "); // You can put your where clause here 
        $row = mysql_fetch_assoc($result);
        $book_name =  $row['name'];
        ?>
        Now print the variables inside the value attribute on the input box. hope this will help. Thanks

        Comment

        • ddtpmyra
          Contributor
          • Jun 2008
          • 333

          #5
          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?

          Code:
          <table width='80%' border='0' summary=''>";
          	$result = mysql_query("select Total from recap where Cal_ID={$id} and category='Target Focus'  ");
          	$row = mysql_fetch_assoc($result);
          	$Total =  $row['Total'];
          	echo "<td><input type = 'checkbox' name ='Community' value='$row[Total]'/>Disabled Community </tr></td>";
          	echo "<tr>";							
          echo "</table>

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            check box values doesn't appear as the HTML output , did you check the generated html source from the browser view source option ?

            Comment

            • ddtpmyra
              Contributor
              • Jun 2008
              • 333

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


              PHP
              Code:
              $result = mysql_query("select Total from tblevent_recap where Cal_ID={$id} and category='Target Focus' and Name ='Disabled Community' ");
              $row= mysql_fetch_assoc($result);
              if(mysql_num_rows($result)!=0) {
              echo "<td><input type = 'checkbox' name ='DisabledCommunity' value='checked'>Disabled Community </tr></td>";
              echo "<tr>";  }
              else 
              { 
              echo "<td><input type = 'checkbox' name ='DisabledCommunity' value=''>Disabled Community </tr></td>";
              echo "<tr>";
              }
              I veiwed the HTML
              Code:
              ><input type = 'checkbox' name ='DisabledCommunity' value='checked'>Disabled Community </tr></td><tr><td>

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                The correct attribute is 'checked'.

                Code:
                <input type='checkbox' checked='checked' />

                Comment

                • ak1dnar
                  Recognized Expert Top Contributor
                  • Jan 2007
                  • 1584

                  #9
                  I think you may better to follow these tutorials on HTML form markup.
                  An HTML form is used to collect user input. The user input is most often sent to a server for processing.

                  Comment

                  Working...