I am developing an update form where I would like to have the first field be a list menu which searchs for all the coursenames that are present in a table called trainingtopics. I have the bulk of the code (below) but at the moment im not sure what code to put in the select name tags below- thanks in anticipation. By the way I have done a search but got very confused.

<select name="select">

</select>  
<?php 

$user = "root";
$host = "localhost";
$password = "";
$connection = mysql_connect($host, $root, $password) or die ("Couldn't connect to server.");
$database = "courses";
$db = mysql_select_db($database) or die ("Couldn't select database.");

$sql = "SELECT coursename FROM trainingtopics";

$result = mysql_query($sql) or die(mysql_error());
echo $result."=result<br>";

while($row=mysql_fetch_array($result))
{
}
?>

    You mean how to print all the options in the list?

    <select name="select">
    
    
    <?php
    $user = "root";
    $host = "localhost";
    $password = "";
    $connection = mysql_connect($host, $root, $password) or die ("Couldn't connect to server.");
    $database = "courses";
    $db = mysql_select_db($database) or die ("Couldn't select database.");
    
    $sql = "SELECT coursename FROM trainingtopics";
    
    $result = mysql_query($sql) or die(mysql_error());
    echo $result."=result<br>";
    
    while($row=mysql_fetch_array($result))
    {
       echo "<option value='".$row['course_id']."'>".$row['course_name']."</option>\n";
    }
    ?>
    </select>
    

    On a side note, I would put your db connection information in a seaprate file and just include it in the pages where you need to make a connection...that way all your connection info is only coded once and in the same place.

      Thx for the help- just out of interest - after selecting a coursename from the list how would I show the corresponding records in each of the fields of the form?

        You mean like this?? Select a coursename from the list and hit a submit button (posting the form to another page). On the resulting page do the following... (although you'll probably want to do a little better job of error checking and whatnot)

        $sql = "select * from courses where course_id='".$_POST['course_id']."'";
        $course = @mysql_fetch_array(@mysql_query($sql));
        if(!$course)
           die("Course does not exist");
        
        echo "<form>";
        echo "<input type='text' name='course_name' value='".$course['course_name']."'>";
        echo "</form>";
        

          What I meant was searching for the coursename in the dropdown box, hitting submit and then being able to edit the course details in a form. Is that possible?

            Sorry- what I meant is I do not want to hit submit and go to another page but have a form on the same page as the coursename drop down box. When a coursename is selected it returns the values of that coursename. Like a lookup field in access that acts as a query
            In this case the form contains the following fields

            coursecode (primary key (doesnt appear on form))
            $coursename (the list menu/drop down box which when selected returns corresponding values for:

            accreditingbody
            briefdesc
            aimedat
            level1
            level2
            level3
            elearning
            facetoface
            blended
            elearningtime
            facetofacetime
            blendedtime

              Your choices to implement something like this would be to use javascript with iframes or XMLHttpRequest...

              here is some psuedocode... for the iframe implementation

              1. Populate your dropdown serverside with php
              2. Use the "OnChange" javascript event handler to send a request to an iframe with the ID of the course they selected
              3. The iframe calls a php page that queries your database...it outputs javascript that references form elements on the calling form in a different frame and populates the fields...

              If that's not enough to go off of, let me know and I'll post some code..

                some code woulde be handy thankyou. I have never used Javascript before and I am a relative newbie.

                  is there any alternative around this so that I can stick with using a PHP form that simply searchs a record then allows me to update it- if you know of any examples or tutorials that would be great

                    You would have to select your course and refresh the page...on the resulting page, grab the selected ID and populate the form...that's an option using PHP...but involves a refresh/postback

                      being a newbie I would not know how to do that.

                        Write a Reply...