How do I use a variable to fill a form with MySQL record data?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • holy moly
    New Member
    • Apr 2007
    • 7

    How do I use a variable to fill a form with MySQL record data?

    Hello,
    I am trying to make a PHP/HTML update script for MySQL.

    I prompt the user to enter the ID# of the record they wish to update into a form which then uses the passed ID# to select the associated record from the database:

    Code:
    $id = @$_GET['q'] ;
    Code:
    $query = "SELECT FROM Hcffres WHERE residentid = $id";
    mysql_query($query);
    How do I output the data from the query into the form so the user simply alters the field that needs to be updated? I've tried variations of this:

    Code:
    Last Name: </font></td><td><input type="text" name="lastname" value="<?php echo $lastname; ?>"  size="40"</td></tr>
    but atm I get a blank form and when I try to input any data the residentid = 0 not the passed variable

    -- thanks for any help --
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    Originally posted by holy moly
    Hello,
    I am trying to make a PHP/HTML update script for MySQL.

    I prompt the user to enter the ID# of the record they wish to update into a form which then uses the passed ID# to select the associated record from the database:

    Code:
    $id = @$_GET['q'] ;
    Code:
    $query = "SELECT FROM Hcffres WHERE residentid = $id";
    mysql_query($query);
    How do I output the data from the query into the form so the user simply alters the field that needs to be updated? I've tried variations of this:

    Code:
    Last Name: </font></td><td><input type="text" name="lastname" value="<?php echo $lastname; ?>"  size="40"</td></tr>
    but atm I get a blank form and when I try to input any data the residentid = 0 not the passed variable

    -- thanks for any help --

    [PHP]$id = $_GET['q'] ;


    $query = "SELECT * FROM Hcffres WHERE residentid = '$id'";
    mysql_query($qu ery);
    [/PHP]

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      I usually build an array returning PHP function:

      [php]
      function query($qry)
      {
      $res = mysql_query($qr y);
      $ret = array();
      $i = 0;
      while($row = mysql_fetch_arr ay($result, MYSQL_BOTH))
      if($row)
      $ret[$i++] = $row;
      return $ret;
      }
      [/php]

      Comment

      • holy moly
        New Member
        • Apr 2007
        • 7

        #4
        thanks for the replies but the problem remains:
        [php]
        $query = "SELECT * FROM hcffres WHERE residentid = $var";
        mysql_query($qu ery);
        // I need to know what command goes here
        echo"<form action=....
        [/php]
        what is the command I need to output the data returned from the query into the html form so the user can alter the field that needs updating

        here's hoping...

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          Originally posted by holy moly
          what is the command I need to output the data returned from the query into the html form so the user can alter the field that needs updating
          Did you even attempt to look at my last post? I provided a function which will return the entire dataset in an associative array for easily accessing your MySQL record set.

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            Originally posted by holy moly
            thanks for the replies but the problem remains:
            [php]
            $query = "SELECT * FROM hcffres WHERE residentid = $var";
            mysql_query($qu ery);
            // I need to know what command goes here
            echo"<form action=....
            [/php]
            what is the command I need to output the data returned from the query into the html form so the user can alter the field that needs updating

            here's hoping...
            [PHP]
            $query = "SELECT * FROM hcffres WHERE residentid = $var";
            $result = mysql_query($qu ery);
            $row = mysql_fetch_arr ay( $result );
            echo 'other stuffs';
            echo '<input type="text" name="name_goes _here" value="'.$row['column_name_he re'].'"/>';[/PHP]

            Comment

            • holy moly
              New Member
              • Apr 2007
              • 7

              #7
              thanks for the replies, I didn't get it to work but I went round the problem.

              i saw the function, thanks, but I wanted to know how to fix that specific problem.

              of course I have a new problem now with JS form validation
              avaialble at: http://www.thescripts.com/forum/show...20#post2492320 if anyone's interested.

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                Originally posted by holy moly
                ...
                i saw the function, thanks, but I wanted to know how to fix that specific problem.
                ...
                Well, the function I gave you shows exactly how to solve your problem. Your problem was that you were not actually retrieving any of the information after you performed a query.

                Comment

                • dzuse
                  New Member
                  • Apr 2007
                  • 2

                  #9
                  Originally posted by Motoma
                  Well, the function I gave you shows exactly how to solve your problem. Your problem was that you were not actually retrieving any of the information after you performed a query.
                  you can add wxtract function in the end ;)

                  $query = "SELECT * FROM hcffres WHERE residentid = $var";

                  $result = mysql_query($qu ery);

                  $row = mysql_fetch_arr ay( $result );
                  extract($row;
                  echo 'other stuffs';

                  echo '<input type="text" name="name_goes _here" value="$column_ name_here"/>';

                  Comment

                  Working...