Results 1 to 3 of 3

Thread: Populate a form using a database.

  1. #1
    Join Date
    Jul 2011
    Location
    Lockport, Illinois
    Posts
    35
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Question Populate a form using a database.

    Ello All,

    I am trying to write an app that acts as a booking system for my dj company, i have a form for new events that inserts the data into a mySQL database. then i have a table that lists all the events on a different page. in that table i have an 'Edit Event' button next to each entry. what i need to do is when i click that button, it will load a form identical to the 'new event' form but populated with the values for that event.

    to populate the form i figured id use a script like this:

    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 
    '<input type="text" name="client_name" value=".$row['cli-name']">';
      
    ///Etc, Etc, Etc.
      
    echo '</form>';
      }

    mysql_close($con);
    ?>
    what i dont know is 2 things, how to select the row using that button, and how to set up a select as i have many values to choose from in the select, but how do i make the one in the database the selected one?

    Thanks in Advance,
    Your help is much appreciated.

    -JL Griffin

  2. #2
    Join Date
    Jul 2011
    Location
    Lockport, Illinois
    Posts
    35
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default

    I should also add that i am open to a differant way of doing this. If you know a better or more efficient way, please let me know.

    Thanks JL Griffin

  3. #3
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    are u using a form where the button is to edit?

    if u are, u may want to add a hidden field in ur form, which will hold the id of each event eg.
    Code:
    <input type="hidden" name="id" value="'.$row['id'].'"/>
    im guessing that once u press the submit button, the form is submitted to the code u posed, so u will need to catch the id from the posted form like so:
    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 where id=".$_POST['id'].""); 

    while(
    $row mysql_fetch_array($result)) 
      { 
      echo 
    '<form action="">'
      echo 
    '<input type="text" name="client_name" value="'.$row['cli-name'].'">'
      
    ///Etc, Etc, Etc. 
      
    echo '</form>'
      } 

    mysql_close($con); 
    ?>
    and hopefully that will display all the info on the selected event
    Last edited by liamallan; 07-21-2011 at 11:09 PM.

  4. The Following User Says Thank You to liamallan For This Useful Post:

    griffinwebnet (07-23-2011)

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
  •