简体   繁体   中英

Dynamically creating a drop down and filling form fields

This one has completely blown my mind. I have a very basic understanding of PHP and hence my code is so wrong. My aim is to dynamically create a drop menu where users can select an entry from the database, which I have working. From that selection I have a form what I need to auto populate with the row data from the selection from the database. Could anyone help?

(Please note: I know my code is really bad, wrong etc and the deprecated tags. This is all for a college project)

<form id="edit" method="POST" action="">

<?php  

        $con = mysql_connect("localhost","root","");
        if(!$con)
        {
        die('Could not connect: ' . mysql_error());
        }
        mysql_select_db('flexiflash', $con);    

        $query = 'SELECT * FROM event';
        $result = mysql_query($query);

        echo "<select name='events' value='-'>\n";
        echo "<option value='Select event'>Select an event to be changed\n";

        while($row = mysql_fetch_row($result))
        {
        $eventSelect = $row[0];
        echo "<option value='$eventSelect'>$row[1]</option>\n";
        }
        echo "</select>"
        ?>

            <h1>
            <button id="submit" type="submit">Delete</button>
            </h1>




     </form>

THE ABOVE PIECE OF CODE WORKS, BELOW IS NOT WORKING

 <?php
if (isset($_POST['events']))
{
$eventselection = $_POST['events'];
$query = "SELECT * FROM events where event_name = '$eventselection'";   
$row=mysql_fetch_array($query);

}

?>

<form name="editevents" method="POST">

<input type="hidden" value="<?php echo ($row['event_id']); ?>" name="id" /> 

            <h1>
            <label for="eventname">Event Name</label>
            <input id="eventname" type="text" name="eventname" value="<?php echo ($row['event_name']); ?>" />
            </h1>

            <h1>
            <label for="eventtime">Event Time</label>
            <input id="eventtime" type="time" name="eventtime" value="<?php echo ($row['event_time']); ?>" />
            </h1>

</form>

Remove value attribute from drop-down and \\n from you code . Close your default </option> for Select an event to be changed option. in second block of code you missing mysql_query

 echo "<select name='events'>";

Corrected Block of code:-

<form id="edit" method="POST" action="">
    <?php
    $con = mysql_connect("localhost", "root", "");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('flexiflash', $con);

    $query = 'SELECT * FROM event';
    $result = mysql_query($query) or die(mysql_error());

    echo "<select name='events'>";
    echo "<option value=''>Select an event to be changed</option>";
    while ($row = mysql_fetch_row($result)) {
        $eventSelect = $row[0];
        echo "<option value='$eventSelect'>$row[1]</option>";
    }
    echo "</select>";
    ?>
    <h1>
        <button id="submit" type="submit">Delete</button>
    </h1>
</form>

and

 <?php
if (isset($_POST['events']))
{
$eventselection = $_POST['events'];
$query = "SELECT * FROM events where event_name = '$eventselection'";  
$result = mysql_query($query);
$row=mysql_fetch_array($result);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM