Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Populating form with record from MySQL db

Options
  • 01-05-2008 8:56pm
    #1
    Closed Accounts Posts: 31


    Hi hope someone can help me.

    I'm creating a form which allows a use to update a record in an SQL database. I want to populate the form with the current data in that record. I've got it working fine for the input boxes and text areas but don't know how to do it for the drop down box (first in the form).

    Any help is greatly appreciated!

    CODE........

    $query1 = "SELECT * FROM articles where id=".$rowid."";

    $getvals = mysql_query($query1);
    while($row = mysql_fetch_array($getvals, MYSQL_ASSOC)){
    ?>
    <form method="post">
    <table width="400" border="0" cellspacing="1" cellpadding="2">
    <tr>
    <td width="100">Section:</td>
    <td>
    <select name="a_section" id="a_section">
    <option value="gw">Global Warming</option>
    <option value="ol">Ozone Layer</option>
    <option value="ae">Alternative Energy</option>
    </select>
    </td>
    </tr>

    <tr>
    <td width="100">Title:</td>
    <td><input name="a_title" type="text" id="a_title" value= "<?php echo htmlentities($row); ?>"> </td>
    </tr>

    <tr>
    <td width="100">Synopsis:</td>
    <td><TEXTAREA name="a_synopsis" type="text" id="a_synopsis" rows="6" cols="32" value=""><?php echo htmlentities($row);?></TEXTAREA></td>
    </tr>

    <tr>
    <td width="100">Article Text:</td>
    <td><TEXTAREA name="a_text" type="text" id="a_text" rows="6" cols="32" value=""><?php echo htmlentities($row);?></TEXTAREA></td>
    </tr>

    <tr>
    <td width="100">Image:</td>
    <td><input name="a_image" type="text" id="a_image" value="<?php echo $row;?>"></td>
    </tr>

    <tr>
    <td width="100">Thumbnail Image:</td>
    <td><input name="a_thumb_image" type="text" id="a_thumb_image" value="<?php echo $row;?>"></td>
    </tr>

    <tr>
    <td width="100">Author Name:</td>
    <td><input name="a_author" type="text" id="a_author" value="<?php echo $row;?>"></td>
    </tr>


    <tr>
    <td width="100"> </td>
    <td><input name="add" type="submit" id="add" value="Add Article"></td>
    </tr>
    </table>
    </form>
    <?php
    }


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Firstly, you shouldn't be using the while loop, if you have more than one row returned from the database for any reason, the entire form would be printed out for each row returned. So instead of
    [php]while($row = mysql_fetch_array($getvals, MYSQL_ASSOC)){[/php]
    just use
    [php]$row = mysql_fetch_array($getvals, MYSQL_ASSOC);[/php]

    You can still refer to the variables in the same manner, but there's no chance of the form being repeated.

    Now on to the select box, you simply need to check the value of a_section for each option, and if it matches echo the html to set that one as the selected option. Like so:

    [php]
    <select name="a_section" id="a_section">
    <option value="gw" <? if $row == "gw" { echo "SELECTED"; } ?>>Global Warming</option>
    <option value="ol" <? if $row == "ol" { echo "SELECTED"; } ?>>Ozone Layer</option>
    <option value="ae" <? if $row == "ae" { echo "SELECTED"; } ?>>Alternative Energy</option>
    </select>
    [/php]

    and Bob should be your auntie's husband/brother! :D


  • Closed Accounts Posts: 31 ninimac


    Thanks for the help Mirror.

    I'm getting ab error up and for the life of me can't figure out what the problem is.


    Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in C:\wamp\www\PROJECT\editv3.php on line 60

    line 60:
    <option value="gw" <?PHP if $row == "gw" { echo "SELECTED"; } ?>>Global Warming</option>

    Any ideas???


  • Closed Accounts Posts: 31 ninimac


    Okay changed the <?PHP back to just <? and I'm not getting an error anymore. However its not working....it just keeps displaying the first option. The angle bracket is showing up in the options now ie ">Global Warming" so maybe its something to do with missing quotes or the like???


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    <?PHP if ($row == "gw" { echo "SELECTED"; }); ?>

    :)


  • Closed Accounts Posts: 31 ninimac


    :)
    Parse error: syntax error, unexpected '{', expecting '(' in C:\wamp\www\PROJECT\editv5.php on line 61


  • Advertisement
  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Yeah sorry, I edited my post again, silly mistake!


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Mirror wrote: »
    <?PHP if ($row == "gw" ) { echo "SELECTED"; } ?>

    :)

    Try that?


  • Closed Accounts Posts: 31 ninimac


    Thanks both of you.....it works!!!:D


Advertisement