Advanced Search

Results 1 to 8 of 8

Thread: How to populate php/html form with MySQL data

  1. #1
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to populate php/html form with MySQL data

    I am learning PHP/MySQL on my own. I've got a MySQL database and a few php/html forms that work fine. What I want to do, but haven't been able to figure out, is to display a form so I can enter a key value to retrieve a record from the database and display that data in the form. I then want to be able to modify 1 or more fields and then update the record in the database. I think I can figure out how to do the update, but I can't figure out how to populate the form with the selected record's data. Anyone have any examples of how to do this? I'd appreciate the help.

  2. #2
    Join Date
    Apr 2009
    Location
    Mac OSX
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    This is the most simplest i know..

    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","user","pass");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("my_db"$con);

    $result mysql_query("SELECT * FROM field");

    while(
    $row mysql_fetch_array($result))
      {
      echo 
    '<form action="">';
      echo 
    '<select name="nameofname :D">';
      echo 
    '<option value=". $row['nameoffield'] .">". $row['nameoffield']. "</option>';
      echo 
    '</form>';
      }

    mysql_close($con);
    ?>

  3. #3
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    Hmmm.. What you mean is that you display the data from the database in a form manner? If that so, then try something like this:

    Code:
    <?php
    
      include('dbconnect.php');
    
      $data = "select * from table_name where ID = 100";
    
      $query = mysql_query($data);
      
      $data2 = mysql_fetch_array($query);
      
      
    ?>
    
    <html>
    <head>
          <title></title>
     </head>
    
    <body>
    
    // form to display record from database
    <form>
      Name: <input type="text" value="<?php echo $data2[name]?>"/> </br>
      age: <input type="text" value="<?php echo $data2[age]?>"/> </br>
      hobby: <input type="text" value="<?php echo $data2[hobby]?>"/>
    </form>
    
    </body>
    
    </html>

  4. #4
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    This is very simple to do with 3 separate scripts:

    The first 1 here is called Form.htm and asks for what record to find:

    Form.htm
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
          <title></title>
     </head>
    
    <body>
    
    <!-- form to get key detail of record in database -->
    <form name="form" method="POST" action="form1.php">
      Keyfield <input type="text" name="search"> <br><br>
      <input type="submit"  value="submit">
    </form>
    
    </body>
    
    </html>
    The second script is called Form1.php, it is called by Form.htm, and displays the relevant record and allows changes to be made.

    Form1.php
    PHP Code:
    <?php

    $connection 
    mysql_connect('XXXXX','XXXXXXX','XXXXXXX') or die ("Couldn't connect to server."); 
    $db mysql_select_db('XXXXXXXX'$connection) or die ("Couldn't select database."); 

    $search=$_POST['search'];

    $data 'SELECT * FROM `table_name` WHERE `ID` = "'.$search.'"';
      
    $query mysql_query($data) or die("Couldn't execute query. "mysql_error());
      
    $data2 mysql_fetch_array($query);
      
      
    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
          <title></title>
     </head>

    <body>

    <!-- form to display record from database -->
    <form name="form" method="POST" action="form2.php">
      Name: <input type="text" name="namefield" value="<?php echo $data2[name]?>"/> <br>
      age: <input type="text" name="agefield" value="<?php echo $data2[age]?>"/> <br>
      hobby: <input type="text" name="hobbyfield" value="<?php echo $data2[hobby]?>"/><br><br>
        <input type="hidden" name="keyfield" value="<?php echo $search?>">  
      <input type="submit"  value="submit">
    </form>

    </body>

    </html>
    The third script is called Form2.php, it is called by Form1.php, it updates the record and redisplays the entered data.

    Form2.php

    PHP Code:
    <?php

    $connection 
    mysql_connect('XXXXX','XXXXXXX','XXXXXXX') or die ("Couldn't connect to server."); 
    $db mysql_select_db('XXXXXXXX'$connection) or die ("Couldn't select database."); 

    $Key=$_POST['keyfield'];
    $Name=$_POST['namefield'];
    $Age=$_POST['agefield'];
    $Hobby=$_POST['hobbyfield'];

    $data "UPDATE `table_name` SET name='$Name', age='$Age', hobby='Hobby' WHERE ID=".'"'.$Key.'"';
      
    $query mysql_query($data) or die("Couldn't execute query. "mysql_error());
      
    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
          <title></title>
     </head>

    <body>

    <!--  display the changed record from database -->
      Name: <?php echo $Name?><br>
      Age: <?php echo $Age?> <br>
      Hobby: <?php echo $Hobby?><br><br>

    </body>

    </html>
    I haven't tested this code but it is hopefully ok and will give you an idea of how you could do what you have requested.

    I have used a hidden input field to pass the "key" field between Form1.php and Form2.php because sometimes this may not a field you want to alter, just a field you want to search by, eg an ID number
    Last edited by forum_amnesiac; 06-14-2009 at 01:59 PM.

  5. #5
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    Ya, that was it, ehehhe... hey forum_amnesiac, why do you use $_POST['value'] instead of $_POST['search'] in Form1.php?

  6. #6
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    typo, as I said I hadn't tested it.

    I'll correct it now

  7. #7
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    ahw.. I thought it was another knowledge to earn.. haahahhaha

  8. #8
    Join Date
    Jul 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question populate data from mysql in the same form

    hi all,
    I want to do the similar operation as described above, but in a different manner.
    My form comprises of 1000's html text boxes. User will enter 2 or 3 fields and then on a button/submit a php script will find thr record in the database using these fields entered by the user.
    Record reterived by the user will populate all the other fields in the form.
    I hope u understand what I mean? I do not want to jump to any other page.

    Moreover, my form already have a submit button which saves the data in mysql. Do I need to create a different form for search and give the search textboxes there only. Or I can use the same form.

    Please suggest. I am struck for two days now because of this.

    Thanks in advance.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •